Reputation: 2214
I am altering a CMake file which works on Windows to create a shared object in Linux.
The CMake file generates a makefile and when I did "make" on Linux, it built objects of foo libraries and it displays linking of all four libraries. For example:
Linking CXX static library lib_foo_d.a
and the final shared object
Linking CXX static library lib_scen_files_d.a
but at the end it displays
/usr/bin/ld: lib_foo3/lib_foo3_d.a(chap_alt_scence_defs.cpp.o):
relocation R_X86_64_32 against `.rodata' can not be used when making a shared object;
recompile with -fPIC
lib_foo3/lib_foo3_d.a: could not read symbols: Bad value
collect2: ld returned 1 exit status
I have tried a few other options, but no joy. Below is my CMakeLists.txt. Any help/criticism will be beneficial to me.
CMAKE_MINIMUM_REQUIRED( VERSION 2.8 )
CMAKE_POLICY( SET CMP0017 NEW )
PROJECT( disk_space_model )
INCLUDE( ../libs/helper_functions.cmake )
INCLUDE_THIRD_PARTY_SFC()
find_path_for_libs()
add_s_library( lib_foo1 )
add_s_library( lib_foo2)
add_s_library( lib_foo3)
add_s_library( lib_foo4)
SET( HEADER_FILES
stdafx.h
INS_sensor_model.h
)
SET( SOURCE_FILES
Disk_space_model.cpp
)
SET( RESOURCE_FILES
"Disk Space DLL.rc"
resource.h
)
COMMON_SETUP()
set( LIB_FILES
lib_foo
lib_foo1
lib_foo3
lib_foo4 )
set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--export-all-symbols")
ADD_LIBRARY( disk_space_model SHARED ${SOURCE_FILES} ${HEADER_FILES} ${RESOURCE_FILES} ${CMAKE_HELPER_FILES} )
SET_OUTPUT_DIRS( disk_space_model )
TARGET_LINK_LIBRARIES( disk_space_model ${LIB_FILES} )
Thanks ...
Upvotes: 0
Views: 1268
Reputation: 171127
The linker is telling you what is wrong: the object is used in a shared library, but wasn't compiled with -fPIC
. You need to add this flag somehow.
When using CMake 2.8.9 or later, you can simply set property POSITION_INDEPENDENT_CODE
on the target(s) in question (the static libraries).
With prior versions of CMake, you need to add the flag directly to the targets' COMPILE_FLAGS
properties (in the proper spelling for your compiler, probably just -fPIC
).
Upvotes: 1
Reputation: 44248
In order to build shared library on Linux all sources, who's object files linked to it, must be compiled as relocatable (-fPIC option as linker complains). You try to link static libraries lib_foo1 lib_foo2 lib_foo3 etc into your shared library, and it fails. So possible solutions:
Upvotes: 0