# How to deal with ld error: 'can not be used when making a PIE object; recompile with -fPIC'?

**URL:** https://discourse.cmake.org/t/how-to-deal-with-ld-error-can-not-be-used-when-making-a-pie-object-recompile-with-fpic/2509
**Category:** Usage
**Created:** [January 11, 2021, 3:49pm UTC](https://discourse.cmake.org/t/how-to-deal-with-ld-error-can-not-be-used-when-making-a-pie-object-recompile-with-fpic/2509 "2021-01-11T15:49:53Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![DavidA](https://discourse.cmake.org/user_avatar/discourse.cmake.org/davida/32/128_2.png) [@DavidA](https://discourse.cmake.org/u/DavidA)
#### Post date: [January 11, 2021, 3:49pm UTC](https://discourse.cmake.org/t/how-to-deal-with-ld-error-can-not-be-used-when-making-a-pie-object-recompile-with-fpic/2509/1 "2021-01-11T15:49:53Z")

</div>

Hi,

I have built the 3rd party Accellera SystemC library using CMake on Centos 7 with devtoolset-7. I can successfully link my C++ application to that library, on the same platform.

I also need to link my application to the same library, on Ubuntu 18.04. But when I try to do so I get error:

```
/usr/bin/ld: ,snip>/SystemC/rel-2.3.3/x64-linux/_release/libsystemc.a(instance_specific_extensions.cpp.o): relocation R_X86_64_32 against symbol `_ZGVZN9tlm_utils42instance_specific_extension_container_pool8instanceEvE4inst' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status

```

devtoolset-7 has gcc 7.3.1, ld v.2.28  
Ubuntu 18.04 has gcc v.7.4.0, ld v.2.30

I think the following CMake article is relevant:

[CMP0083 — CMake 3.14.7 Documentation](https://cmake.org/cmake/help/v3.14/policy/CMP0083.html)

but I am not sure what I need to do. Do I need to add:

set\_property(TARGET foo PROPERTY POSITION\_INDEPENDENT\_CODE TRUE)

to the build of the SystemC library, or should I change the build of my application?

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [January 11, 2021, 4:02pm UTC](https://discourse.cmake.org/t/how-to-deal-with-ld-error-can-not-be-used-when-making-a-pie-object-recompile-with-fpic/2509/2 "2021-01-11T16:02:26Z")

</div>

If you didn’t do any settings regarding PIC in `CMake`, it seems you have incompatible default settings of the compilers between `Centos` and `Ubuntu`.  
From the error on `Ubuntu`, this platform use, by default, `PIC` and `PIE` flags.

The simple solution is effectively to compile your library `SystemC` in `PIC`. For that purpose, setting the property `POSITION_INDEPENDENT_CODE` is the right action.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [January 12, 2021, 2:22am UTC](https://discourse.cmake.org/t/how-to-deal-with-ld-error-can-not-be-used-when-making-a-pie-object-recompile-with-fpic/2509/3 "2021-01-12T02:22:54Z")

</div>

Since the `systemc` library is a static library, its default for `POSITION_INDEPENDENT_CODE` is false. You need to set it to true and rebuild the systemc library. That should fix the error you’re seeing. You may find the `CMAKE_POSITION_INDEPENDENT_CODE` variable to be useful if you need to set this across a number of targets rather than just this one case.

---

<div class="post-metadata">

### Author: ![DavidA](https://discourse.cmake.org/user_avatar/discourse.cmake.org/davida/32/128_2.png) [@DavidA](https://discourse.cmake.org/u/DavidA)
#### Post date: [January 12, 2021, 4:04pm UTC](https://discourse.cmake.org/t/how-to-deal-with-ld-error-can-not-be-used-when-making-a-pie-object-recompile-with-fpic/2509/4 "2021-01-12T16:04:02Z")

</div>

Thank you both for your answers. POSITION\_INDEPENDENT\_CODE=True did help but I then faced another problem of abi incompatibility. I’ve fixed the problem by compiling the library separately for each platform.
