Clang linking with a .so file(Clang 与 .so 文件的链接)
问题描述
我不断得到
ld: library not found for -lchaiscript_stdlib-5.3.1.so
clang: error: linker command failed with exit code 1 (use -v to see invocation)
尝试链接到 .so 文件时.
When trying to link to a .so file.
我正在使用这个命令:
clang++ Main.cpp -o foo -L./ -lchaiscript_stdlib-5.3.1.so
我做错了什么?
文件 libchaiscript_stdlib-5.3.1.so 与文件 Main.cpp 位于同一目录中.我认为 -L./
会将 .so 添加到库搜索路径中.
File libchaiscript_stdlib-5.3.1.so is in the same directory as file Main.cpp. I thought the -L./
would add the .so to the library search paths.
推荐答案
是的,-L
选项添加了搜索路径,但是链接器添加了.so
(或 .a
) 后缀本身(就像它添加 lib
前缀一样).所以你只需要有 -lchaiscript_stdlib-5.3.1
链接器就会找到它.
Yes, the -L
option adds the search path, but the linker adds the .so
(or .a
) suffix itself (just like it adds the lib
prefix). So you only need to have -lchaiscript_stdlib-5.3.1
and the linker will find it.
你也可以跳过添加路径,直接与文件链接:
You can also skip the adding of the path, and link directly with the file:
clang++ Main.cpp -o foo libchaiscript_stdlib-5.3.1.so
请注意,如果库不在运行时链接器的路径中,则运行时链接器(当您运行程序时实际加载共享库)可能无法找到该库.不过,您可以告诉(编译时)链接器在生成的程序中添加到共享库路径的路径:
Note that the runtime linker (which is what actually loads the shared libraries when you run your program) might not be able to find the library if it's not in the runtime linker's path. You can tell the (compile time) linker to add a path to the shared-library path in the generated program though:
clang++ Main.cpp -o foo libchaiscript_stdlib-5.3.1.so -Wl,-rpath,/absolute/path
-Wl
选项告诉编译器前端将一个选项传递给链接器,而链接器选项 -rpath
将路径添加到运行时链接器搜索路径.
The -Wl
option tells the compiler front-end to pass an option to the linker, and the linker option -rpath
adds a path to the runtime-linker search path.
这篇关于Clang 与 .so 文件的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Clang 与 .so 文件的链接


基础教程推荐
- CString 到 char* 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 初始化列表*参数*评估顺序 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16