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

问题描述:

我在linux中用cmake构建我的项目。如何在64位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") 

,但没有改变。

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

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

+0

尝试建立Imlib2自己,它可能是一个包装或上游故障。 –

+0

我这样做,并链接我自己的Imlib2.a但该问题仍然存在。 – Oz6848

+0

我想你必须用'-fPIC'重建'Imlib2.a'作为[常见的x64问题](http://www.gentoo.org/proj/en/base/amd64/howtos/index.xml?part = 1&章= 3) –

你需要自己建立Imlib2(实际上是)所有的共享库,其中包括-fPIC。请看this article以解释为什么会发生这种情况。

该问题需要用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()