如何在没有硬编码的完整依赖路径的情况下构建共享库(.so)? [英] How to build a shared library (.so) without hardcoded full dependency paths?

查看:32
本文介绍了如何在没有硬编码的完整依赖路径的情况下构建共享库(.so)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要构建两个 3rd 方共享库,因此它们的 .so 文件将被其他项目重用.但是,在构建这些库之一后,包含到另一个库的硬编码路径.此路径在其他机器上无效并导致链接器警告.如何防止完整路径嵌入到生成的 .so 文件中?

I need to build two 3rd party shared libraries, so their .so files will be reused by other projects. However, after build one of these libraries contains hardcoded path to another. This path is invalid on other machines and causes linker warnings. How can I prevent the full path from being embedded in the resulting .so files?

详情:

第一个库源:~/dev/A
第二个库源:~/dev/B

它们都有 configure 脚本来生成 make 文件.库 B 依赖于 A.所以,首先我构建 A:

Both of them have configure script to generate make files. Library B depends on A. So, first I build A:

$ ~/dev/A/configure --prefix=~/dev/A-install
$ make && make install

然后我构建B:

$ ~/dev/B/configure --prefix=~/dev/B-install --with-A=~/dev/A-install
$ make && make install

然后我想把~/dev/A-install~/dev/B-install的内容上传到我们的文件服务器,让其他团队和build机器可以使用二进制文件.但是当他们尝试使用 B 时会收到链接器警告:

Then I want to upload the contents of ~/dev/A-install and ~/dev/B-install to our file server, so other teams and build machines can use the binaries. But they get linker warnings when they try to use B:

/usr/bin/ld: warning: libA.so.2, needed by /.../deps/B/lib/libB.so, not found (try using -rpath or -rpath-link)

当我运行 ldd libB.so 时,它给出:

When I run ldd libB.so it gives:

...
libA.so.2 => /home/alex/dev/A-install/lib/libA.so.2

显然这条路径只存在于我的机器上,其他机器上找不到.

Obviously this path exists only on my machine and cannot be found on other machines.

如何从 libB.so 中删除完整的硬编码路径?

How can I remove full hardcoded path from libB.so?

谢谢.

推荐答案

您必须使用 --prefix 值,该值将在两个包的 runtime 环境中有效!

You have to use --prefix value that will be valid in the runtime environment for both packages!

比你覆盖 prefixDESTDIR (prefix 替换前缀,DESTDIR 是前置的,但是安装时在 make 命令行上工作更可靠).喜欢:

Than you override prefix or DESTDIR (prefix replaces the prefix, DESTDIR is prepended to it, but works more reliably) on the make command-line when installing. Like:

~/dev/A$ ./configure
~/dev/A$ make 
~/dev/A$ make install prefix=~/dev/A-install
~/dev/B$ ./configure --with-A=~/dev/A-install
~/dev/B$ make
~/dev/B$ make install prefix=~/dev/B-install

或者(这是首选,也是所有包构建工具使用它的方式):

or (which is preferred and is how all package-building tools use it):

~/dev/A$ ./configure
~/dev/A$ make 
~/dev/A$ make install DESTDIR=~/dev/A-install
~/dev/B$ ./configure --with-A=~/dev/A-install/usr/local
~/dev/B$ make
~/dev/B$ make install prefix=~/dev/B-install

因为这样你是安装到~/dev/A-install/$prefix,所以使用默认前缀~/dev/A-install/usr/local.后一个选项的优点是,如果您重新定义一些特定的安装路径而不引用前缀(例如 --sysconfdir=/etc),DESTDIR 仍将被添加到它,虽然它不会受到prefix的影响.

because this way you are installing to ~/dev/A-install/$prefix, so with default prefix ~/dev/A-install/usr/local. The advantage of this later option is, that if you redefine some specific installation paths without refering to prefix (say --sysconfdir=/etc), DESTDIR will still get prepended to it, while it won't be affected by prefix.

这篇关于如何在没有硬编码的完整依赖路径的情况下构建共享库(.so)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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