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.24k stars 7.06k forks source link

cocos2d-x compile broken on Ubuntu 17、18 #19246

Open drelaptop opened 5 years ago

drelaptop commented 5 years ago

compile errors like:

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 PIE object; recompile with -fPIC
/usr/bin/ld: ../../../../external/freetype2/prebuilt/linux/64-bit/libfreetype.a(ftinit.linux64.o): relocation R_X86_64_32 against symbol `tt_driver_class' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: ../../../../external/freetype2/prebuilt/linux/64-bit/libfreetype.a(ftsystem.linux64.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: ../../../../external/freetype2/prebuilt/linux/64-bit/libfreetype.a(sfnt.linux64.o): relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: ../../../../external/freetype2/prebuilt/linux/64-bit/libfreetype.a(psaux.linux64.o): relocation R_X86_64_32S against `.rodata' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: ../../../../external/freetype2/prebuilt/linux/64-bit/libfreetype.a(psnames.linux64.o): relocation R_X86_64_32S against `.rodata' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: ../../../../external/freetype2/prebuilt/linux/64-bit/libfreetype.a(pfr.linux64.o): relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: ../../../../external/freetype2/prebuilt/linux/64-bit/libfreetype.a(truetype.linux64.o): relocation R_X86_64_32 against `.text' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: ../../../../external/freetype2/prebuilt/linux/64-bit/libfreetype.a(winfnt.linux64.o): relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: ../../../../external/freetype2/prebuilt/linux/64-bit/libfreetype.a(type1.linux64.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: ../../../../external/freetype2/prebuilt/linux/64-bit/libfreetype.a(type42.linux64.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: ../../../../external/freetype2/prebuilt/linux/64-bit/libfreetype.a(cff.linux64.o): relocation R_X86_64_32 against `.text' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: ../../../../external/freetype2/prebuilt/linux/64-bit/libfreetype.a(type1cid.linux64.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: ../../../../external/freetype2/prebuilt/linux/64-bit/libfreetype.a(bdf.linux64.o): relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: ../../../../external/freetype2/prebuilt/linux/64-bit/libfreetype.a(pcf.linux64.o): relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: ../../../../external/freetype2/prebuilt/linux/64-bit/libfreetype.a(ftf.linux64.o): relocation R_X86_64_32S against `.text' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: ../../../../external/freetype2/prebuilt/linux/64-bit/libfreetype.a(smooth.linux64.o): relocation R_X86_64_32S against `.rodata' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: ../../../../external/freetype2/prebuilt/linux/64-bit/libfreetype.a(raster.linux64.o): relocation R_X86_64_32S against symbol `ft_raster1_renderer_class' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: ../../../../external/freetype2/prebuilt/linux/64-bit/libfreetype.a(autofit.linux64.o): relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: ../../../../external/freetype2/prebuilt/linux/64-bit/libfreetype.a(pshinter.linux64.o): relocation R_X86_64_32S against `.text' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: ../../../../external/freetype2/prebuilt/linux/64-bit/libfreetype.a(ftglyph.linux64.o): relocation R_X86_64_32S against symbol `ft_bitmap_glyph_class' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: ../../../../external/freetype2/prebuilt/linux/64-bit/libfreetype.a(ftstroke.linux64.o): relocation R_X86_64_32S against symbol `ft_outline_glyph_class' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: ../../../../external/freetype2/prebuilt/linux/64-bit/libfreetype.a(ftgzip.linux64.o): relocation R_X86_64_32S against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: ../../../../external/freetype2/prebuilt/linux/64-bit/libfreetype.a(ftlzw.linux64.o): relocation R_X86_64_32S against `.text' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: ../../../../external/freetype2/prebuilt/linux/64-bit/libfreetype.a(ftbitmap.linux64.o): relocation R_X86_64_32S against `.rodata' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
engine/tests/cpp-empty-test/CMakeFiles/cpp-empty-test.dir/build.make:185: recipe for target 'bin/Debug/cpp-empty-test/cpp-empty-test' failed
make[2]: *** [bin/Debug/cpp-empty-test/cpp-empty-test] Error 1
CMakeFiles/Makefile2:1445: recipe for target 'engine/tests/cpp-empty-test/CMakeFiles/cpp-empty-test.dir/all' failed
make[1]: *** [engine/tests/cpp-empty-test/CMakeFiles/cpp-empty-test.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
flykule commented 5 years ago

Hi, you can add " set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -no-pie -fno-exceptions -std=c++11 -Wno-deprecated-declarations -Wno-reorder") " to CMakeList below line 70.It worked for me!

slackmoehrle commented 5 years ago

@flykule Thank you for commenting! I am going to try this out.

slackmoehrle commented 5 years ago

@flykule Thank you so much!

I have to modify a bit to:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -no-pie -fexceptions -std=c++11 -Wno-deprecated-declarations -Wno-reorder")
playmyskay commented 5 years ago

@flykule Thank you so much!

I have to modify a bit to:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -no-pie -fexceptions -std=c++11 -Wno-deprecated-declarations -Wno-reorder")

Compiling with -no-pie option just for one library (freetype2) sounds a little bit strange. In my opinion the prebuilt of libfreetype.a should be compiled with -fPIC option, or i am wrong?

Sorry, i am not a compiler/linker expert.

If you remove the dependency to libfreetype.a (i.e. in link.txt) you are able to successfully link everything with a clean/new project.

I compiled and linked the libfreetype.a by myself and replaced it with the prebuilt. Now it's linking.

slackmoehrle commented 5 years ago

@playmyskay Yeah, true perhaps.

I have tried a few things, so far and nothing works.

elseif(LINUX)
        #target_compile_options(${target} PUBLIC -fPIC)
        #target_compile_options(${target} PUBLIC -no-pie)
playmyskay commented 5 years ago

@slackmoehrle

step1: Compile the freetype library by yourself from: https://github.com/cocos2d/cocos2d-x-3rd-party-libs-src

./build.sh -p=linux --libs=freetype --arch=x86_64 --mode=release

output file: cocos2d-x-3rd-party-libs-src/build/linux/freetype/prebuilt/x86_64/libfreetype.a

step2: Create or use an existing project and replace the file libfreetype.a with the output file of step1. Now link again and it works in my case.

file to replace: _YourProject_/cocos2d/external/freetype2/prebuilt/linux/64-bit/libfreetype.a

The file in repo 'https://github.com/cocos2d/cocos2d-x-3rd-party-libs-bin' must be replaced with a correct binary.

leJTA commented 5 years ago

@slackmoehrle

step1: Compile the freetype library by yourself from: https://github.com/cocos2d/cocos2d-x-3rd-party-libs-src

./build.sh -p=linux --libs=freetype --arch=x86_64 --mode=release

output file: cocos2d-x-3rd-party-libs-src/build/linux/freetype/prebuilt/x86_64/libfreetype.a

step2: Create or use an existing project and replace the file libfreetype.a with the output file of step1. Now link again and it works in my case.

file to replace: _YourProject_/cocos2d/external/freetype2/prebuilt/linux/64-bit/libfreetype.a

The file in repo 'https://github.com/cocos2d/cocos2d-x-3rd-party-libs-bin' must be replaced with a correct binary.

Thank you very much i had the same problem and it solved it

AbaoFromCUG commented 5 years ago

It solve by rollback gcc version and g++ version ,reference the file cocos2d-x-3.17.1/build/install-deps-linux.sh , in the end:

........
sudo update-alternatives --remove-all gcc
sudo update-alternatives --remove-all g++

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 60
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 60

echo "Cocos uses GCC Version: `gcc --version`"
echo "Cocos uses G++ Version: `g++ --version`"
echo "Cocos uses ld Version: `ld --version`"
echo "Cocos uses /usr/bin/ld Version: `/usr/bin/ld --version`"

the "-fPIC" error occur because gcc\g++ version too new (ubuntu17 or 18) , may be cocos2d-x should update gcc version

minggo commented 5 years ago

i think we can use ubuntu 18 for next the testing version

stephenwithav commented 5 years ago

That would be fantastic, @minggo! I just started learning Cocos and this was a pain point.

codewithmichael commented 5 years ago

Just to share another [easier] solution for 3.17.1 (Ubuntu 18.04 LTS / LinuxMint 19.1): I just created a symlink to avoid editing my shared CMake build's link.txt.

$ cd ~/cocos2d-x/external/freetype2/prebuilt/linux/64-bit
$ mv libfreetype.a libfreetype.a.bak
$ ln -s /usr/lib/x86_64-linux-gnu/libfreetype.a

(Edit) Note: The package libfreetype6-dev must be installed.

theSlyest commented 5 years ago

I just had a similar issue with libfmod:

/usr/bin/ld: ../../cocos2d/external/linux-specific/fmod/prebuilt/64-bit/libfmod.so: .dynsym local symbol at index 2 (>= sh_info of 2)

How can I solve it?

jack9603301 commented 4 years ago

Can you improve the documentation for cocos2dx installation in Linux? It’s best not to set the operating system dependency. but I can’t see it in the documentation. I use kubuntu 20.04