尽管启用了 POSITION_INDEPENDENT_CODE 标志,但使用 -fPIC 重新编译时出错

问题描述

我正在尝试通过 cmake 将 afinit_state.a 库与我的 C++ 项目链接起来。但我收到了 bolow 附加错误。我的 cmake 文件是:

cmake_minimum_required(VERSION 3.12)

project(java_telemetry_wrapper)

set(CMAKE_CXX_STANDARD 14)

find_package(JNI)
set(JAVA_INCLUDE_PATH "$ENV{JAVA_HOME}/include")
find_path(JAVA_INCLUDE_PATH jni.h ${JAVA_AWT_INCLUDE_DIRECTORIES})

set(JNI_LIBRARIES ${JAVA_AWT_LIBRARY} ${JAVA_JVM_LIBRARY})
set(JNI_INCLUDE_DIRS ${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2} ${JAVA_AWT_INCLUDE_PATH})
set(STATE_PATH "/home/poc/src/state/")
set(LOGGER_PATH "../../logger")

include_directories(${JNI_INCLUDE_DIRS} ${STATE_PATH} ${LOGGER_PATH} ${AFN_STATE_INC} ${AFN_LOGGER_INC})
link_directories(/opt/lib/)

add_library(${PROJECT_NAME} SHARED header/TelemetryWrapper.h src/TelemetryWrapper.cpp ../telemetry.h ../telemetry.cpp ../common.hpp)
set_property(TARGET ${PROJECT_NAME} PROPERTY POSITION_INDEPENDENT_CODE ON)
target_link_libraries(${PROJECT_NAME} "-lpthread" afinit_state "-lz")

在发布这个问题之前,我已经尝试了我可以在互联网上找到的选项。很多人都说使用 set_property(TARGET ${PROJECT_NAME} PROPERTY POSITION_INDEPENDENT_CODE ON),我确实这样做了,但错误仍然存​​在。请帮助我缩小问题的范围,是什么导致了这个问题。

/opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: /opt//lib/libafinit_state.a(af_state_service.c.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
    /opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: /opt//lib/libafinit_state.a(StateSvcMsgQ.cc.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
    /opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: /opt//lib/libafinit_state.a(StateSvcShmCtl2.cc.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
    /opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: /opt//lib/libafinit_state.a(StateSvcShmCtl.cc.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
    /opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: /opt//lib/libafinit_state.a(af_ss_agent_store.c.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
    /opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: /opt//lib/libafinit_state.a(af_ss_call_store.c.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
    /opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: /opt//lib/libafinit_state.a(af_ss_hashed_tbl.c.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
    /opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: /opt//lib/libafinit_state.a(af_ss_mega_store.c.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
    /opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: /opt//lib/libafinit_state.a(StateSvcMQ_Msgs.cc.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
    /opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: /opt//lib/libafinit_state.a(StateSvcObjDefs.cc.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
    /opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: final link failed: Nonrepresentable section on output
    collect2: error: ld returned 1 exit status
    make[2]: *** [CMakeFiles/java_telemetry_wrapper.dir/build.make:99: libjava_telemetry_wrapper.so] Error 1
    make[1]: *** [CMakeFiles/Makefile2:73: CMakeFiles/java_telemetry_wrapper.dir/all] Error 2
    make: *** [Makefile:84: all] Error 2

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)