首页 > 解决方案 > Getting error "can not be used when making a shared object; recompile with -fPIC" although fpic is used

问题描述

I am currently building a shared library (lib1.so) out of a cmake environment. lib1.so depends on an external static lib libLASlib.a (which I am able to recompile if necessary). Everything works wonder on windows so far, but it's another story when switching to linux:

/usr/bin/ld: lib/LASlib/libLASlib.a(lasreader.cpp.o): relocation R_X86_64_PC32 against symbol `_ZN9LASreader35read_point_filtered_and_transformedEv' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Bad value

So I tried recompiling the libLASlib with -fPIC -> same error

Due to my environment I could not verify the fpic was effectivelly added to the gcc command line.

Here is what I tried to confirm there was no issue with the fPIC:

readelf --dynamic libLASlib.a | grep lasreader.cpp.o -A2
File: libLASlib.a(lasreader.cpp.o)

There is no dynamic section in this file.

For the record not a single cpp.o was found with a dynamic section

I have tried just to see what it would give if i changed liblas from a static to a shared library -> no error

Any thoughs? Many thanks!

标签: c++linuxgcclibraries

解决方案


确实没有应用 fPIC

柯南似乎没有转发 fPIC 选项

我编辑了 CMAKELIST 并添加了

set_property(TARGET LASlib PROPERTY POSITION_INDEPENDENT_CODE ON)

它最终通过了


推荐阅读