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)

Community
  • 1
  • 1
Effie
  • 758
  • 5
  • 15
  • I must admit I avoid automake but why not just write that whole thing in `pktanon_LDADD` ? – Per Johansson Mar 05 '14 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 Mar 05 '14 at 23:18

1 Answers1

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.

Community
  • 1
  • 1
ptomato
  • 56,175
  • 13
  • 112
  • 165
  • 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 Jul 15 '20 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 Aug 10 '20 at 00:27