How to link static library with cmake in 64bit linux?
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
开发者_C百科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.
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.
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()
精彩评论