用.so文件链接的Clang [英] Clang linking with a .so file

查看:62
本文介绍了用.so文件链接的Clang的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断得到

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 文件时.

我正在使用以下命令:

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.

这篇关于用.so文件链接的Clang的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆