Skip to content

cgo linking issue with C++ dynamic libraries #31723

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
DineshReddyK opened this issue Apr 28, 2019 · 4 comments
Closed

cgo linking issue with C++ dynamic libraries #31723

DineshReddyK opened this issue Apr 28, 2019 · 4 comments

Comments

@DineshReddyK
Copy link

DineshReddyK commented Apr 28, 2019

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

go version go1.10.1 linux/amd64

Does this issue reproduce with the latest release?

Yes.

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

go env Output
$ go env
cp:~/containerfolder/API_Gateway_5G_cOAM_Productized$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/root/go"
GORACE=""
GOROOT="/usr/lib/golang"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/golang/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
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-build634932465=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I have a C++ shared library in /opt/xx/lib64. Headers are in C. In my go code, I am including proper header and linker options using CPPFLAGS/CFLAGS and LDFLAGS. Still while building go I am getting 'undefined reference` error.

import (
    //#cgo LDFLAGS: -L/opt/xxx/lib64/ -lsessionmgmt
    //#cgo CPPFLAGS: -I/opt/xxx/include/
    //#include <stdlib.h>
    //#include <sessionMgmt/sessionMgmt.h>
    "C"
    "unsafe"
)

What did you expect to see?

successful build

What did you see instead?

I am getting this linking errors:

/tmp/go-build609413262/b163/_x002.o: In function `_cgo_e38947506e9d_Cfunc_um_get_lastlogin_info':
/tmp/go-build/cgo-gcc-prolog:52: undefined reference to `um_get_lastlogin_info'
collect2: error: ld returned 1 exit status
make[3]: *** [Makefile:48: vet] Error 2

Library is exist in the right path and do contain the required method. Not sure why this error.

Binary file /opt/xxx/lib64/libsessionmgmt.so matches

Also tried using the pkg-config with cgo. Still the same linking error appears.

@beoran
Copy link

beoran commented Apr 29, 2019

Is the function um_get_lastlogin_info compiled as extern "C" in the C++ library ? Otherwise this might be due to the name mangling C++ applies.

@DineshReddyK
Copy link
Author

Thanks a lot :). Yes, extern "C" was missing. After generating the .so with the changes, linking worked fine.

But from the cgo documentation, I see that there is already support for c++ right. Any reason, why is it mandatory to keep extern "C".

@AlexRouSg
Copy link
Contributor

cgo is integrating C and Go, not C++ and Go. You can compile and use C++ sources/libs but to use those inside Go sources you need to make it into something you can call from C.

@DineshReddyK
Copy link
Author

Thanks for the info. I will close this.

@golang golang locked and limited conversation to collaborators Apr 28, 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