c++ - Does anybody know how to link against `ld` on `WSL`?
问题描述
I'm compiling a test program on clion using windows subsystem for linux WSL
. I am using dlopen
etc. to load a library at runtime. The error I am getting is:
CMakeFiles/test.dir/test_add.cpp.o: In function `test_test_add_windows_Test::TestBody()':
/mnt/d/ACrossPlatformCppLibrary/test/test_add.cpp:26: undefined reference to `dlopen'
/mnt/d/ACrossPlatformCppLibrary/test/test_add.cpp:28: undefined reference to `dlsym'
Reports seem to suggest that I need to link against ld
. However, adding
target_link_libraries(ACrossPlatformCppLibrary "${CMAKE_DL_LIBS}")
Doesn't work, which is no suprise because it looks like "${CMAKE_DL_LIBS}"
is empty on windows. So I tried to manually give the path to ld
target_link_libraries(ACrossPlatformCppLibrary "/usr/bin/ld")
Does anybody know how to link against ld
on WSL
?
update
Thanks to comments and the answer by @squareskittles, I've noticed that the dl
binary is not available on the system path. On closer inspection, it doesn't look like the dl
binary exists at all in WSL
. Here is a cut and paste of the directory I think dl
should be in:
$ cd /lib
$ ls
apparmor hdparm libhandle.so.1.0.3 netplan udev
console-setup init lsb open-iscsi ufw
cpp klibc-wBFLvVtxy4xJqEadIBJMa78iJz8.so modprobe.d recovery-mode x86_64-linux-gnu
cryptsetup **ld-linux.so.2** modules systemd
ebtables libhandle.so.1 modules-load.d terminfo
解决方案
I think you are confusing the GNU link library flag -l
with the library name dl
, which together in the link stage appear as -ldl
. If the library is in the system path (of WSL), you should not need the full path to the library either. You can simply put the library name dl
in this command:
target_link_libraries(ACrossPlatformCppLibrary PRIVATE dl)
For what it's worth, the libdl.so
library on WSL should be located here if you have the proper packages installed:
/lib/x86_64-linux-gnu/libdl.so.2
推荐阅读
- javascript - 将href添加到span标签
- r - R数据立方体定义层次结构
- ajax - 如何使用 ActiveForm 和 Pjax 删除 Yii2 重定向?
- sql - 如何在 MS SQL 中编写动态日期周期板/分组查询?
- mysql - MySQL函数-保存多次往返
- opencv - 使用 OpenCV Python 和 Hough 变换进行线检测
- assembly - 组装:增加 2(或更大的数字)而不破坏 ADC 循环中的 CF?
- javascript - Javascript - Calling a variable in a literal string with a passed variable name
- python - Python,使用 .dat 文件中的数据
- linux - docker:在不更改 CMD 的情况下在 $PATH 解决方案中找不到可执行文件