MathManiac MathManiac - 2 months ago 13
C++ Question

Autotools adds library flags to ar

I'm currently porting my C++ project to use the GNU autotools toolchain, and I'm having trouble setting up my library dependencies. What seems to be happening is that it gives the command

ar
flags meant for g++, such as
-lSDL2 -L/usr/local/lib -lSDL2_image -lSDL2
.

Here's the code for
src/Makefile.am
:

noinst_LIBRARIES = libccade.a
libccade_a_SOURCES = CCade.h CCade_graphics.h CCade_graphics.cpp CCade_util.h CCade_util.cpp
libccade_a_LIBADD = $(libsdl2_LIBS) $(libsdl2_image_LIBS)
libccade_a_CFLAGS = $(libsdl2_CFLAGS) $(libsdl2_image_CFLAGS)


Here's what
make
puts out:

ar cru libccade.a CCade_graphics.o CCade_util.o -lSDL2 -L/usr/local/lib -lSDL2_image -lSDL2
ar: invalid option -- 'L'
Usage: ar [emulation options] [-]{dmpqrstx}[abcDfilMNoPsSTuvV] [--plugin <name>] [member-name] [count] archive-file file...
...(more documentation on ar)...


It seems to be adding the args from
libccade_a_LIBADD
, but if I remove that variable, then it wouldn't give the flags to
g++
. What do I do?

Answer

It looks like you are not using libtool. What I generally do is put this in my configure.ac:

AM_PROG_LIBTOOL

And then my Makefile.am would be something like:

noinst_HEADERS = CCade.h CCade_graphics.h CCade_util.h

noinst_LTLIBRARIES = libccade.la

libccade_la_SOURCES = CCade_graphics.cpp CCade_util.cpp
libccade_la_LIBADD = $(libsdl2_LIBS) $(libsdl2_image_LIBS)
libccade_la_CPPFLAGS = $(libsdl2_CFLAGS) $(libsdl2_image_CFLAGS)

Making sure that the second variable is noinst_LTLIBRARIES, it should work for you.