"relocation R_X86_64_32S against symbol [...] can not be used when making a PIE object; recompile with -fPIC" with static library
With GCC 4.8 and the following cmake file for including fmt on Ubuntu 18.04.5 LTS:
option(CPP-LAZY_USE_INSTALLED_FMT "Import {fmt} using find_package" NO)
if(CPP-LAZY_USE_INSTALLED_FMT)
find_package(fmt 7 REQUIRED CONFIG)
else()
set(FMT_INSTALL YES CACHE INTERNAL "" FORCE)
set(FMT_OS YES CACHE INTERNAL "")
include(FetchContent)
FetchContent_Declare(fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG a581e9e5d83673fefbecb7cc6ea2514af83f77c8
UPDATE_DISCONNECTED YES)
FetchContent_MakeAvailable(fmt)
endif()
target_link_libraries(cpp-lazy INTERFACE fmt::fmt)
With cmake command:
cmake -S . -B build/install -D CMAKE_CXX_STANDARD:STRING=11 -D CMAKE_BUILD_TYPE:STRING=Release -D CMAKE_INSTALL_PREFIX=build/prefix
Gives (as shown here):
/usr/bin/ld: /home/runner/work/cpp-lazy/cpp-lazy/build/prefix/lib/libfmt.a(format.cc.o): relocation R_X86_64_32S against symbol `_ZTVN3fmt2v712format_errorE' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
make[2]: *** [LazyTests] Error 1
make[1]: *** [CMakeFiles/LazyTests.dir/all] Error 2
CMakeFiles/LazyTests.dir/build.make:356: recipe for target 'LazyTests' failed
make: *** [all] Error 2
CMakeFiles/Makefile2:92: recipe for target 'CMakeFiles/LazyTests.dir/all' failed
Makefile:111: recipe for target 'all' failed
We are not building a shared library as you can see by the libfmt.a extension. Even if we would insert the -fPIC using
-D "CMAKE_PROJECT_FMT_INCLUDE_BEFORE=$(pwd)/cmake/fmtPICFix.cmake"
That gives us the following linker errors (as shown here):
CMakeFiles/LazyTests.dir/function-tools-tests.cpp.o: In function `____C_A_T_C_H____T_E_S_T____0()':
function-tools-tests.cpp:(.text+0x1afd): undefined reference to `fmt::v7::detail::vformat[abi:cxx11](fmt::v7::basic_string_view<char>, fmt::v7::format_args)'
function-tools-tests.cpp:(.text+0x5b15): undefined reference to `fmt::v7::detail::vformat[abi:cxx11](fmt::v7::basic_string_view<char>, fmt::v7::format_args)'
function-tools-tests.cpp:(.text+0x5e03): undefined reference to `fmt::v7::detail::vformat[abi:cxx11](fmt::v7::basic_string_view<char>, fmt::v7::format_args)'
function-tools-tests.cpp:(.text+0x6039): undefined reference to `fmt::v7::detail::vformat[abi:cxx11](fmt::v7::basic_string_view<char>, fmt::v7::format_args)'
function-tools-tests.cpp:(.text+0x6262): undefined reference to `fmt::v7::detail::vformat[abi:cxx11](fmt::v7::basic_string_view<char>, fmt::v7::format_args)'
CMakeFiles/LazyTests.dir/join-tests.cpp.o:join-tests.cpp:(.text+0xb132): more undefined references to `fmt::v7::detail::vformat[abi:cxx11](fmt::v7::basic_string_view<char>, fmt::v7::format_args)' follow
Both fmt/ostream.h and fmt/format.h are included (but I doubt that's the root cause). However, this only occurs in GCC 4.8. GCC 5.0 has no problems whatsoever. On my own local Windows 10 PC using MinGW GCC 4.8, this problem does not persist. I'm completely clueless where to look, or what to change. Any pointers would be great.
This doesn't look like a {fmt} bug since the same config works with newer version of gcc. There are some suggestions here: https://stackoverflow.com/q/19768267/471164. You can workaround the problem by creating an {fmt} target containing https://github.com/fmtlib/fmt/blob/master/src/format.cc in your CMake config and ensuring that compiler and linker flags are fully consistent.