问题
I'm getting undefined reference to dlsym@@GLIBC_2.2.5
even after linking it before and after the libraries. However in the linking output it appears that it's linking just before, but linking before all libraries should work, I guess.
/bin/g++-9 CMakeFiles/http_downloader.dir/http_downloader_cli.cpp.o CMakeFiles/http_downloader.dir/SimpleOpenVPNSocket.cpp.o -o http_downloader -lpthread /usr/lib/x86_64-linux-gnu/libssl.so /usr/lib/x86_64-linux-gnu/liblz4.so -ldl downloader/libhttp_downloader_cli.a downloader/libhttp_downloader_lib.a ../../libopenvpn/libopenvpn_lib.a ../../_smoltcp_cpp_interface/libsmoltcp_cpp.a ../../_libtins/lib/libtins.a -ldl /usr/lib/x86_64-linux-gnu/libcrypto.so -lpthread /usr/lib/x86_64-linux-gnu/liblz4.so /usr/lib/x86_64-linux-gnu/libssl.so /usr/lib/x86_64-linux-gnu/libcrypto.so ../../../../smoltcp_cpp_interface/target/debug/libsmoltcp_cpp_interface_rust.a /usr/lib/x86_64-linux-gnu/libssl.so /usr/lib/x86_64-linux-gnu/libcrypto.so
/usr/bin/ld: ../../../../smoltcp_cpp_interface/target/debug/libsmoltcp_cpp_interface_rust.a(std-6640d3868fa846e8.std.1mk5kra7-cgu.0.rcgu.o): undefined reference to symbol 'dlsym@@GLIBC_2.2.5'
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libdl.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [src/examples/http_downloader/CMakeFiles/http_downloader.dir/build.make:113: src/examples/http_downloader/http_downloader] Error 1
make[2]: Leaving directory '/workspaces/libopenvpnclient/build'
make[1]: *** [CMakeFiles/Makefile2:519: src/examples/http_downloader/CMakeFiles/http_downloader.dir/all] Error 2
make[1]: Leaving directory '/workspaces/libopenvpnclient/build'
make: *** [Makefile:130: all] Error 2
This is how I'm linking it in CMake:
add_executable(http_downloader http_downloader_cli.cpp SimpleOpenVPNSocket.cpp)
add_core_dependencies(http_downloader)
target_include_directories(http_downloader PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/downloader/include)
add_dependencies(http_downloader http_downloader_cli http_downloader_lib openvpn_lib smoltcp_cpp tins)
set_property(TARGET http_downloader PROPERTY CXX_STANDARD 17)
target_link_libraries(http_downloader dl http_downloader_cli http_downloader_lib openvpn_lib smoltcp_cpp tins dl)
As you can see I've put dl
before and after everything just to make sure.
If I understand correctly, .a
libs do not have dependencies, they only have missing symbols and we have to fill them. Seems like I'm doing that by linking with -ldl
回答1:
Would you inspect carefully the actual command line used for linking, you will find that after libsmoltcp_cpp_interface_rust.a
, which has missed symbol, there is no -ldl
parameter.
It seems that in your case smoltcp_cpp
is an IMPORTED library target, which has libsmoltcp_cpp_interface_rust.a
as a link dependency (not as IMPORTED_LOCATION
property or so).
While CMake preserves order of the libraries, linked into a single binary (an or another library), order between dependencies of these libraries is not defined.
You need to add -ldl
as a link dependency for the smoltcp_cpp
target itself:
target_link_libraries(smoltcp_cpp INTERFACE -ldl)
However, this will work only if libsmoltcp_cpp_interface_rust.a
is a direct link dependency for smoltcp_cpp
target, that is specified as
target_link_libraries(smoltcp_cpp INTERFACE libsmoltcp_cpp_interface_rust.a)
In case of indirect dependencies, like
target_link_libraries(smoltcp_cpp INTERFACE <intermediate-target>)
target_link_libraries(<intermediate-target> INTERFACE libsmoltcp_cpp_interface_rust.a)
you need to add -ldl
as a dependency for that <intermediate-target>
.
Ideally, every IMPORTED target should be self-contained, so you may safely link with that target without knowing its internals.
来源:https://stackoverflow.com/questions/63153342/cmake-undefined-reference-to-symbol-dlsymglibc-2-2-5-even-though-i-link-with