我正试图通过cmake将afinit_state.a库与我的C++项目链接起来。但我得到了附加错误。我的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发布于 2021-03-11 14:16:23
来自文档:https://cmake.org/cmake/help/latest/policy/CMP0083.html#policy:CMP0083
“CMP0083
新版本3.14。
若要控制位置独立可执行(PIE)的生成,需要在链接时间使用一些标志。
CMake 3.13和更低版本在设置POSITION_INDEPENDENT_CODE时没有添加这些链接标志。
此策略的旧行为是不管理饼链接标志。如果设置了POSITION_INDEPENDENT_CODE,则新的行为是添加链接标志:“
尝试使用更新版本的CMake
https://stackoverflow.com/questions/66581608
复制相似问题