[CMake] Default INTERFACE_POSITION_INDEPENDENT_CODE value for static library
Robert Maynard
robert.maynard at kitware.com
Fri Apr 10 14:04:06 EDT 2015
(In my own opinion) The current behavior is mainly historical. We have
previously never enabled pic'iness for static libraries, so changing
that behavior would require a new policy (historical), and second
really only x86_64 linux requires pic'iness, 32bit static libraries
could be embedded into 32bit dynamic libraries.
Now for current projects that want all libraries built with
POSITION_INDEPENDENT_CODE enabled, I would use
CMAKE_POSITION_INDEPENDENT_CODE to set the default to TRUE.
Really the discussion about changing the default CMake behavior is
better suited to the CMake Developers list.
On Fri, Apr 10, 2015 at 12:26 PM, Ruslan Baratov
<ruslan_baratov at yahoo.com> wrote:
> 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