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.
相关文章