206

I've come across at least 3 ways to do this and I'm wondering which is the idiomatic way. This needs to be done almost universally to any static library. I'm surprised that the Makefile generator in CMake doesn't automatically add this to static libraries. (unless I'm missing something?)

target_compile_options(myLib PRIVATE -fPIC)

add_compile_options(-fPIC)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fpic")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fpic")

I believe there might also be other variations. (please edit my question if you find one)

If you happen to know the answer to this question, do you also know if there is a way to cause a 3rd party CMake project to be compiled with this flag without modifying its CMakeLists.txt file? I have run across static libraries missing that flag. It causes problems when compiling a static library into a dynamic library.

You get:

relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
101010
  • 14,866
  • 30
  • 95
  • 172

2 Answers2

319

You can set the position independent code property on all targets:

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

or in a specific library:

add_library(lib1 lib1.cpp)
set_property(TARGET lib1 PROPERTY POSITION_INDEPENDENT_CODE ON)

Reference:

starball
  • 20,030
  • 7
  • 43
  • 238
Amadeus
  • 10,199
  • 3
  • 25
  • 31
  • 1
    The second method appears to be the right way to add it to a 3rd party lib as well. Perfect. – 101010 Jul 11 '16 at 03:04
  • Hrm -- I wonder how would you add this to a project added by ExternalProject_Add? – 101010 Jul 16 '16 at 02:59
  • 3
    @010110110101 I don't know if it works, but you can try specifying, at ExternalProject_Add with option `CMAKE_CACHE_ARGS "-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=true"` – Amadeus Jul 16 '16 at 03:42
  • 29
    The `CMAKE_POSITION_INDEPENDENT_CODE` property is set by default for `SHARED` targets, there is no need to set it explicitly. – robert Oct 13 '16 at 14:18
  • Excuse this noob question - if I have a CMakeLists with 5 ADD_SUBDIRECTORIES, would I need to add this to each add_subdirectory that has a static lib? – Carbon Jan 09 '18 at 18:16
  • 1
    @Carbon if you're using the `set` command (the first version above), then no. If you're using the second version, it's per library, not per directory. – pattivacek Feb 19 '18 at 08:39
  • 1
    There is relatively undocumented behavior for `CMAKE_POSITION_INDEPENDENT_CODE` described in this [post](https://github.com/arsenm/sanitizers-cmake/issues/13#issuecomment-315532253). This may have undesired consequences, particularly for Qt executables requiring `fPIC`, not `fPIE`. So `target_compile_options()` may be the best option in some cases. – Kevin Oct 22 '19 at 18:22
  • Using `set(CMAKE_POSITION_INDEPENDENT_CODE ON)` needs to be done _*before*_ the target is created. It has no effect on the target after it's already been created. That is has been my experience using cmake v3.16.4. – John Rocha Jun 23 '21 at 17:57
  • When using `CMAKE_POSITION_INDEPENDENT_CODE` I get linking errors about "copy relocation against non-copyable". With `target_compile_options` with `-fPIC` it works fine – Post Self Jan 17 '23 at 22:38
61

You can also pass the following command line option to cmake (in case this is not your cmake project and/or you can't or don't want to modify the project files):

-DCMAKE_POSITION_INDEPENDENT_CODE=ON
scai
  • 20,297
  • 4
  • 56
  • 72