17

I have one project that can generate two diferent applications based on one define.

libfoo_la_CXXFLAGS = -DMYDEFINE

I have to modify the Makefile.am to set this define, so it is not automatic.

Can I set this define somehow through the configure command? Is there any other way to set one define using autotools?

jww
  • 97,681
  • 90
  • 411
  • 885
Luiz Antonio
  • 223
  • 1
  • 2
  • 6

2 Answers2

13

You have to edit the file configure.ac, and before AC_OUTPUT (which is the last thing in the file) add a call to AC_DEFINE.

In a simple case like yours, it should be enough with:

AC_DEFINE(MYDEFINE)

If you want to set a value, you use:

AC_DEFINE(MYDEFINE, 123)

This last will add -DMYDEFINE=123 to the flags (DEFS = in Makefile), and #define MYDEFINE 123 in the generated autoconf header if you use that.

I recommend you read the documentation from the beginning, and work through their examples and tutorials. Also check other projects' configure files to see how they use different features.

Edit: If you want to pass flags on the command line to the make command, then you do something like this:

libfoo_la_CXXFLAGS = $(MYFLAGS)

Then you call make like this:

$ make MYFLAGS="-DMYDEFINE"

If you don't set MYFLAGS on the command line, it will be undefined and empty in the makefile.

You can also set target-specific CPPFLAGS in Makefile.am, in which case the source files will be recompiled, once for each set of flags:

lib_LTLIBRARIES = libfoo.la libbar.la
libfoo_la_SOURCES = foo.c
libfoo_la_CPPFLAGS = -DFOO
libbar_la_SOURCES = foo.c
libbar_la_CPPFLAGS = -DBAR
Darren Ng
  • 373
  • 5
  • 12
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Ok, maybe I haven't been that clear. I have to set this define to create an executable, but I need to compile the same source code without this define to create another executable. Can I set this define some other way on command line? – Luiz Antonio Aug 09 '12 at 13:18
  • @LuizAntonio Added how to add make-variables on the command line – Some programmer dude Aug 09 '12 at 14:10
  • 1
    Remark: You're meant to set `-D` flags in `AM_CPPFLAGS`,`libfoo_la_CPPFLAGS` or `bar_CPPFLAGS`, as it's a preprocessor flag. – Jack Kelly Aug 09 '12 at 21:49
  • I think Carlo's answer is more correct. Using the pattern above I get `/usr/bin/autoheader failed with exit status: 1` (after a few other warnings). – jww Nov 04 '17 at 02:21
13

These days autoheader demands

AC_DEFINE([MYDEFINE], [1], [Description here])
Carlo Wood
  • 5,648
  • 2
  • 35
  • 47