简体   繁体   中英

standalone static library (.a) with cmake

I need to provide an SDK with a static library. Let's call it "libsdk.a". This library should hopefully be standalone, meaning a simple example "example.cpp" could link against it without any other library, except the system ones.

Here my configuration:

  • cmake for all my 10 dependencies libraries. There is a static library (.a) generated for each of my module. These libraries contain only object files.o of the given module. Dependency tree is not flat, some of them depends on others.
  • a simple example "example.cpp", with a cmake, which compiles and works. At this level, cmakes generates a very complex link command to deal with deps tree.
  • external dependencies such as boost (some static libraries too)

At the moment, I tried this:

  • make an archive of the different.a generated but it doesn't work because linking against this lib tells me the archive a no index (even after ranlib). Yet, I remembered I could add.a libraries inside.a files without problems.
  • extract all.o object (with ar -x) files from all *.a files and recreated a "libsdk.a" with all these object files. It doesn't work either (unresolved references). Moreover, it includes all objects even those which are not needed... My working example takes 3.7M. This library is around 35M.
  • make a.so shared library. It seems to work but I would prefer having a static library.
  • compile all statically but then linker complains it doesn't find -lgcc_s. Ok I want to compile in static but not that far, and just my own libs together !

So my final question: is there any way I can generate my static lib containing all my other libs, and not system ones?

BTW, another interesting topic on that: Combining static libraries

Thank you for any piece of advice to open my mind !

What you are trying to do by hand is the job of the linker. While it's feasible, you shouldn't bother with it.

When you compile libsdk.a , make sure that all of its dependencies are linked statically. If you do this, libsdk.a should be standalone. Static linking means copying the code to the right places in the final executable, so anything that is linked statically will not need to be provided in an external file.

See this post on CMake mailing list . libutils.cmake attached to the message has MERGE_STATIC_LIBS() macro that does the job. On Linux (and all other Unixes except OSX) it uses ar to pack and unpack objects files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM