MantisBT - CMake
View Issue Details
0014968CMakeCMakepublic2014-06-11 12:082015-01-05 08:38
Nico Schlömer 
 
normalmajoralways
closedwon't fix 
 
 
0014968: -fPIC not properly forwarded through OBJECT libraries
When compiling a project that includes an OBJECT library, e.g.,
```
project(mytest C)
add_library(test2 OBJECT test2.c)
add_library(test test.c $<TARGET_OBJECTS:test2>)
```
and if BUILD_SHARED_LIBS is set to ON, the compile switch `-fPIC` will be correctly added to the sources of `test`, but not to the sources of `test2`. The library fails to link.
The attachment contains a minimal failing example.
No tags attached.
bz2 2014-06-11-fpic-object-lib.tar.bz2 (593) 2014-06-11 12:08
https://public.kitware.com/Bug/file/5161/*
Issue History
2014-06-11 12:08Nico SchlömerNew Issue
2014-06-11 12:08Nico SchlömerFile Added: 2014-06-11-fpic-object-lib.tar.bz2
2014-06-11 13:08Brad KingNote Added: 0036159
2014-06-11 14:29Nico SchlömerNote Added: 0036160
2014-06-11 15:07Brad KingNote Added: 0036161
2014-06-11 15:17Nico SchlömerNote Added: 0036162
2014-06-11 15:44Brad KingStatusnew => resolved
2014-06-11 15:44Brad KingResolutionopen => won't fix
2015-01-05 08:38Robert MaynardNote Added: 0037564
2015-01-05 08:38Robert MaynardStatusresolved => closed

Notes
(0036159)
Brad King   
2014-06-11 13:08   
This is by design. The compilation of an OBJECT library is not influenced by its consumers. You can set the POSITION_INDEPENDENT_CODE target property on the OBJECT library.
(0036160)
Nico Schlömer   
2014-06-11 14:29   
Hm, too bad. It's been quite comfortable not having to deal with specific flags needed for shared library objects.

Would you recommend to do
```
IF(BUILD_SHARED_LIBS)
  SET(CMAKE_POSITION_INDEPENDENT_CODE ON)
ENDIF()
```
or is there an even better way to handle things?
(0036161)
Brad King   
2014-06-11 15:07   
Re 0014968:0036160: That will work but it will also enable -fPIC on libraries that are explicitly marked STATIC. You could set it directly on the object library:

add_library(test2 OBJECT test2.c)
if(BUILD_SHARED_LIBS)
  # These objects will be consumed by a shared library.
  set_property(TARGET test2 PROPERTY POSITION_INDEPENDENT_CODE ON)
endif()
(0036162)
Nico Schlömer   
2014-06-11 15:17   
Ideally I'd want CMake to add `-fPIC` whenever it is necessary, but I guess I live with setting PIC globally ON.

Feel free to close.
(0037564)
Robert Maynard   
2015-01-05 08:38   
Closing resolved issues that have not been updated in more than 4 months.