[CMake] Default INTERFACE_POSITION_INDEPENDENT_CODE value for static library

Ruslan Baratov ruslan_baratov at yahoo.com
Fri Apr 10 12:26:59 EDT 2015


On 10-Apr-15 14:59, Robert Maynard wrote:
> The default for PIC is no for static libraries, see
> http://www.cmake.org/cmake/help/v3.2/prop_tgt/POSITION_INDEPENDENT_CODE.html
Yes, default for shared library is YES and for static library is NO. 
That's the problem when you're trying to link them together. GCC on Linux:

   > cat CMakeLists.txt
   cmake_minimum_required(VERSION 3.0)
   project(Foo)

   add_library(foo STATIC foo.cpp)
   add_library(boo SHARED boo.cpp)

   target_link_libraries(boo PUBLIC foo)

   > cat foo.cpp
   int foo_var;

   int foo() {
     return foo_var;
   }

   > cat boo.cpp
   int foo();

   int boo() {
     return foo();
   }

   > cmake -H. -B_builds -DCMAKE_VERBOSE_MAKEFILE=ON && cmake --build 
_builds
   # no fpic for foo.cpp
   /usr/bin/c++ -o .../foo.cpp.o -c .../foo.cpp
   # fpic for boo.cpp
   /usr/bin/c++   -Dboo_EXPORTS -fPIC -o .../boo.cpp.o -c .../boo.cpp
   /usr/bin/ld: libfoo.a(foo.cpp.o): relocation R_X86_64_PC32 against 
symbol `foo_var' can not be used when making a shared object; recompile 
with -fPIC

Problem occurs on linking stage. To detect this problem on CMake 
configure step you can add INTERFACE_POSITION_INDEPENDENT_CODE manually:

   > cat CMakeLists.txt
   cmake_minimum_required(VERSION 3.0)
   project(Foo)

   add_library(foo STATIC foo.cpp)
   set_target_properties(foo PROPERTIES 
INTERFACE_POSITION_INDEPENDENT_CODE NO)
   add_library(boo SHARED boo.cpp)

   target_link_libraries(boo PUBLIC foo)

   > cmake -H. -B_builds -DCMAKE_VERBOSE_MAKEFILE=ON
   ...
   -- Configuring done
   CMake Error: Property POSITION_INDEPENDENT_CODE on target "boo" does
   not match the INTERFACE_POSITION_INDEPENDENT_CODE property requirement
   of dependency "foo".

So may be CMake can set INTERFACE_POSITION_INDEPENDENT_CODE property 
automatically for static libraries by default?

Ruslo

>
> On Wed, Apr 8, 2015 at 4:56 PM, Ruslan Baratov via CMake
> <cmake at cmake.org> wrote:
>> Hi,
>>
>> I've got next error while trying to link static library to shared one on
>> Linux:
>>      /usr/bin/ld: ...: relocation R_X86_64_32 against `.rodata' can not be
>> used when making a shared object; recompile with -fPIC
>>
>> This error can be detected on CMake configure step instead of linker stage
>> if I add next code:
>>    set_property(TARGET ... PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE YES)
>>
>> in this case error message is:
>>    CMake Error: Property POSITION_INDEPENDENT_CODE on target "..." does
>>    not match the INTERFACE_POSITION_INDEPENDENT_CODE property requirement
>>    of dependency "...".
>>
>> because there is POSITION_INDEPENDENT_CODE=YES default value for shared
>> library. So I wonder why there is no default value
>> INTERFACE_POSITION_INDEPENDENT_CODE=NO for static one?
>>
>> Thanks, Ruslo
>> --
>>
>> Powered by www.kitware.com
>>
>> Please keep messages on-topic and check the CMake FAQ at:
>> http://www.cmake.org/Wiki/CMake_FAQ
>>
>> Kitware offers various services to support the CMake community. For more
>> information on each offering, please visit:
>>
>> CMake Support: http://cmake.org/cmake/help/support.html
>> CMake Consulting: http://cmake.org/cmake/help/consulting.html
>> CMake Training Courses: http://cmake.org/cmake/help/training.html
>>
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html
>>
>> Follow this link to subscribe/unsubscribe:
>> http://public.kitware.com/mailman/listinfo/cmake



More information about the CMake mailing list