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 · 3 comments

Comments

@StefanCepa
Copy link

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

This comment has been minimized.

Copy link
Contributor

commented Mar 19, 2019

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

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Mar 19, 2019

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

@gopherbot

This comment has been minimized.

Copy link

commented Apr 19, 2019

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.)

@gopherbot gopherbot closed this Apr 19, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.