java调用clang编译的so,用SO进行Clang链接

当你尝试链接动态库时遇到ld: library not found错误,通常是由于链接器没有找到库文件。即使库文件在同一目录下,-L选项添加的路径并不自动加上.so扩展名。只需指定-lchaiscript_stdlib-5.3.1,链接器会自动查找。如果运行时链接器找不到库,可以使用-Wl,-rpath选项指定绝对路径添加到运行时搜索路径。
摘要由CSDN通过智能技术生成

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.

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.

解决方案

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

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值