Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a define through "./configure" with Autoconf

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?

like image 366
Luiz Antonio Avatar asked Aug 09 '12 12:08

Luiz Antonio


People also ask

How do I setup a script autoconf?

To create a configure script with Autoconf, you need to write an Autoconf input file configure.ac (or configure.in ) and run autoconf on it. If you write your own feature tests to supplement those that come with Autoconf, you might also write files called aclocal. m4 and acsite. m4 .

What is Autotools configure?

Autotools configurationThis file is used by autoconf to create the configure shell script that users run before building. The file must contain, at the very least, the AC_INIT and AC_OUTPUT M4 macros.

How does autoconf work?

Autoconf essentially runs the preprocessor on your script to produce a portable shell script which will perform all the requisite tests, produce handy log files, preprocess template files, for example to generate Makefile from Makefile.in and and take a standard set of command line arguments.


2 Answers

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
like image 104
Some programmer dude Avatar answered Oct 05 '22 23:10

Some programmer dude


These days autoheader demands

AC_DEFINE([MYDEFINE], [1], [Description here])
like image 32
Carlo Wood Avatar answered Oct 06 '22 01:10

Carlo Wood