Discussion:
How to add directory to CFLAGS (or CXXFLAGS) search path?
Patrick Doyle
2014-11-10 03:06:52 UTC
Permalink
Hello,
I am very new to developing autotools maintained projects, but like (a
lot) what I've read so far. So, I would like to use autotools on
projects going forward, but have a large body of legacy code to
maintain. I would like to reference an include directory from some of
that legacy code in my new, autotools maintained, program.

What is the best way to do this? Is there an existing macro I can use
to say "Search the filesystem for somedir/custominclude.h and add
-Isomedir to CFLAGS"?

I know I can write a custom macro, or just plain m4sh code myself, to
do this (once I learn a little more), but I figured I should ask if
this has already been done.

Thanks for any pointers you care to give me...

--wpd
Mike Frysinger
2014-11-10 17:59:48 UTC
Permalink
Post by Patrick Doyle
I am very new to developing autotools maintained projects, but like (a
lot) what I've read so far. So, I would like to use autotools on
projects going forward, but have a large body of legacy code to
maintain. I would like to reference an include directory from some of
that legacy code in my new, autotools maintained, program.
What is the best way to do this? Is there an existing macro I can use
to say "Search the filesystem for somedir/custominclude.h and add
-Isomedir to CFLAGS"?
I know I can write a custom macro, or just plain m4sh code myself, to
do this (once I learn a little more), but I figured I should ask if
this has already been done.
Thanks for any pointers you care to give me...
preprocess flags (like -I) go in CPPFLAGS, not CFLAGS/CXXFLAGS

if you're just using autoconf, you can merely append CPPFLAGS:
CPPFLAGS="$CPPFLAGS -Ifoo"

however, assuming you're trying to use a source or build directory, you should
do it in your automake files instead so you can leverage $(top_srcdir) and
$(top_builddir). see this page for details:
http://www.gnu.org/software/automake/manual/html_node/Program-Variables.html#Program-Variables
-mike
Patrick Doyle
2014-11-10 18:17:17 UTC
Permalink
Hi Mike,
Thanks for the tip. I received some help this morning on IRC and
settled on something vaguely like:

dnl Search for legacy code trunk directory, allow --with-legacy=/path/to/branch
legacy_shared_include=legacy_include.h
AC_ARG_WITH([legacy],
[AS_HELP_STRING([--with-legacy=/path/to/branch],
[specify path to legacy source branch @<:@default=../../../trunk@:>@])],
[legacy_path=$withval],
[legacy_path=../../../trunk])
AC_CHECK_FILE([$legacy_path/common/include/$legacy_shared_include],
[LEGACY_CFLAGS="-I$legacy_path/common/include"],
[AC_MSG_ERROR([unable to find $legacy_shared_include])])
AC_SUBST([LEGACY_CFLAGS])

Note, I chose LEGACY_CFLAGS over LEGACY_CPPFLAGS in order to mimic the
behavior of PKG_CHECK_MODULES. That way, the Makefile.am for my new
application looks like:

bin_PROGRAMS = new

new_CXXFLAGS = $(OPENCV_CFLAGS) $(GSTREAMER_CFLAGS) $(ZBAR_CFLAGS)
$(LEGACY_CFLAGS)
new_LDADD = $(OPENCV_LIBS) $(GSTREAMER_LIBS) $(ZBAR_LIBS)
new_SOURCES = new.cpp

I could/should still add some code to configure.ac to handle the case
where the end user specifies --with-legacy or --without-legacy
(instead of --with-legacy=/path/to/branch), but since only 5 of us
will ever use this code, and we're all within mumbling distance of
each other, I decided to punt.

This is more or less what I where I was heading when I first asked my
question... thanks very much to help from <berndj> and <va> on IRC.
But I wanted to ask first if there was an existing macro or method for
doing this. (I think I also had some whacked out idea about magic
code that would traverse the file system back up the tree from where
my code base was installed until it found
common/include/legacy_include.h, but I decided that would offer far
less usefulness than the trouble of writing it would cost).

--wpd

Loading...