title: How to use --whole-archive with libtool
body: <p>How can I use  --whole-archive with libtool without it being reordered?</p>
  
  <p><strong>Background:</strong>
  I'm compiling Extrae (performance profiling) from sources which depends on Dyninst which depends on libdwarf, which, on Debian Wheezy, is provided as a static library (usr/lib/libdwarf.a).</p>
  
  <p>Dyninst is compiled from sources:</p>
  
  <pre><code> # ./configure --with-libdwarf-static; make; make install
  </code></pre>
  
  <p>creates: </p>
  
  <pre><code>/usr/lib/libdynDwarf.so.8.1
  /usr/lib/libdyninstAPI.so.8
  </code></pre>
  
  <p>Then Extrae is compiled from <a href="http://www.bsc.es/computer-sciences/performance-tools/downloads" rel="noreferrer">sources</a>.</p>
  
  <pre><code># ./configure  --with-mpi=/usr --with-mpi-libs=/usr/lib --with-papi=/usr/local --with-unwind=/usr --with-dyninst=/usr --with-dwarf=/usr --with-dwarf-libs=/usr/lib
  ...
  # make
  ...
  /bin/sh ../../../libtool --tag=CXX   --mode=link g++ -I../../../src/common -I/usr/include  -g -O2 -Wall -W -L/usr/lib64 -ldyninstAPI -L/usr/lib64 -lunwind -lparseAPI -lsymtabAPI -linstructionAPI   -lcommon -L/usr/lib -ldwarf  -lelf -L/usr/lib -lxml2  -o extrae extrae-extrae.o extrae-forkSnippets.o extrae-cudaSnippets.o extrae-ompSnippets.o extrae-apiSnippets.o extrae-mpiSnippets.o extrae-commonSnippets.o extrae-applicationType.o extrae-mini-xml-parse.o
  libtool: link: g++ -I../../../src/common -I/usr/include -g -O2 -Wall -W -o extrae extrae-extrae.o extrae-forkSnippets.o extrae-cudaSnippets.o extrae-ompSnippets.o extrae-apiSnippets.o extrae-mpiSnippets.o extrae-commonSnippets.o extrae-applicationType.o extrae-mini-xml-parse.o  -L/usr/lib64 -ldyninstAPI -lunwind -lparseAPI -lsymtabAPI -linstructionAPI -lcommon -L/usr/lib -ldwarf -lelf -lxml2
  /usr/lib/libdynDwarf.so.8.1: undefined reference to `dwarf_elf_init'
  /usr/lib/libdynDwarf.so.8.1: undefined reference to `dwarf_finish'
  collect2: error: ld returned 1 exit status
  make[4]: *** [extrae] Error 1
  make[4]: Leaving directory `/root/src/extrae-2.3.4/src/launcher/dyninst'
  </code></pre>
  
  <p>This error is caused because libdynDwarf needs symbols that aren't included when the static library is linked. A solution to this is provided <a href="https://lists.cs.wisc.edu/archive/dyninst-api/2013/msg00026.shtml" rel="noreferrer">here</a>, to replace:</p>
  
  <pre><code> -ldwarf
  </code></pre>
  
  <p>with:</p>
  
  <pre><code>-Wl,--whole-archive -ldwarf -Wl,--no-whole-archive
  </code></pre>
  
  <p>I make this change to:</p>
  
  <pre><code>./src/launcher/dyninst/Makefile.am
  </code></pre>
  
  <p>then configure and make again. I get this output:</p>
  
  <pre><code>/bin/sh ../../../libtool --tag=CXX   --mode=link g++ -I../../../src/common -I/usr/include  -g -O2 -Wall -W -L/usr/lib64 -ldyninstAPI -L/usr/lib64 -lunwind -lparseAPI -lsymtabAPI -linstructionAPI   -lcommon -L/usr/lib -Wl,--whole-archive -ldwarf -Wl,--no-whole-archive  -lelf -L/usr/lib -lxml2  -o extrae extrae-extrae.o extrae-forkSnippets.o extrae-cudaSnippets.o extrae-ompSnippets.o extrae-apiSnippets.o extrae-mpiSnippets.o extrae-commonSnippets.o extrae-applicationType.o extrae-mini-xml-parse.o
  libtool: link: g++ -I../../../src/common -I/usr/include -g -O2 -Wall -W -Wl,--whole-archive -Wl,--no-whole-archive -o extrae extrae-extrae.o extrae-forkSnippets.o extrae-cudaSnippets.o extrae-ompSnippets.o extrae-apiSnippets.o extrae-mpiSnippets.o extrae-commonSnippets.o extrae-applicationType.o extrae-mini-xml-parse.o  -L/usr/lib64 -ldyninstAPI -lunwind -lparseAPI -lsymtabAPI -linstructionAPI -lcommon -L/usr/lib -ldwarf -lelf -lxml2
  /usr/lib/libdynDwarf.so.8.1: undefined reference to `dwarf_elf_init'
  /usr/lib/libdynDwarf.so.8.1: undefined reference to `dwarf_finish'
  </code></pre>
  
  <p>Note that the liking ordering translated by libtool to (-ldwarf no longer wrapped by --whole-archive):</p>
  
  <pre><code> g++ ... -Wall -W -Wl,--whole-archive -Wl,--no-whole-archive -o extrae ... -ldyninstAPI ... -ldwarf ...
  </code></pre>
  
  <p>If I manually run the following two reordered commands, compilation succeeds and I can re-run make and it continues successfully. </p>
  
  <pre><code>g++ -I../../../src/common -I/usr/include -g -O2 -Wall -W -o load-module load_module-load-module.o  -L/usr/lib64 -ldyninstAPI -lunwind -lparseAPI -lsymtabAPI -linstructionAPI -lcommon -L/usr/lib -Wl,--whole-archive -ldwarf -Wl,--no-whole-archive -lelf -lxml2
  g++ -I../../../src/common -I/usr/include -g -O2 -Wall -W -o demo-launcher demo_launcher-demo-launcher.o  -L/usr/lib64 -ldyninstAPI -lunwind -lparseAPI -lsymtabAPI -linstructionAPI -lcommon -L/usr/lib -Wl,--whole-archive -ldwarf -Wl,--no-whole-archive -lelf -lxml2
  </code></pre>
  
  <p><strong>Question</strong></p>
  
  <ul>
  <li><strong>how can I stop libtool reordering flags?</strong></li>
  <li><strong>Is there a better solution?</strong></li>
  </ul>
  
  <p>Reference: Snipits of Makefile.am:</p>
  
  <pre><code>...
  bin_PROGRAMS = extrae
  ...
  
  extrae_SOURCES = extrae.C, ...
  ...
  extrae_CXXFLAGS = -I$(COMMON_DIR) 
  extrae_CFLAGS = -I$(COMMON_DIR) -I@DYNINST_INCLUDES@ @XML2_CFLAGS@
  extrae_LDFLAGS =  @DYNINST_LDFLAGS@ -ldyninstAPI @UNWIND_LDFLAGS@ @UNWIND_LIBS@ -lparseAPI -lsymtabAPI -linstructionAPI $(DYNINST_EXTRA_LIBS) -lcommon @DWARF_LDFLAGS@ -ldwarf @ELF_LDFLAGS@ -lelf @XML2_LDFLAGS@ @XML2_LIBS@
  </code></pre>