1

I have a problem very similar to this question. I need to tell the linker to add the static lib, despite the fact that to reference to its symbols seems to be made. The reason for that is that the library contains an function in .init or using ( __attribute__ ((constructor))) to perform its initialisation, which in turn, is going to call a register() function from the main program to register its functionality (read: pass other function pointers).

So despite the fact that no symbol from the library seems to be used in the main program, the main program will call functions from the library as soon as the latter are registered via the library init function.

But I am using libtool... So taking the example given in the previously mentionned question, I'd need to write something like:

bin_PROGRAMS = pktanon 
pktanon_SOURCES = main.cpp
pktanon_DEPENDENCIES = $(lib_LIBRARIES)
pktanon_LDADD = libpktanon.la $(LDADD) 

Note the "la" extension instead of "a" for the lib.

Now, how shall I pass the --whole-archive option to the linker? The answer suggested in the question assumes the path to the archive (.a file) is known... It does not feel right to hard code a path like .libs/libptanon.a in the Makefile.am... And the linker does not like meeting a .la file in its whole-archive otpion if trying:

pktanon_LDFLAGS = -Wl,--whole-archive,libpktanon.la,--no-whole-archive

Any suggestions?

Community
  • 1
  • 1
user1159290
  • 951
  • 9
  • 27

1 Answers1

0

The use of --whole-archive is not portable, so why not create a Libtool convenience library instead? In your Automake Makefile.am, add noinst_ to the library, then instead of creating a (possibly non-pic) static archive, it will take all the object files in the library and add it to anything that links to it. It is portable, and should work for this case, as it doesn't seem like you want a library here at all.

Robert Boehne
  • 271
  • 3
  • 11
  • The problem is that my lib uses symbols from the main program, rather than the opposite: autotools assumes that convenience libs L defines symbols referenced by the main program P: autotools will pass P and L to the linker, in this order. Without the --whole-archive (or --start-group) option, the linker will not "cross-link" L and P. In my case L refers to symbols from P: P defines the symbols referenced by L, which is the opposite of autotools assumtions. This guy had the same problem: http://lists.gnu.org/archive/html/automake/2006-03/msg00042.html See point II (motivation) section 3. – user1159290 Oct 20 '16 at 07:09
  • You will be better off if you fix your design so it isn't circular - or just treat your "library" as a collection of object files, and don't try to put them in an archive. A design like this cannot be ported to Windows for example. – Robert Boehne Feb 28 '17 at 22:29