qtcmakeqt5fpic

CMake fails to add fPIC compile option


I am having some issues trying to link an executable which transitively uses Qt5. I have configured my project using CMake with the option CMAKE_POSITION_INDEPENDENT_CODE = ON but still, when compiling the executable, Qt5 complains that I should use 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. "\

I have these two options enabled in my main CMakeLists.txt file

set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(BUILD_SHARED_LIBS ON)

How should I correctly add the fPIC option for my final executable?


Solution

  • Your error states:

    "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)."

    However, your compilation flags only contain fPIE, not 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

    You were correct to try using 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.

    Thus, you will have to add the -fPIC flag for your executable somewhat manually, with something like this:

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