如何编译具有静态链接依赖项的自动工具项目?

发布于 2024-09-02 17:59:33 字数 323 浏览 5 评论 0原文

我想使用一个开源库。由于我想将我的软件作为二进制包传播,我不希望该库依赖于其他库,因此我需要静态链接依赖项。

现在,由于该库是开源的并且没有提供二进制文件,因此我自己编译它。该库使用自动工具,我没有找到任何有关如何静态链接依赖项的有用文档。我所做的尝试是使用 --enable-static 调用配置脚本,但这显然只告诉配置编译库的静态版本 - 但我需要的是一个动态库,其中包含它所依赖的所有库。

因此,我需要一种方法来告诉配置静态链接依赖项,或者一种对构建的库进行后处理以包含所有依赖项的方法。谁能告诉我该怎么做?

哦,如果重要的话:我使用的是 64 位 Snow Leopard。

There's an open source library I want to use. As I want to spread my software as binary package, I do not want the library to have dependancies on other libraries, so I need to link the dependancies statically.

Now as the library is open source and there are no binaries provided, I compile it myself. The library uses autotools, and I didn't find any useful documentation on how to link dependancies statically. What I did try is to call the configure script with --enable-static, but this apparently only tells configure to compile a static version of the library - but what I need is a dynamic library that includes all the libraries it depends on.

So, I need a way to either tell configure to link against dependancies statically, or a way to post-process the built library to include all dependancies. Can anyone tell me how to do this?

Oh, and if it matters: I'm on 64bit Snow Leopard.

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

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

发布评论

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

评论(2

素食主义者 2024-09-09 17:59:33

最近我自己也走上了这条路,不幸的是,我发现静态库实际上并不是这样工作的。

当您生成静态可执行二进制文件时,链接器会查看它需要的所有函数,然后查看提供的库列表并提取您需要的每个函数的代码。

当您生成静态库时,您没有进行任何链接,因此所有编译后的代码都会被压缩(实际上它使用 ar 但功能上它与 zip 相同)到 .a 静态库。 (“a”代表“archive”。)因为没有链接阶段,所以没有任何东西可以检查库使用哪个函数调用。它只是有一堆“未解决的外部问题”,稍后会解决。

这意味着当链接二进制文件时,您需要为您需要的所有函数提供代码(库) - 包括您自己的代码使用的函数,以及您需要的所有库使用的函数。

我完全可以理解为什么您想要生成一个包含所有依赖项的库,但是据我所知,它并不是那样工作的。这就是为什么有像 pkg-config 这样的程序,您可以使用它来通知您的(静态或动态)库的用户他们需要链接哪些依赖库才能使用您的库。

最后,请确保检查要链接的任何库的许可证。仅仅因为一个库是开源的,并不意味着您可以不必链接到它。如果它是 GPL 库,那么通过链接到它,您同意在 GPL 下发布您自己的源代码,但您可能不想这样做。

Having recently gone down this path myself, I discovered that unfortunately static libraries don't actually work this way.

When you produce a static executable binary, the linker looks at all the functions it needs, then looks at the list of libraries provided and pulls in the code for each function you need.

When you produce a static library, you're not doing any linking, so all your compiled code just gets zipped up (actually it uses ar but functionally it's the same as a zip) into a .a static library. (The 'a' stands for 'archive'.) Because there is no link stage, there is nothing that examines which function calls the library uses. It merely has a bunch of 'unresolved externals' that will be resolved later.

This means that when the time comes to link your binary, you need to supply code (libraries) for all the functions you need - both the functions your own code uses, and the functions used by all the libraries you need.

I can absolutely see why you'd want to produce a library with all your dependencies included in it, however as far as I can tell, it doesn't work like that. This is why there are programs like pkg-config, which you can use to inform users of your (static or dynamic) library which dependent libraries they need to link with in order to use yours.

Lastly, make sure you check the licence of any library you want to link with. Just because a library is open source, it doesn't mean you can get away with linking to it. If it's a GPL library, then by linking to it you are agreeing to release your own source code under the GPL, which you may not want to do.

折戟 2024-09-09 17:59:33

如果您获得了库的 .a 文件,您可以尝试在项目的 Makefile.am 中添加以下内容。

yourproject_LDADD =  libxxx.a

If you get the .a file of the library you can try to add the following in the Makefile.am of your project.

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