CMake无法添加fPIC编译选项

snz8szmq  于 2023-06-06  发布在  其他
关注(0)|答案(2)|浏览(247)

我有一些问题,试图链接一个可执行的过渡使用Qt5。我已经使用CMake配置了我的项目,选项为CMAKE_POSITION_INDEPENDENT_CODE = ON,但在编译可执行文件时,Qt5仍然抱怨我应该使用fPIC

[ 99%] Building CXX object tools/deSimRunner/CMakeFiles/deSimRunner.dir/main.cpp.o
cd /home/jjcasmar/projects/Hybrid/Desilico/prj/Release/tools/deSimRunner && /home/jjcasmar/usr/local/bin/c++  -DBOOST_ALL_DYN_LINK -DFMT_SHARED -DHAVE_CUDA -DQT_CORE_LIB -DQT_GUI_LIB -DQT_NO_DEBUG -DSPDLOG_COMPILED_LIB -DSPDLOG_FMT_EXTERNAL -D_GLIBCXX_USE_CXX11_ABI=1 -I/home/jjcasmar/projects/Hybrid/Desilico/src/deCore -I/home/jjcasmar/projects/Hybrid/Desilico/src/deCore/.. -I/home/jjcasmar/projects/Hybrid/Desilico/src/deGeom/.. -I/opt/cuda/include -I/home/jjcasmar/projects/Hybrid/Desilico/src/deSim/.. -isystem /home/jjcasmar/.conan/data/eigen/3.3.7/conan/stable/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9/include/eigen3 -isystem /home/jjcasmar/.conan/data/rapidjson/1.1.0/bincrafters/stable/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9/include -isystem /home/jjcasmar/.conan/data/boost/1.71.0/conan/stable/package/393cfc058d5be864014f06fc0bb0e29c6845d9e9/include -isystem /home/jjcasmar/.conan/data/zlib/1.2.11/conan/stable/package/1d877a3df840030e6a8abb74c5ffb9088d08b47a/include -isystem /home/jjcasmar/.conan/data/bzip2/1.0.8/conan/stable/package/a5875aed3fc7ae8dd0488f7e5e99acbc480d721d/include -isystem /usr/include/qt -isystem /usr/include/qt/QtCore -isystem /usr/lib/qt/mkspecs/linux-g++ -isystem /home/jjcasmar/.conan/data/spdlog/1.4.2/bincrafters/stable/package/f31849fd5982a882fc905666fe297f0c231b10af/include -isystem /home/jjcasmar/.conan/data/fmt/6.0.0/bincrafters/stable/package/ca4b767114c8e107f72671a3380d9917e2b9adff/include -isystem /home/jjcasmar/.conan/data/OpenMesh/7.1/desilico/stable/package/b80d46004713aa37d6a90b42e2a326a056a237b5/include -isystem /home/jjcasmar/.conan/data/nanoflann/1.3.0/desilico/stable/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9/include -isystem /home/jjcasmar/usr/local/include -isystem /home/jjcasmar/.conan/data/cereal/1.2.2/conan/stable/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9/include -isystem /home/jjcasmar/.conan/data/glm/0.9.9.5/g-truc/stable/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9/include -isystem /usr/include/qt/QtGui -isystem /home/jjcasmar/.conan/data/range-v3/0.9.1/ericniebler/stable/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9/include  -fopenmp  -O3 -DNDEBUG -fPIE   -DNON_COMMERCIAL_LICENSE -std=gnu++14 -o CMakeFiles/deSimRunner.dir/main.cpp.o -c /home/jjcasmar/projects/Hybrid/Desilico/tools/deSimRunner/main.cpp
In file included from /usr/include/qt/QtCore/qjsonvalue.h:43,
                 from /usr/include/qt/QtCore/qjsondocument.h:43,
                 from /usr/include/qt/QtCore/QJsonDocument:1,
                 from /home/jjcasmar/projects/Hybrid/Desilico/tools/deSimRunner/main.cpp:23:
/usr/include/qt/QtCore/qglobal.h:1204:4: error: #error "You must build your code with position independent code if Qt was built with -reduce-relocations. " "Compile your code with -fPIC (-fPIE is not enough)."
 #  error "You must build your code with position independent code if Qt was built with -reduce-relocations. "\

我在主CMakeLists.txt文件中启用了这两个选项

set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(BUILD_SHARED_LIBS ON)

如何正确地为最终可执行文件添加fPIC选项?

5us2dqdw

5us2dqdw1#

您的错误状态:
“如果Qt是用-reduce-relocations构建的,你必须用位置无关的代码构建你的代码。““使用-fPIC编译代码(-fPIE不够)。"
但是,您的编译标志只包含fPIE,而不是fPIC
/usr/local/bin/c++ -DBOOST_ALL_DYN_LINK ...
...
-fopenmp -O3 -DNDEBUG**-fPIE**-DNON_COMMERCIAL_LICENSE -std=gnu++14 -o CMakeFiles/deSimRunner.dir/main.cpp.o -c/home/jjcasmar/projects/Hybrid/Desilico/tools/deSimRunner/main.cpp
您尝试使用CMAKE_POSITION_INDEPENDENT_CODE是正确的,但是这个变量可能不会设置您期望的标志。这个变量的相当没有文档记载的行为是这样的:

  • 如果目标是library,则CMake会将标志-fPIC添加到编译和链接步骤中。
  • 如果目标是可执行文件,则CMake会在编译和链接步骤中添加-fPIE标志。

因此,您必须手动为可执行文件添加-fPIC标志,如下所示:

add_executable(deSimRunner ... )
target_compile_options(deSimRunner PRIVATE -fPIC)
ssgvzors

ssgvzors2#

我认为最可移植的方法是使用OBJECT库,如下所示:

add_library(deSimRunner_lib OBJECT main.cpp)
set_property(TARGET deSimRunner_lib PROPERTY POSITION_INDEPENDENT_CODE ON)
add_executable(deSimRunner)
target_link_libraries(deSimRunner deSimRunner_lib)

相关问题