确保包含 config.h

Ensure config.h included

我正在将现有代码移植到 autotools。以前,所有定义都是通过命令行传递的,例如-DOS_LINUX.

源文件很多,生成了一些。

在我的 configure.ac 我添加了:

AC_CONFIG_HEADERS(config.h)

现在我想确定如果源文件不包含 config.h,编译会失败。

我目前的解决方案是在命令行上重新定义 int 并在 config.h 中修复它:

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

custom.h:

#undef int

我正在寻找一种更简洁的方法。例如,像我这样使用makefile变量$(top_srcdir)是否正确?

在我看来,更好的解决方案(当你看到错误时更清楚)如下:

custom.h:

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

config.h:

#define CONFIG_INCLUDED

main.cpp:

#include "config.h"
#include "custom.h"

这与您的要求不完全相同,但是您会考虑将 config.h 添加到每个文件中吗?您可以在 CFLAGS/CXXFLAGS 中添加 -include 选项,以确保项目中的每个文件都包含 config.h.

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.

几年前,当我做同样的事情时,我只是将 autoconf 生成的定义留在命令行上,因为要修复的东西太多了,但收效甚微。 要求您必须包含 config.h 的 GNU 构建系统。