Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

ERROR: relocation R_X86_64_PC32 against symbol can not be used when making a shared object; recompile with -fPIC



  • I'm developing a Qt C++ shared library that uses my custom Fortran static library called: "liblobular-bearing.a"

    Everything was compiling and working good.
    Just to test my compile toolchain in a different system, I pushed both codes to a Gitlab online repo and downloaded it on my iMac.

    The C++ and Fortran projects are CMake projects, so I did just a little ajust JUST ON CMAKE if(APPLE) section in the CMakeLists.txt dependencies path and I got a working build on OSX too. Compile and works good on OSX.
    So, I did a newer commit to my gitlab repo with theses little CMakeLists.txt ajusts.

    Here is the problem:
    Now, I went back to my Linux machine and received the last commit did on OSX using git pull. Ok!
    I compiled the Fortran static library using CMake...it compiles ok and I got the "liblobular-bearing.a" file.

    When I tried to link it on my Qt C++ shared library project, I got this new error:

    error: /home/nyck/Desktop/FEM/lobular-bearing-plugin/fortran/lib/build/liblobular-bearing.a(lobular-bearing.f90.o): relocation R_X86_64_PC32 against symbol `__mod_lobularbearing_MOD_ms' can not be used when making a shared object; recompile with -fPIC
    

    Neither code has been modified (Fortran and C++).

    I already tried to clone this project again in another folder, but I got the same error above.

    Everything was working and I don't understand why just moving my project files to OSX and get back to Linux is given me linking errors.

    Any ideas?

    My Systems are:

    • Ubuntu 20.04 x64
    • Qt Creator 4.13.2
    • Qt 5.15.1
    • CMake 3.16.3
    • GCC/GFortran 9.3

    My iMac is:

    • OSX Catalina 10.15 x64
    • Qt Creator 4.11.0
    • CMake 3.14.0
    • CLang / GFortran

    Here is my CMakeLists.txt of my Qt C++ shared library:

    cmake_minimum_required(VERSION 3.0)
    
    project("Lobular-Bearing" LANGUAGES CXX)
    
    # Add all plugin source files
    file(GLOB plugin_src
        "./mylib.cpp"
        "./dialog.cpp"
        "./plugin.cpp"
        "./py_plugin.cpp"
        "./plugin_qt_api.cpp"
        "./custom_widget/table.cpp"
        "./custom_widget/value_unit.cpp"
        "./fortran_interface.cpp"
    )
    # ========== Generate shared library ========== #
    set(CMAKE_POSITION_INDEPENDENT_CODE ON) # -fPIC
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    set(CMAKE_AUTOUIC ON)
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTORCC ON)
    set(CMAKE_CXX_STANDARD 11)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    find_package(Qt5 COMPONENTS Core Widgets REQUIRED)
    
    set(TS_FILES plugin_pt_BR.ts)
    
    qt5_wrap_ui(plugin_src dialog.ui custom_widget/value_unit.ui)
    
    include_directories(${Qt5Widgets_INCLUDE_DIRS})
    add_definitions(-DUSING_QT)
    add_definitions(-DPLUGIN_LIBRARY)
    message("=====> GENERATING SHARED LIBRARY <=====")
    add_library(${CMAKE_PROJECT_NAME} SHARED ${plugin_src})
    #add_library(${CMAKE_PROJECT_NAME} SHARED ${plugin_src} ${TS_FILES})
    target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC include)
    
    # ========== OS / COMPILER SPECIFIC ========== #
    if(MSVC)
        message(FATAL_ERROR "This project is not compatible with Microsoft MSVC compiler. To build this project in Windows environment you MUST use an MingW compiler")
    endif()
    
    if(MSYS OR MINGW)
        TARGET_LINK_DIRECTORIES(${CMAKE_PROJECT_NAME} PUBLIC "C:/Users/VM/Downloads/h5fortran/build")
        TARGET_LINK_DIRECTORIES(${CMAKE_PROJECT_NAME} PUBLIC "C:/Users/VM/Downloads/json-fortran/build/lib")
        TARGET_LINK_DIRECTORIES(${CMAKE_PROJECT_NAME} PUBLIC "C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/10.2.0")
        TARGET_LINK_DIRECTORIES(${CMAKE_PROJECT_NAME} PUBLIC "C:/Users/VM/Desktop/lobular-bearing-plugin/fortran/lib/build")
    endif()
    
    if(APPLE)
        INCLUDE_DIRECTORIES("~/Desktop/json-fortran/build/include")
        TARGET_LINK_DIRECTORIES(${CMAKE_PROJECT_NAME} PUBLIC "~/Desktop/json-fortran/build/lib")
        TARGET_LINK_DIRECTORIES(${CMAKE_PROJECT_NAME} PUBLIC "~/Desktop/h5fortran/build")
        TARGET_LINK_DIRECTORIES(${CMAKE_PROJECT_NAME} PUBLIC "/usr/local/Cellar/gcc/9.3.0/lib/gcc/9")
        TARGET_LINK_DIRECTORIES(${CMAKE_PROJECT_NAME} PUBLIC "~/Desktop/FEM/lobular-bearing-plugin/fortran/lib/build")
    endif()
    
    if(UNIX AND NOT APPLE)
        INCLUDE_DIRECTORIES("~/Desktop/json-fortran/include")
        TARGET_LINK_DIRECTORIES(${CMAKE_PROJECT_NAME} PUBLIC "~/Desktop/json-fortran/lib")
        TARGET_LINK_DIRECTORIES(${CMAKE_PROJECT_NAME} PUBLIC "~/Desktop/h5fortran/build")
        TARGET_LINK_DIRECTORIES(${CMAKE_PROJECT_NAME} PUBLIC "~/Desktop/FEM/lobular-bearing-plugin/fortran/lib/build")
    endif()
    
    # ========== LINK LIBRARIES ========== #
    if(MSYS OR MINGW)
        TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME} PUBLIC -llobular-bearing -lh5fortran -ljsonfortran -lgfortran -lquadmath Qt5::Core Qt5::Gui Qt5::Widgets)
    elseif(APPLE)
        TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME} PUBLIC -llobular-bearing -lh5fortran -ljsonfortran -lgfortran Qt5::Core Qt5::Gui Qt5::Widgets)
    elseif(UNIX AND NOT APPLE)
        TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME} PUBLIC -llobular-bearing -ljsonfortran -lgfortran Qt5::Core Qt5::Gui Qt5::Widgets)
    endif()
    


  • I found the solution...

    Only on Linux, the static library that will be linked to my shared library MUST be recompiled using -fPIC flag.

    I don't understand why...but only works in this way....



  • I found the solution...

    Only on Linux, the static library that will be linked to my shared library MUST be recompiled using -fPIC flag.

    I don't understand why...but only works in this way....


Log in to reply