I\'m working on a C++ project that uses autoconf
& automake
, and I\'m struggling to correctly set up the include paths in *CPPFLAGS
. I
I know it's difficult to get a straight answer from the autotools manuals. There are a couple of good start-to-finish tutorials here and here.
There isn't a standard variable for package-specific *CPPFLAGS
in autoconf. configure
can be invoked with CPPFLAGS=...
, and automake will add this CPPFLAGS
to the relevant makefile rules - search for CPPFLAGS
in a Makefile.in
file for examples. For that reason, I suggest that you not use this variable for anything else.
Add flags in Makefile.am
to the AM_CPPFLAGS
variable (the default for all preprocessor calls) or override individual preprocessor flags with target_CPPFLAGS
. In the example of a 3rd party library, it's best to use a name like: FOO_CPPFLAGS
to hold preprocessor options, e.g.,
FOO_CPPFLAGS="-I${FOO_DIR}/include -DFOO_BAR=1"
...
AC_SUBST(FOO_CPPFLAGS)
and in the Makefile.am
:
AM_CPPFLAGS = -I$(top_srcdir) $(FOO_CPPFLAGS)
# or:
target_CPPFLAGS = -I$(top_srcdir) $(FOO_CPPFLAGS)
The top_srcdir
variable is defined by configure
- I use it to illustrate the 2nd case. Let's say you have file.h
in another directory other
, under the top-level directory. -I$(top_srcdir)
allows you to include it as <other/file.h>
. Alternatively, -I$(top_srcdir)/other
would allow you to include it as <file.h>
.
Another useful preset variable is srcdir
- the current directory. -I$(srcdir)
is added to AM_CPPFLAGS
by default. So if file.h
is in the current directory you can include it with <file.h>
or even "file.h"
. If other
was a 'sibling' directory, -I$(srcdir)/..
would allow you to include <other/file.h>
, and -I$(srcdir)/../other
would allow <file.h>
.
I'd also add that some packages install a pkg-config .pc
file. Provided the installation of pkg-config is set up to search the right directories, you might find the PKG_CHECK_MODULES
macro very useful.