我在 Linux 中使用 cmake 构建我的项目。
我通过使用链接了一些静态库
set(BUILD_SHARED_LIBS FALSE)
set(CMAKE_EXE_LINKER_FLAGS "-static")
target_link_libraries(MyProject /usr/lib/libImlib2.a)
它在 32 位 linux(在我的例子中是 Ubuntu)中完美运行,而不是在 64 位 Ubuntu 中
出现此错误消息。
/usr/bin/ld: /usr/lib64/libImlib2.a(api.o) : relocation R_X86_64_32 againts '.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
/usr/lib64/libImlib2.a : could not read symbols: Bad value
collect2:ld returned 1 exit status
我发现的一些文档说这是关于 64 位 linux 的问题,需要设置标志。
所以我补充
set(CMAKE_CXX_FLAGS_DEBUG "-fPIC")
set(CMAKE_CXX_FLAGS_RELEASE "-fPIC")
但没有任何改变。
您能给我一些建议吗?
非常感谢您阅读这个问题。
最佳答案
您需要使用 -fPIC
自行构建 Imlib2
(实际上是所有共享库)。看看this article解释为什么会这样。
关于linux - 如何在 64 位 linux 中将静态库与 cmake 链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7329029/