3
votes

I'm porting existing code to autotools. Previously, all defines were passed through the command line, e.g. -DOS_LINUX.

There are many source files, some are generated.

In my configure.ac I've added:

AC_CONFIG_HEADERS(config.h)

Now I want to be sure that if a source file does not include config.h, compile fails.

My current solution is to redefine int on command line and fix it back in config.h:

CPPFLAGS="$CPPFLAGS -Dint=bad -I\$(top_srcdir)"
AH_BOTTOM([#include <custom.h>])
AC_OUTPUT

custom.h:

#undef int

I'm looking for a cleaner way. For example, is it correct to use the makefile variable $(top_srcdir) the way I use it?

3
Normally any declarations or statements in a source file that depend on declarations in a header file will trigger compilation errors if that header is not #included. Obviously the error messages will be syntax errors and the like (rather than something like "header file config.h needed" but it does cause the compilation unit to not compile. And, if there is no actual dependency, what does it matter if the header file is not #included? Redefining any keyword (including int) also causes undefined behaviour - even if later "fixed".Peter

3 Answers

1
votes

This is not exactly what you ask, but would you consider adding config.h into every file? You could add in your CFLAGS/CXXFLAGS the -include option in order to ensure that every file in the project includes the config.h.

See https://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html

-include file Process file as if #include "file" appeared as the first line of the primary source file. However, the first directory searched for file is the preprocessor's working directory instead of the directory containing the main source file. If not found there, it is searched for in the remainder of the #include "..." search chain as normal. If multiple -include options are given, the files are included in the order they appear on the command line.

0
votes

In my opinion, better solution (more clear when you see the error) is the following:

custom.h:

#ifndef CONFIG_INCLUDED
#error "config.h is missing"
#endif

config.h:

#define CONFIG_INCLUDED

main.cpp:

#include "config.h"
#include "custom.h"
0
votes

When I did the same thing a few years ago, I just left the autoconf generated defines on the command line because there was just too much to fix with so little benefit. There's nothing about the GNU Build System that requires you to have a config.h included.