linux - 如何在 64 位 linux 中将静态库与 cmake 链接?

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

我在 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/

相关文章:

linux - 如果一个失败,如何同步访问三个linux

assembly - 64 位汇编,何时使用较小尺寸的寄存器

c - CMake下的多个目录

c++ - 将代码移动到函数中时,错误 0xC0000138 在 DLL "myApp.exe"中找不到序号

c++ - CMake Automoc 错误 1 ​​- 无法编译项目

c++ - 从 C++ 调用 C API 时缺少 printf 语句的输出

linux - 如何从多个文件中获取特定文件结果的行号

Linux 守护进程没有 fork ?

linux - OpenMPI 多个 MPI_Send 和 MPI_recv 不工作

c++ - x64 平台上的 GCC 是否默认启用扩展精度?