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

601 views
Skip to first unread message

czha...@gmail.com

unread,
Jun 30, 2019, 8:22:08 PM6/30/19
to golang-nuts
I posted this question in SO, and was suggested to post here for more help:


----------------------

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.



Thanks!

----------------------


1, first thing first, I don't have the c++ source code, so I can't recompile it. 

2, I am using ubuntu 18, initially, I believe I install go via "apt-get install", but later I uninstall that version using "apt-get remove" , and then downloaded go1.9 pkg from official website and install directly. 

3, go env gives:

GOARCH="amd64"

GOBIN=""

GOEXE=""

GOHOSTARCH="amd64"

GOHOSTOS="linux"

GOOS="linux"

GOPATH="/home/coolart/work/go"

GORACE=""

GOROOT="/usr/local/go"

GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"

GCCGO="gccgo"

CC="gcc"

GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build628698308=/tmp/go-build -gno-record-gcc-switches"

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"


4, some system information


go version go1.9.7 linux/amd64


No LSB modules are available.

Distributor ID: Ubuntu

Description: Ubuntu 18.04.2 LTS

Release: 18.04

Codename: bionic


Using built-in specs.

COLLECT_GCC=gcc

COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper

Target: x86_64-linux-gnu

Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.5.0-12ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu

Thread model: posix

gcc version 5.5.0 20171010 (Ubuntu 5.5.0-12ubuntu1)


Using built-in specs.

COLLECT_GCC=g++

COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper

Target: x86_64-linux-gnu

Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.5.0-12ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu

Thread model: posix

gcc version 5.5.0 20171010 (Ubuntu 5.5.0-12ubuntu1)





nju...@gmail.com

unread,
Dec 5, 2019, 12:27:21 PM12/5/19
to golang-nuts
I also run into this problem these days. Finally I found that adding "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".
Reply all
Reply to author
Forward
0 new messages