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

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

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?

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.

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.

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.