antlr4
antlr4 copied to clipboard
[c++ runtime] Can't link with lld
I'm attemping to link the cpp runtime project with the lld linker. I followed compiling on linux and it works fine with the regular linker GNU ld (GNU Binutils for Ubuntu) 2.30
but when I attempt ld LLD 8.0.0 (compatible with GNU linkers)
it fails with the following
[100%] Linking CXX shared library ../../dist/libantlr4-runtime.so ld: error: can't create dynamic relocation R_X86_64_DTPOFF32 against symbol: guard variable for std::__cxx11::basic_string<char, std::char_traits
, std::allocator > antlrcpp::utf32_to_utf8<std::__cxx11::basic_string<char32_t, std::char_traits<char32_t>, std::allocator<char32_t> > >(std::__cxx11::basic_string<char32_t, std::char_traits<char32_t>, std::allocator<char32_t> > const&)::converter[abi:cxx11] in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output >>> defined in CMakeFiles/antlr4_shared.dir/src/ANTLRInputStream.cpp.o >>> referenced by ANTLRInputStream.cpp >>> CMakeFiles/antlr4_shared.dir/src/ANTLRInputStream.cpp.o:(antlr4::ANTLRInputStream::toStringabi:cxx11 const)
If I use the following cmake switch to attempt to resolve this
cmake .. -DCMAKE_SHARED_LINKER_FLAGS="-Wl,-z,notext"
I get this
ld: error: relocation R_X86_64_DTPOFF32 cannot be used against symbol guard variable for std::__cxx11::basic_string<char, std::char_traits
, std::allocator > antlrcpp::utf32_to_utf8<std::__cxx11::basic_string<char32_t, std::char_traits<char32_t>, std::allocator<char32_t> > >(std::__cxx11::basic_string<char32_t, std::char_traits<char32_t>, std::allocator<char32_t> > const&)::converter[abi:cxx11]; recompile with -fPIC >>> defined in CMakeFiles/antlr4_shared.dir/src/ANTLRInputStream.cpp.o >>> referenced by ANTLRInputStream.cpp >>> CMakeFiles/antlr4_shared.dir/src/ANTLRInputStream.cpp.o:(antlr4::ANTLRInputStream::toStringabi:cxx11 const)
which is very strange because cmake is supposed to be turning on -fPIC automatically when building a shared library.
But I tried to force it on anyways as follows
cmake .. -DCMAKE_CXX_FLAGS="-fPIC" -DCMAKE_SHARED_LINKER_FLAGS="-Wl,-z,notext"
and
cmake .. -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_SHARED_LINKER_FLAGS="-Wl,-z,notext"
and in both cases the output was identical to the above
Any thoughts?
I compile ANTLR normally, but I link against the static library, and got a similar error:
/usr/bin/ld: antlr-runtime-4.8/runtime/libantlr4-runtime.a(ANTLRInputStream.cpp.o): relocation R_X86_64_TPOFF32 against symbol
By adding set_property(TARGET antlr4_static PROPERTY POSITION_INDEPENDENT_CODE ON)
(adding -fPIC to build the static library), I managed to link properly.
I compile ANTLR normally, but I link against the static library, and got a similar error:
/usr/bin/ld: antlr-runtime-4.8/runtime/libantlr4-runtime.a(ANTLRInputStream.cpp.o): relocation R_X86_64_TPOFF32 against symbol
By addingset_property(TARGET antlr4_static PROPERTY POSITION_INDEPENDENT_CODE ON)
(adding -fPIC to build the static library), I managed to link properly.
Hey, did you put set_property(TARGET antlr4_static PROPERTY POSITION_INDEPENDENT_CODE ON)
in ExternalAntlr4Cpp.cmake?
I do not remember and I have not kept a copy of that project, sorry.
It is probably set wherever add_library(antlr4_static ...)
is called.
But it could also be where you call target_link_libraries(... antlr4_static ...)
.
I do not remember and I have not kept a copy of that project, sorry. It is probably set wherever
add_library(antlr4_static ...)
is called. But it could also be where you calltarget_link_libraries(... antlr4_static ...)
.
Thanks, I was able to use the cmake cache variable:
Hope this can help other people!
I added the line "set_property(TARGET antlr4_static PROPERTY POSITION_INDEPENDENT_CODE ON)" @victorpaleologue mentioned in runtime/CMakeLists.txt
like
add_library(antlr4_static STATIC ${libantlrcpp_SRC})
set_property(TARGET antlr4_static PROPERTY POSITION_INDEPENDENT_CODE ON)
It works as well.
Should be part of the documentation guys!