简体   繁体   中英

golang gcc c++ existing static library can not be used when making a PIE object; recompile with -fPIC

I tried to link an existing C++ library to go code. The C++ library only has a static library and a header file, no source code.

I used swigc to generate a libfoo.go and I wrote a simple libb.go to build this library. This worked well on ubuntu 16.04 with gcc-6 earlier.

However, once I upgraded to ubuntu 18, and even with older go1.9 and gcc-6, which used to work, I am hitting the following error:

/usr/bin/ld: ./lib/libfoo.a(parser.o): relocation R_X86_64_32S against symbol `xmlSAX2IgnorableWhitespace' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: ./lib/libfoo.a(tree.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIC

I have downgraded both go compiler and gcc to the version that used to work.

The following is the libb.go that used to work

/*
#cgo CXXFLAGS: -std=c++11 -D_GLIBCXX_USE_CXX11_ABI=0 -w
#cgo CFLAGS: -I${SRCDIR}/include -w
#cgo LDFLAGS: -Wl -rpath,./lib, -L${SRCDIR}/lib -l:libfoo.a  -l:libxml2.a 
*/
import "C"

What should I do get this fixed? I searched and it seems that I have to recompile that static library, which is mission impossible in my case. I tried to pass the -no-pie parameter to LDFLAGS, that didn't work either.

I also run into this problem these days. Finally, I found that adding below lines

"env CGO_LDFLAGS=-no-pie"

to go build command line could solve this problem. For example,

"env CGO_LDFLAGS=-no-pie go build -o main main.go"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM