我在使用 gcc version 12.3.0 (Ubuntu 12.3.0-1ubuntu1~23.04) 编译链接时报错:
/usr/bin/ld: idgen/libidgen.a(SnowWorkerM1.c.o): warning: relocation against ThreadMutex' in read-only section.text'
/usr/bin/ld: idgen/libidgen.a(SnowWorkerM1.c.o): relocation R_X86_64_PC32 against symbol `ThreadMutex' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: bad value
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: [CMakeFiles/YitIdGenLib.dir/build.make:98: yitidgenc.so] Error 1
make[1]: [CMakeFiles/Makefile2:104: CMakeFiles/YitIdGenLib.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
我在使用 gcc version 12.3.0 (Ubuntu 12.3.0-1ubuntu1~23.04) 编译链接时报错: /usr/bin/ld: idgen/libidgen.a(SnowWorkerM1.c.o): warning: relocation against
ThreadMutex' in read-only section
.text' /usr/bin/ld: idgen/libidgen.a(SnowWorkerM1.c.o): relocation R_X86_64_PC32 against symbol `ThreadMutex' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: final link failed: bad value clang: error: linker command failed with exit code 1 (use -v to see invocation) make[2]: [CMakeFiles/YitIdGenLib.dir/build.make:98: yitidgenc.so] Error 1 make[1]: [CMakeFiles/Makefile2:104: CMakeFiles/YitIdGenLib.dir/all] Error 2 make: *** [Makefile:91: all] Error 2原因是由于在生成静态库 libidgen.a 时,SnowWorkerM1.c.o中的某些符号(这里是 ThreadMutex)没有被正确地编译为位置无关代码(Position Independent Code, PIC)。对于共享库,所有包含的代码都必须是位置无关的,以便能够在内存中任意位置加载。 根据编译器提示,增加-fPIC选项