如何在 C++ Autotools 项目中禁用 C 编译器

How to disable C compiler in C++ Autotools project

我正处于向 C++ 库添加 Autotools 支持的早期阶段。此时我 运行 autoreconf 具有以下配置。

$ cat Makefile.am
AUTOMAKE_OPTIONS = foreign
bin_PROGRAMS=cryptest

$ cat configure.ac
AC_INIT(Crypto++, 6.0, http://www.cryptopp.com/wiki/Bug_Report)
AM_INIT_AUTOMAKE
AC_PROG_CXX
AC_CONFIG_FILES([Makefile])

正在生产:

$ autoreconf --install --force
/usr/share/automake-1.15/am/depend2.am: error: am__fastdepCC does not appear in AM_CONDITIONAL
/usr/share/automake-1.15/am/depend2.am:   The usual way to define 'am__fastdepCC' is to add 'AC_PROG_CC'
/usr/share/automake-1.15/am/depend2.am:   to 'configure.ac' and run 'aclocal' and 'autoconf' again
Makefile.am: error: C source seen but 'CC' is undefined
Makefile.am:   The usual way to define 'CC' is to add 'AC_PROG_CC'
Makefile.am:   to 'configure.ac' and run 'autoconf' again.
autoreconf: automake failed with exit status: 1

我正在尝试先解决 error: C source seen but 'CC' is undefined 问题。

基于邮件列表阅读的传统智慧是添加 AC_PROG_CC 来解决这个问题。我真的不想解决 C++ 标志将导致 C 编译器出现的问题,尤其是在像 IBM 的 xlc 和 Sun 的 cc 这样的编译器上。这似乎也是错误的,因为 GNU 完全是关于用户选择的。

如何告诉 Autotools 这个项目是一个 C++ 项目,它不应该用 C 或 C 编译器做任何事情?


以下是它引起的一些问题。

$ egrep 'CC|CFLAGS' Makefile
COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
        $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
...
LTCOMPILE = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
        $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) \
        $(AM_CFLAGS) $(CFLAGS)
...
CCLD = $(CC)
...
LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
        $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \

$ autoreconf --version
autoreconf (GNU Autoconf) 2.69

$ autoconf --version
autoconf (GNU Autoconf) 2.69

$ automake --version
automake (GNU automake) 1.15

当你定义一个像 bin_PROGRAMS=cryptest 这样的程序时,automake 会寻找 cryptest_SOURCES 来找出 cryptest 的源文件。如果你没有定义 cryptest_SOURCES,automake 将通过在程序名称后附加“.c”(默认情况下)自动生成一个,例如就好像你已经定义了 cryptest_SOURCES=cryptest.c。要覆盖默认值,您可以明确定义每个程序的源代码,例如cryptest_SOURCES=cryptest.cpp,或者您可以定义 AM_DEFAULT_SOURCE_EXT=.cpp 以使所有自动生成的源文件名以“.cpp”而不是“.c”结尾。

当然,如果您的源名称与程序名称不匹配,或者有多个源(包括您希望 "make dist" 包含的任何头文件),您将需要无论如何明确定义,例如cryptest_SOURCES=cryptest.cpp cryptest-part2.cpp cryptest.h.

参见:https://www.gnu.org/software/automake/manual/automake.html#Default-_005fSOURCES

编辑添加:假设您要使用 AC 宏来测试编译器的功能,您将要先调用 AC_LANG([C++])(在 AC_PROG_CXX 之后)来告诉autoconf,它应该测试 C++ 编译器,而不是 C 编译器。