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 SINGLE function #40512

Closed
shanduur opened this issue Jul 31, 2020 · 4 comments
Closed

cmd/cgo: undefined reference to SINGLE function #40512

shanduur opened this issue Jul 31, 2020 · 4 comments

Comments

@shanduur
Copy link

shanduur commented Jul 31, 2020

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

$ go version
go version go1.14.4 linux/amd64

Does this issue reproduce with the latest release?

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/shanduur/.cache/go-build"
GOENV="/home/shanduur/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/shanduur/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
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-build113119094=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Little bit of backstory:

  • I started working with the GoCV, and I found I am missing one key feature for my project. I decided to implement it by myself, so I created a fork and started writing it. I crumbled into some issues, that I fortunately fixed, but finally I am stuck with error. I am also using other functions calling CGO code from this particular file (features2d.go), and they are working as they should.

To reproduce the problem, install my fork of GoCV (for example with make install after performing go get inside GoCV src folder) and try to run cmd/hello-sift/main.go file on features2d/drawMatches branch.

What did you expect to see?

Working program, without compilation problem.

What did you see instead?

$ go run ./cmd/hello-sift/main.go /home/shanduur/Pictures/box.png /home/shanduur/Pictures/box_in_scene.png
# gocv.io/x/gocv
/usr/bin/ld: $WORK/b032/_x006.o: in function `_cgo_9737641c630c_Cfunc_DrawMatches':
/tmp/go-build/cgo-gcc-prolog:333: undefined reference to `DrawMatches'
collect2: error: ld returned 1 exit status
@ianlancetaylor ianlancetaylor changed the title CGO Undefined reference to SINGLE function cmd/cgo: undefined reference to SINGLE function Jul 31, 2020
@mdempsky
Copy link
Contributor

It seems like you figured out the issue: shanduur/gocv@6ea7213

@mdempsky
Copy link
Contributor

mdempsky commented Jul 31, 2020

For posterity, the issue was features2d.h declared:

extern "C" {
void DrawMatches(..., struct DMatch matches1to2[], ...);
}

but features2d.cpp defined:

void DrawMatches(..., struct DMatches matches1to2, ...) { ... }

C++ allows function overloading, so the compiler treats this as a definition of a separate function from the one declared as extern "C".

@shanduur
Copy link
Author

Yup, figured this out, it was my mistake. Thanks for help!

@mdempsky
Copy link
Contributor

In the future, if you discover the root cause of an issue like this, it would be polite to update the issue with those further details. I spent a fair bit of time investigating and struggling to reproduce this issue before discovering you'd already fixed it.

@golang golang locked and limited conversation to collaborators Jul 31, 2021
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

3 participants