如何在64位linux中使用cmake链接静态库?

发布于 2024-12-03 07:35:41 字数 774 浏览 7 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瀟灑尐姊 2024-12-10 07:35:41

您需要使用 -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.

哆兒滾 2024-12-10 07:35:41

要修复该问题,您需要使用 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()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文