简体   繁体   English

Linux上的共享库和-fPIC错误

[英]Shared library on Linux and -fPIC error

I am trying to compile a shared library in Linux using a Makefile created with Cmake, but running make I obtain the following error: 我正在尝试使用使用Cmake创建的Makefile在Linux中编译共享库,但是运行make我获得以下错误:

   Linking CXX shared library libcpp-lib.so
   /usr/bin/ld: /home/davide/Desktop/boost_1_55_0/stage/lib/libboost_system.a(error_code.o): relocation R_X86_64_32 against .rodata.str1.1 can  not be used when making a shared object; recompile with -fPIC
   /home/davide/Desktop/boost_1_55_0/stage/lib/libboost_system.a: could not read symbols:   Bad value
   collect2: ld returned 1 exit status
   make[2]: *** [libcpp-lib.so] Error 1
   make[1]: *** [CMakeFiles/cpp-lib.dir/all] Error 2
   make: *** [all] Error 2

I provide the following command in the CMakeLists.txt in order to say that I want a shared (.so) library: 我在CMakeLists.txt中提供以下命令,以便说我想要一个共享(.so)库:

add_library(cpp-lib SHARED ${CPP_FILES})

What else do I need to specify in order to avoid the -fPIC error shown above? 为了避免上面显示的-fPIC错误,还需要指定什么?

Thanks a lot in advance 非常感谢提前

The boost libraries needs to be compiled using -fPIC: Please have a look at: How to compile static library with -fPIC from boost.python 需要使用-fPIC编译boost库:请看一下: 如何使用boost.python中的-fPIC编译静态库

Try to add compiler flags by cmake by in your project: 尝试在项目中通过cmake by添加编译器标志:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM