【问题标题】:How to link static library with cmake in 64bit linux?如何在 64 位 linux 中将静态库与 cmake 链接?
【发布时间】:2011-09-07 04:59:01
【问题描述】:

我在 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")

但没有任何改变。

你能给我一些关于我应该做什么的建议吗?

非常感谢您阅读这个问题。

【问题讨论】:

  • 尝试自己构建 Imlib2,可能是打包或上游故障。
  • 我这样做并链接了我自己的 Imlib2.a,但问题仍然存在。
  • 我猜你必须用-fPIC 重建Imlib2.a 作为common x64 issue

标签: linux 64-bit cmake static-libraries static-linking


【解决方案1】:

您需要在启用-fPIC 的情况下自己构建Imlib2(实际上是所有共享库)。请查看this article,了解为什么会发生这种情况。

【讨论】:

    【解决方案2】:

    修复了您需要为 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()

    【讨论】:

      猜你喜欢
      • 2011-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多