0
votes

I have an Autogen Makefile.am that I'm trying to use to build a test program for a shared library. To build my test binary, I want to continue building the shared library as target but I want the test program to be linked statically. I've spent the last few hours trying to craft my Makefile.am to get it to do this.

  • I've tried explicitly changing the LDADD line to use the .a version of the library and get a file not found error even though I can see this library is getting built.

  • I try to add the .libs directory to my link path via LDFLAGS and still it can't find it.

  • I tried moving my library sources to my test SOURCES list and this won't work because executable object files are built differently than those for static libraries.

  • I even tried replicating a lib_LIBRARIES entry for the .a version (so there's both a lib_LTLIBRARIES and a lib_LIBRARIES) and replicate all the LDFLAGS, SOURCES, dir and HEADERS for the shared version as part of the static version (replacing la with a of the form _a_SOURCES = _la_SOURCES. Still that doesn't work because now it can't figure out what to build.

  • My configure.ac file is using the default LT_INIT which should give me both static and dynamic libraries and as I said it is apprently building both even if the libtool can't see the .a file.

Please, anyone know how to do this?

1
prog_LDFLAGS = -staticBrett Hale
Thank you! That worked! Keep the LDADD with the library in .la form (shared) but then forcing static on the LDFLAGS does indeed force a static linking with my library! Thank you thank you thank you!TimeHorse
.la isn't necessarily a shared library. It's actually a text file containing the locations of static and shared versions of the library, along with some other information.ptomato

1 Answers

0
votes

As @Brett Hale mentions in his comment, you should tell Makefile.am that you want the program to be statically linked.

To achieve this you must append -static to your LDFLAGS.
Changing the LDFLAGS for a specific binary is achieved by changing binary_LDFLAGS (where binary is the name of the binary you want to build).

so something like this should do the trick:

binary_LDFLAGS = $(AM_LDFLAGS) -static