Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/cgo: undefined reference to OpenCV C++ function call #30800

Closed
StefanCepa opened this issue Mar 13, 2019 · 4 comments
Closed

cmd/cgo: undefined reference to OpenCV C++ function call #30800

StefanCepa opened this issue Mar 13, 2019 · 4 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@StefanCepa
Copy link

StefanCepa commented Mar 13, 2019

What version of Go are you using (go version)? 1.12

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/stefan/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/stefan/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build128313540=/tmp/go-build -gno-record-gcc-switches"
go env Output
$ go env

What did you do?

I have successfully installed gocv package from https://github.com/hybridgroup/gocv and I am trying to run C++ code which I wrapped in C library inside my Go project. But there seems to be the problem when I try to call cv::optflow::createOptFlow_DualTVL1() method. I get undefined reference error. I dont know why, since in optflow.hpp file there is a method called createOptFlow_DualTVL1()

Here is my main.go:

package main


/*
#cgo LDFLAGS: -L/usr/local/lib -lopencv_core -lopencv_video -lopencv_videoio -lopencv_highgui -lopencv_tracking -lopencv_optflow
#include "dense_flow.h"
*/
import (
       "C"
       "gocv.io/x/gocv"
)

func main(){

}

Here is dense_flow.h:

#ifndef DENSEFLOW_DENSE_FLOW_H
#define DENSEFLOW_DENSE_FLOW_H


#ifdef __cplusplus
#define EXTERNC extern "C"
#else
#define EXTERNC
#endif

#include "opencv2/opencv.hpp"
#include "opencv2/optflow.hpp"
#include <iostream>
	
using namespace std;
using namespace cv;
using namespace cv::optflow;

EXTERNC void CalcOpticalFlowTVL1(Mat* , Mat*, Mat*);


#endif //DENSEFLOW_DENSE_FLOW_H

And here is dense_flow.cpp:

#include "dense_flow.h"

#ifdef __cplusplus
#define EXTERNC extern "C"
#else
#define EXTERNC
#endif


EXTERNC void CalcOpticalFlowTVL1(Mat* previousFrame, Mat* currentFrame, Mat* flow){
	Ptr<DualTVL1OpticalFlow> tvl1 = createOptFlow_DualTVL1();
	tvl1->calc(*previousFrame, *currentFrame, *flow);
}

What did you expect to see?

For my program to be built.

What did you see instead?

#_/home/stefan/Desktop/GoLang/OpticalFlow
/tmp/go-build442589631/b001/_x003.o: In function CalcOpticalFlowTVL1': ./dense_flow.cpp:12: undefined reference to cv::optflow::createOptFlow_DualTVL1()'
collect2: error: ld returned 1 exit status

@katiehockman katiehockman changed the title Undefined Reference to OpenCV C++ function call cgo: undefined reference to OpenCV C++ function call Mar 19, 2019
@katiehockman katiehockman changed the title cgo: undefined reference to OpenCV C++ function call go/src/runtime/cgo: undefined reference to OpenCV C++ function call Mar 19, 2019
@katiehockman
Copy link
Contributor

/cc @ianlancetaylor
I don't know enough about cgo to guess what's going on here. Nothing is jumping out, any ideas?

@katiehockman katiehockman added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 19, 2019
@ianlancetaylor
Copy link
Contributor

When using a Unix linker the order of -l options matters. Without knowing anything about this code I would guess that you need to put -lopencv_core at the end of the list, not the beginning.

If that doesn't help, use nm -C to find which library defines the function cv::optflow::createOptFlow_DualTVL1().

@ianlancetaylor ianlancetaylor changed the title go/src/runtime/cgo: undefined reference to OpenCV C++ function call cmd/cgo: undefined reference to OpenCV C++ function call Mar 19, 2019
@ianlancetaylor ianlancetaylor added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Mar 19, 2019
@gopherbot
Copy link

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@PoisonBOx
Copy link

have you ever solved the problem?

@golang golang locked and limited conversation to collaborators Apr 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

5 participants