Search code examples
linux64-bitcmakestatic-librariesstatic-linking

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

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.


Solution

  • 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.