5

I am trying to add some compilation options in Automake, but none of the approaches work.

Here is my configure.ac:

AC_INIT(...)
AC_PREREQ([2.59])
AC_CONFIG_AUX_DIR([build-aux])
AM_INIT_AUTOMAKE([1.10 foreign -Wall no-define])
AC_PROG_CXX
AC_PROG_LIBTOOL
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_FILES([Makefile src/Makefile test/Makefile])
AC_LANG(C++)
AC_OUTPUT

And I have the parent Makefile.am (in top-level dir):

#AM_CFLAGS = ... # doesn't work
AUTOMAKE_OPTIONS = subdir-objects
ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS}
SUBDIRS = src test
dist_noinst_SCRIPTS = autogen.sh

and a Makefile.am for each source dir; src/Makefile:

include_HEADERS = ...
lib_LIBRARIES = ...

and test/Makefile:

#AM_CFLAGS = ... # doesn't work
bin_PROGRAMS = myprog #test
myprog_SOURCES = ...
myprog_LDADD = ../src/libmylib.a
#myprog_CFLAGS = ... # Doesn't work either

I tried uncommenting all combinations of commented lines, i.e.:

  • add AM_CFLAGS = ... to the parent Makefile.am - this should set the CFLAGS for all affected sources
  • add AM_CFLAGS = ... to other two Makefile.ams
  • add myprog_CFLAGS = ... to Makefile.am (this should set CFLAGS when compiling myprog)

but none of these has any effects. Both my program (myprog) and its library (mylib) are kept being compiled / linked with some default flags which are something like -DHAVE_CONFIG -g -O2.

I also tried using INCLUDES instead of AM_CFLAGS, but didn't help.

Anybody has some idea what is going on here and how to fix it?

eold
  • 5,972
  • 11
  • 56
  • 75

2 Answers2

7
  1. It looks like you're compiling C++, in which case the variable to use is AM_CXXFLAGS.
  2. Setting AM_CXXFLAGS should be in the Makefile.am that declares things you're actually compiling (i.e., bin_PROGRAMS, lib_LTLIBRARIES, ...).
  3. If you're repeating yourself, don't forget automake supports an include statement.
  4. Recursive make considered harmful. Modern automake supports subdir-objects. If one Makefile.am gets out of hand, use include statements.
  5. The modern way to turn on libtool is LT_INIT, not AC_PROG_LIBTOOL.
  6. AC_LANG(C++) doesn't do anything at point of configure.ac. It sets the language to use when running configure tests. Besides, AC_LANG_PUSH and AC_LANG_POP are smarter ways of doing that.
  7. Why are you assigning to ACLOCAL_AMFLAGS like that?
Jack Kelly
  • 18,264
  • 2
  • 56
  • 81
  • Thanks. Btw, what do you mean by 7., what would you do instead? I am new to autotools. – eold Mar 17 '13 at 18:22
  • No worries. `ACLOCAL_AMFLAGS` is where you set extra flags to be passed to `aclocal` (usually something like `-I m4`, if you've got a directory of custom macros). If you're not setting `ACLOCAL_FLAGS`, there shouldn't be a need for that line at all. – Jack Kelly Mar 17 '13 at 20:55
-1

I tried adding AM_CFLAGS to the parent Makefile.am as suggested, which didn't work. When I added it to the relevant Makefile.am, it ended introducing two conflicting -g options as shown below:

libtool: link: gcc -std=gnu99 -g -O0 -g -O2 -Wl -pthread ...

The correct way to disable optimization is to add CFLAGS = -g -O0 to the Makefile.am where it's needed. Try deleting Makefile.in and Makefile (no extension) if the change doesn't take effect for some reason.

Here's the correct linker directive:

libtool: link: gcc -std=gnu99 -g -O0 -Wl -pthread ...
Dan
  • 3,246
  • 1
  • 32
  • 52
  • 1
    "*The correct way to disable optimization is to add `CFLAGS = -g -O0` to the Makefile.am*" That gives a warning: "'CFLAGS' is a user variable, you should not override it; use 'AM_CFLAGS' instead" – Geremia Feb 16 '16 at 04:29