0

I have a Makefile.am with two noinst_LIBRARIES, and one of them needs to link with the other.

Adding it to the CFLAGS throws a compiler warning, but as far as I know, automake likes to freak out about using LDADD with libraries, since they are not complete programs.

How can I do this, assuming libb.a needs to pull in liba.a?

2mac
  • 1,609
  • 5
  • 20
  • 35
  • 1
    You don't link static libraries, they are added to your binary. – Iharob Al Asimi Jun 03 '15 at 13:46
  • 1
    It's not clear to me what exactly you're trying to do, but note that you cannot link one static library to another. They're just a collection of object files, and there is no mechanism in static libraries to indicate that they depend on another library. – nos Jun 03 '15 at 13:46
  • @nos true, however if there is dependency, i.e. if `liba.a` uses symbols from `libb.a` then when including the libraries in your binary you must be careful about the order in which you pass them. Which means that circular dependencies are **not allowed**. – Iharob Al Asimi Jun 03 '15 at 13:47
  • [Possible Duplicate](http://stackoverflow.com/questions/2157629/linking-static-libraries-to-other-static-libraries) – Dayal rai Jun 03 '15 at 13:48
  • Copy past the compiler warning. – mathk Jun 03 '15 at 13:49
  • 1
    @iharob Circular dependencies can be handled, but you must mention the libraries several times when linking the program that uses them, e.g. pass the flags `-lA -lB -lA` – nos Jun 03 '15 at 13:51
  • possible duplicate of [How to merge two "ar" static libraries into one](http://stackoverflow.com/questions/3821916/how-to-merge-two-ar-static-libraries-into-one) – n. m. could be an AI Jun 03 '15 at 14:12
  • This does make sense in the context of `libtool` libraries, where you can create convenience libraries with `noinst_LTLIBRARIES`, and use: `libb_la_LIBADD = ./liba.la` to let `libtool` take care of resolving static library dependencies. But as others have pointed out, these are just archives. – Brett Hale Jun 03 '15 at 19:32

1 Answers1

1

You can't do it. Actually, what you are trying to do doesn't really make sense. Static libraries are just archives containing object files and a table of contents. Put simply, you can think of a static library as a .zip containing .o files.

The linking phase only takes place when compiling a shared object or executable. When your program is linked against liba.a, you also need to specify -static -lb or similar and that's it.

Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80
  • I suppose I was considering "linking" the static libraries to be "include the object files from the other one in the final one". – 2mac Jun 03 '15 at 14:09
  • 1
    That's very far from linking :) if you meant merging two archives into another, there is no universal way of doing so (because static libraries are not meant to be handled like that), but you might find platform-specific ways. For instance, GCC/Clang static libraries are `ar` archives that you could extract, merge and repack. Instead, MSVC has a tool for that. Be careful with conflicting names or symbols though. – Stefano Sanfilippo Jun 03 '15 at 14:17