MantisBT - CMake
View Issue Details
0013479CMakeCMakepublic2012-08-17 05:232013-01-09 14:05
Markus Blatt 
Brad King 
normalminoralways
closedfixed 
x86_64GNU/LinuxDebian wheezy
CMake 2.8.9 
CMake 2.8.10CMake 2.8.10 
0013479: add_library documentation does not mention POSITION_INDEPENDENT_CODE property
I need to used the object libraries for building libs out of serveral convenient libraries.

When trying to build shared libraries by using set(BUILD_SHARED_LIBS ON) the compilation with gcc fails with

"[100%] Building CXX object CMakeFiles/a.dir/dummy.cc.o
[100%] Built target a
Linking CXX shared library libb.so
/usr/bin/ld: CMakeFiles/a.dir/dummy.cc.o: relocation R_X86_64_32 against `_ZSt4cout' can not be used when making a shared object; recompile with -fPIC
CMakeFiles/a.dir/dummy.cc.o: could not read symbols: Bad value
collect2: error: ld returned 1 exit status
make[2]: *** [libb.so] Error 1
make[1]: *** [CMakeFiles/b.dir/all] Error 2
make: *** [all] Error 2
"
Setup the attached testcase and call cmake && make
No tags attached.
gz testcase.tar.gz (564) 2012-08-17 05:23
https://public.kitware.com/Bug/file/4426/testcase.tar.gz
Issue History
2012-08-17 05:23Markus BlattNew Issue
2012-08-17 05:23Markus BlattFile Added: testcase.tar.gz
2012-08-17 08:36Brad KingNote Added: 0030688
2012-08-31 06:20Markus BlattNote Added: 0030807
2012-08-31 07:40Brad KingNote Added: 0030809
2012-08-31 07:40Brad KingAssigned To => Brad King
2012-08-31 07:40Brad KingPriorityhigh => normal
2012-08-31 07:40Brad KingSeveritymajor => minor
2012-08-31 07:40Brad KingStatusnew => resolved
2012-08-31 07:40Brad KingResolutionopen => fixed
2012-08-31 07:40Brad KingFixed in Version => CMake 2.8.10
2012-08-31 07:40Brad KingTarget Version => CMake 2.8.10
2012-08-31 07:40Brad KingSummarybuildig shared libs from object libraries fails because of missing -fPIC opion => add_library documentation does not mention POSITION_INDEPENDENT_CODE property
2013-01-09 14:05Robert MaynardNote Added: 0032083
2013-01-09 14:05Robert MaynardStatusresolved => closed

Notes
(0030688)
Brad King   
2012-08-17 08:36   
The BUILD_SHARED_LIBS option only affects whether add_library defaults to SHARED or STATIC. CMake has no idea what you're going to do with the object files in an OBJECT library. In CMake 2.8.9 you can set the POSITION_INDEPENDENT_CODE property:

 http://www.cmake.org/cmake/help/v2.8.9/cmake.html#prop_tgt:POSITION_INDEPENDENT_CODE [^]

on the object library to get -fPIC. Add this line to your example:

 set_property(TARGET a PROPERTY POSITION_INDEPENDENT_CODE 1)
(0030807)
Markus Blatt   
2012-08-31 06:20   
I did not know this property. Thanks a lot.

Is it mentioned in the add_library documentation?

Wouldn't it be more user-friendly to honor a supplied SHARED option?
(0030809)
Brad King   
2012-08-31 07:40   
Re 0013479:0030807: The property was just added in CMake 2.8.9. I've updated the documentation of add_library to mention it:

 http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=4eaea3c3 [^]
(0032083)
Robert Maynard   
2013-01-09 14:05   
Closing resolved issues that have not been updated in more than 4 months.