cocos2d / cocos2d-x

Cocos2d-x is a suite of open-source, cross-platform, game-development tools utilized by millions of developers across the globe. Its core has evolved to serve as the foundation for Cocos Creator 1.x & 2.x.
https://www.cocos.com/en/cocos2d-x
18.22k stars 7.05k forks source link

Build cocos2d-x as a shared library on Linux with BUILD_SHARED_LIBS:BOOL=ON fails with "libfreetype.a(ftbase.linux64.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC" #18399

Open cirosantilli opened 7 years ago

cirosantilli commented 7 years ago

Why I want to do this:

Specs:

Steps to Reproduce:

  1. cmake -DBUILD_SHARED_LIBS:BOOL=ON && make (Using gcc 6.3 from my distro)

Outcome: link fails with:

/usr/bin/ld: ../../../external/freetype2/prebuilt/linux/64-bit/libfreetype.a(ftbase.linux64.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC

However, freetype is already compiled with -fPIC if you compile it yourself, https://github.com/cocos2d/cocos2d-x-3rd-party-libs-src/blob/d020d9ef98dea233992802d151afdbbbff3acd8e/contrib/src/main.mak#L288 so I think it is about having the prebuilt binary built with an old GCC? So I just compile it myself with:

./build.sh -p=linux --libs=freetype

and copy into the prebuilts and it works.

Next, the link fails with:

/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/libcurl.so: undefined reference to symbol 'inflateEnd'                                                   
/usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/libz.so: error adding symbols: DSO missing from command line

libz.a gets built together with freetype: linux/z/prebuilt/x86_64/libz.a, which uses libz functions, however for some reason it is not present in the binaries https://github.com/cocos2d/cocos2d-x-3rd-party-libs-bin/tree/8d1bf3e7199948755c98a36a59bf15c50943e27e and make VERBOSE=1 says it is not used during link, only:

-lz

which ends up looking for my system's shared library, which cannot be used for a shared link .

I also don't understand the error because: readelf says that the /usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/libz.so on my system does have inflateEnd.

I then copy pasted the failing linker command, and replaced all -lz entries with the full path to the built libz.a and the link now works, and cpp-tests runs!

However, even after the link works, I sill have the following conerns:

simpers commented 7 years ago

Any progress on this yet? Right now I'm using v3.13.1 for building on Linux and v3.16 for iOS, due to all previous versions apparently being deprecated due to a system-call which does not exist in iOS 11 anymore.

cirosantilli commented 7 years ago

@simpers nope. Cocos really needs some packaging love :-)