开发者

How can I compile an autotools project with statically linked dependancies?

开发者 https://www.devze.com 2023-01-01 05:01 出处:网络
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

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开发者_StackOverflow 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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消