问题描述
我不断得到
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 .soThe command I'm using is.
clang++ Main.cpp -o foo -L./ -lchaiscript_stdlib-5.3.1.so
我在做什么错了?
libchaiscript_stdlib-5.3.1.so与Main.cpp位于同一目录中,我认为-L./会将.so添加到库搜索路径中.
libchaiscript_stdlib-5.3.1.so is in the same directory as Main.cpp I thought the -L./ would add the .so to the library seach 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 linkers 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.
这篇关于用SO进行Clang链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!