Skip to content

Running OpenCV4.0.0 C++ function inside GoLang project #30707

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

Closed
StefanCepa opened this issue Mar 9, 2019 · 6 comments
Closed

Running OpenCV4.0.0 C++ function inside GoLang project #30707

StefanCepa opened this issue Mar 9, 2019 · 6 comments

Comments

@StefanCepa
Copy link

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

$ go version 1.12

Does this issue reproduce with the latest release?

Yes

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="-I/usr/local/include"
CGO_CXXFLAGS="--std=c++11"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-L/usr/local/lib -lopencv_core -lopencv_video -lopencv_videoio -lopencv_highgui"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build788193926=/tmp/go-build -gno-record-gcc-switches"
go env Output
$ go env

What did you do?

I am trying to run C++ function in GoLang project by wrapping it in C library first and then linking with my GoLang project. Details can be found on: https://stackoverflow.com/questions/55082336/running-opencv-4-0-0-c-function-in-golang

What did you expect to see?

I expected for my project to be built successfully.

What did you see instead?

command-line-arguments /tmp/go-build823465636/b001/_x002.o: In function "_cgo_dcf5d601f507_Cfunc_OpenVideo": /tmp/go-build/cgo-gcc-prolog:48: undefined reference to `OpenVideo' collect2: error: ld returned 1 exit status

And when running go build -x opencvTest.go I get the following:

WORK=/tmp/go-build568047770

mkdir -p $WORK/b001/

cd /home/stefan/Desktop/GoLang

CGO_LDFLAGS='"-L/usr/local/lib" "-lopencv_core" "-lopencv_video" "-lopencv_videoio" "-lopencv_highgui" "-L/usr/local/lib" "-lopencv_core" "-lopencv_video" "-lopencv_videoio" "-lopencv_highgui"' /usr/local/go/pkg/tool/linux_amd64/cgo -objdir $WORK/b001/ -importpath command-line-arguments -- -I/usr/local/include -I $WORK/b001/ -g -O2 ./opencvTest.go

cd $WORK

gcc -fno-caret-diagnostics -c -x c - || true

gcc -Qunused-arguments -c -x c - || true

gcc -fdebug-prefix-map=a=b -c -x c - || true

gcc -gno-record-gcc-switches -c -x c - || true

cd $WORK/b001

TERM='dumb' gcc -I /home/stefan/Desktop/GoLang -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches

-I/usr/local/include -I ./ -g -O2 -o ./_x001.o -c _cgo_export.c TERM='dumb' gcc -I /home/stefan/Desktop/GoLang -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches

-I/usr/local/include -I ./ -g -O2 -o ./_x002.o -c opencvTest.cgo2.c TERM='dumb' gcc -I /home/stefan/Desktop/GoLang -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches

-I/usr/local/include -I ./ -g -O2 -o ./_cgo_main.o -c _cgo_main.c cd /home/stefan/Desktop/GoLang TERM='dumb' gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -o $WORK/b001/cgo.o $WORK/b001/_cgo_main.o $WORK/b001/_x001.o $WORK/b001/_x002.o -L/usr/local/lib -lopencv_core -lopencv_video -lopencv_videoio -lopencv_highgui -L/usr/local/lib -lopencv_core -lopencv_video -lopencv_videoio -lopencv_highgui

command-line-arguments /tmp/go-build568047770/b001/_x002.o: In function _cgo_dcf5d601f507_Cfunc_OpenVideo':
  /tmp/go-build/cgo-gcc-prolog:48: undefined reference toOpenVideo' collect2: error: ld returned 1 exit status
@AlexRouSg
Copy link
Contributor

You forgot to include your header in the .cpp file

@StefanCepa
Copy link
Author

You are right, My bad. But I still get the same error message

@AlexRouSg
Copy link
Contributor

AlexRouSg commented Mar 10, 2019

Are all the files in the same folder?

EDIT:
Just saw your command go build -x opencvTest.go
That will only compile that specific file. You need to do a go build with no args

@StefanCepa
Copy link
Author

Oh my .. this actually works now. Thank you! Could you tell me how come?

@AlexRouSg
Copy link
Contributor

I just explained it in the earlier post, giving it a file(s) in the arg literally means compile just the file(s) you gave it no more. So the .cpp file doesn't get compiled and added to the binary cause it wasn't in the list of files to compile. Having no args mean compile the folder in the current working directory.

This is documented at https://golang.org/cmd/go/#hdr-Compile_packages_and_dependencies

For further help I suggest you ask around in one of the places in https://github.com/golang/go/wiki/Questions

@ALTree
Copy link
Member

ALTree commented Mar 10, 2019

This seems resolved, so I'm closing this issue.

@ALTree ALTree closed this as completed Mar 10, 2019
@golang golang locked and limited conversation to collaborators Mar 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants