如何在64位linux中使用cmake链接静态库?
我在 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")
但没有任何改变。
您能给我一些关于我应该做什么的建议吗?
非常感谢您阅读这个问题。
I build my project with cmake in linux.
I link some static libraries by using
set(BUILD_SHARED_LIBS FALSE)
set(CMAKE_EXE_LINKER_FLAGS "-static")
target_link_libraries(MyProject /usr/lib/libImlib2.a)
It work perfectly in 32bit linux(In my case, Ubuntu), not in 64bit Ubuntu
This error message appears.
/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
Some document what I found says it's problem about 64bit linux, need to set flags.
So I add
set(CMAKE_CXX_FLAGS_DEBUG "-fPIC")
set(CMAKE_CXX_FLAGS_RELEASE "-fPIC")
but nothing is changed.
Could you give me some advice about what I should do?
Thank you very much for reading this question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用
-fPIC
自行构建Imlib2
(实际上是所有共享库)。请查看这篇文章,了解发生这种情况的原因。You need to build
Imlib2
(~all shared libraries, in fact) yourself with-fPIC
on. Take a look at this article for an explanation of why this is happening.要修复该问题,您需要使用 x86_64 架构的
-fPIC
重新编译。有关详细信息,请访问:
http: //www.technovelty.org/c/position-independent-code-and-x86-64-libraries.html
https://en.wikipedia.org/wiki/Position-independent_code
添加以下行进入您的主 CMakeLists.txt
IF(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
ADD_DEFINITIONS(-fPIC)
ENDIF()
The fix that issue you need to recompile with
-fPIC
for x86_64 architecture.More information about that is available at:
http://www.technovelty.org/c/position-independent-code-and-x86-64-libraries.html
https://en.wikipedia.org/wiki/Position-independent_code
Add following lines into your main CMakeLists.txt
IF(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
ADD_DEFINITIONS(-fPIC)
ENDIF()