CMake 无法添加 fPIC 编译选项
CMake fails to add fPIC compile option
我在尝试 link 传递使用 Qt5 的可执行文件时遇到了一些问题。
我已经使用带有 CMAKE_POSITION_INDEPENDENT_CODE = ON
选项的 CMake 配置了我的项目,但是在编译可执行文件时,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
选项?
您的错误状态:
"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)."
但是,您的编译标志仅包含 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
, but this variable may not set the flags you expect. The fairly undocumented behavior for this variable is this 是正确的:
- If the target is a library, the flag
-fPIC
is added by CMake to the compilation and linker steps.
- If the target is an executable, the flag
-fPIE
is added by CMake to the compilation and linker steps.
因此,您必须手动为您的 可执行文件 添加 -fPIC
标志,如下所示:
add_executable(deSimRunner ... )
target_compile_options(deSimRunner PRIVATE -fPIC)
我在尝试 link 传递使用 Qt5 的可执行文件时遇到了一些问题。
我已经使用带有 CMAKE_POSITION_INDEPENDENT_CODE = ON
选项的 CMake 配置了我的项目,但是在编译可执行文件时,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
选项?
您的错误状态:
"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)."
但是,您的编译标志仅包含 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
, but this variable may not set the flags you expect. The fairly undocumented behavior for this variable is this 是正确的:
- If the target is a library, the flag
-fPIC
is added by CMake to the compilation and linker steps.- If the target is an executable, the flag
-fPIE
is added by CMake to the compilation and linker steps.
因此,您必须手动为您的 可执行文件 添加 -fPIC
标志,如下所示:
add_executable(deSimRunner ... )
target_compile_options(deSimRunner PRIVATE -fPIC)