可以静态链接共享对象库吗?

发布于 2024-10-07 16:49:11 字数 240 浏览 0 评论 0原文

我正在构建一个需要动态链接到我的项目的库。输出是一个 .so 文件,所以我认为我走在正确的轨道上。我担心它在编译时的链接方式 - 通过指定其 makefile 的位置并依赖于一堆我以前从未遇到过的宏。

我是否可以假设,由于我正在构建一个 .so 库(而不是 .a),所以我实际上是在动态链接?或者 .so 库是否可以静态链接,在这种情况下我需要分解 make/config 文件以更好地了解发生了什么?

谢谢,

安德鲁

I'm building a library that needs to be dynamically linked to my project. The output is a .so file, so I think I'm on the right track. I'm concerned by the way it's being linked at compile time - by specifying the location of its makefile and depending on a bunch of macros, which I've never encountered before.

Can I assume that since I'm building a .so library (rather than a .a) that I'm in fact dynamically linking? Or is it possible for .so libs to be statically linked, in which case I need to rip apart the make/config files to better understand what's going on?

Thanks,

Andrew

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮光之海 2024-10-14 16:49:11

我不熟悉可执行文件和共享对象的内部结构,所以我只能给出一些实用的提示。

假设您使用gcc,当将目标文件链接到库时,它应该有-shared选项 - 这样ld(由gcc<调用) /code>) 生成共享对象而不是可执行二进制文件。

gcc -shared -o libabc.so *.o ...

当您将某些应用程序与此 libabc.so 链接时,它应该正确链接,然后使用 ldd 命令您应该能够在其依赖项中看到 libabc.so 。

$ ldd app
    ...
    libabc.so => ...............

I'm not familiar with internal structure of executables and shared objects, so I could only give some practical hints.

Assuming you use gcc, it should have -shared option when linking object files into library - this way ld (called by gcc) makes shared object instead of executable binary.

gcc -shared -o libabc.so *.o ...

When you link some application with this libabc.so it should link without errors and after that with ldd command you should be able to see libabc.so among its dependencies.

$ ldd app
    ...
    libabc.so => ...............
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文