4

Is it possible to tell autotools to link one of the libraries with -Wl,-whole-archive flag?

Makefile.am

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

I need to link libpktanon.a with -Wl,-whole-archive flag, also I want make to execute something like this:

g++ -o pktanon main.o -Wl,-whole-archive libpktanon.a -Wl,-no-whole-archive -l...

(as in this question)

2
  • I must admit I avoid automake but why not just write that whole thing in pktanon_LDADD ? Commented Mar 5, 2014 at 23:06
  • error: linker flags such as '-Wl,-whole-archive' belong in 'pktanon_LDFLAGS'; adding pktanon_LDFLAGS = -Wl,-whole-archive generages following line and two pages of link errors: g++ -g -O2 -std=c++11 -Wl,-whole-archive -o pktanon main.o -l...
    – Effie
    Commented Mar 5, 2014 at 23:18

1 Answer 1

2

I ran into a similar problem here. You can do this:

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

The issue is that Libtool doesn't guarantee the order of linker flags on the actual command line it executes, so you have to force it like this.

2
  • To expose that libpktanon.a file as a dependency (in case it's built right alongside), you may also need a dummy pktanon_LDADD = libpktanon.a line.
    – fche
    Commented Jul 15, 2020 at 21:55
  • I had similar problems before. However these flags get put at the very beginning of the command and the compilation failed.
    – edhu
    Commented Aug 10, 2020 at 0:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.