basin
basin

Reputation: 4200

Ensure config.h included

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?

Upvotes: 3

Views: 1420

Answers (3)

ldav1s
ldav1s

Reputation: 16315

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.

Upvotes: 0

Harald
Harald

Reputation: 3180

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.

Upvotes: 1

Alex
Alex

Reputation: 10136

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"

Upvotes: 0

Related Questions