C++ 与 SO 的 Clang 链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25160245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clang Linking with SO
提问by Some programmer dude
I keep getting
我不断得到
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)
When trying to link to a .so The command I'm using is.
尝试链接到 .so 时,我使用的命令是。
clang++ Main.cpp -o foo -L./ -lchaiscript_stdlib-5.3.1.so
What am I doing wrong?
我究竟做错了什么?
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.
libchaiscript_stdlib-5.3.1.so 与 Main.cpp 位于同一目录中,我认为 -L./ 会将 .so 添加到库搜索路径中。
回答by Some programmer dude
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.
是的,该-L
选项会添加搜索路径,但链接器会自行添加.so
(or .a
) 后缀(就像添加lib
前缀一样)。所以你只需要拥有-lchaiscript_stdlib-5.3.1
,链接器就会找到它。
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
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.
该-Wl
选项告诉编译器前端将一个选项传递给链接器,而链接器选项-rpath
将一个路径添加到运行时链接器搜索路径。