# CMake adds -fPIC for all shared libraries

**URL:** https://discourse.cmake.org/t/cmake-adds-fpic-for-all-shared-libraries/12690
**Category:** Usage
**Tags:** os:linux, gen:makefiles, gen:ninja
**Created:** [October 6, 2024, 4:52am UTC](https://discourse.cmake.org/t/cmake-adds-fpic-for-all-shared-libraries/12690 "2024-10-06T04:52:40Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![RedNicStone](https://discourse.cmake.org/user_avatar/discourse.cmake.org/rednicstone/32/4987_2.png) [@RedNicStone](https://discourse.cmake.org/u/RedNicStone)
#### Post date: [October 6, 2024, 4:52am UTC](https://discourse.cmake.org/t/cmake-adds-fpic-for-all-shared-libraries/12690/1 "2024-10-06T04:52:40Z")

</div>

Hello there.  
For a project I am trying to build an elf relocatable binary that has no PLT/GOT and relies purely on relocations. I’m aware that this can be archived in gcc (possibly with `-mcmodel=large`).  
However the generator always appends `-fPIC` to the link commands, no matter if `POSITION_INDEPENDENT_CODE` is set to False not.  
This happens with both gnu make and ninja. My current (temporary) solution to this problem is to sed replace `-fPIC` in the generator files.

Here is a quick MRE:  
- `CMakeList.txt`

```cmake
add_library(test SHARED test.c)

set_target_properties(test
    PROPERTIES
        POSITION_INDEPENDENT_CODE False
)

```

When running this with `cmake -G "Unix Makefiles"` this produces the following:  
- `CMakeFiles/test.dir/link.txt:1`

```auto
/usr/lib64/ccache/cc -fPIC -shared -Wl,-soname,libtest.so -o libtest.so CMakeFiles/test.dir/test.o

```

When running with `cmake -G "Ninja"` instead it produces something simmilar:  
- `MakeFiles/rules.ninja:28`

```auto
command = $PRE_LINK && /usr/lib64/ccache/cc -fPIC $LANGUAGE_COMPILE_FLAGS $ARCH_FLAGS $LINK_FLAGS -shared $SONAME_FLAG$SONAME -o $TARGET_FILE $in $LINK_PATH $LINK_LIBRARIES && $POST_BUILD

```

I am not sure if this is a bug or a mistake on my part.  
I appreciate any help with this!
