gcc编译时rpath选项不生效问题
问题
笔者在尝试编译qemu时,需要指定qemu在运行时用指定路径下的spice库,尝试使用-wl,-rpath=<link_path>选项,但是编译出来的程序总是自动链接到系统自带的spice库上。
解决方案
通过检索,找到一下链接:https://stackoverflow.com/questions/47117443/dynamic-linking-with-rpath-not-working-under-ubuntu-17-10;他似乎遇到和我一样的问题。
原文答案如下:
What likely happened is that your updated linker emits DT_RUNPATH
dynamic tag, where the old linker emitted DT_RPATH
. (It’s also possible that your old linker was GNU-ld, and the new one is Gold.)
The DT_RUNPATH
is preferred as more correct, and affects search path of the binary itself, but not of any of the dependent libraries.
The DT_RPATH
has global effect, similar to adding the directory to LD_LIBRARY_PATH
environment variable.
You can verify this with: readelf -d a.out | grep 'R.*PATH'
.
If you do see the RPATH
vs. RUNPATH
difference, and in fact are using Gold, you can force the “old” behavior with -Wl,--disable-new-dtags
(GNU ld also had --disable-new-dtags
added to it recently, so it should work for either linker).
总结
通过添加-Wl,--disable-new-dtags
选项可以解决。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)