无法使自动制作使用 C++11

Can't make automake to use C++11

本文关键字:C++11      更新时间:2023-10-16

我正在做一些项目,似乎我不能使自动制作脚本使用c++ 11。

在我的项目的rootdir中,我有文件Makefile.am,看起来像这样(它是由eclipse自动生成的):

SUBDIRS=src

然后在/rootdir/src中我有Makefile。它看起来像这样:

AM_CXXFLAGS=-Wall -fPIC -std=gnu++11 -DVERSION="$(VERSION)" -DPROG=""$(PACKAGE)""
bin_PROGRAMS = algatorc
algatorc_SOURCES = algatorc.cpp
include_HEADERS = Timer.hpp TestSetIterator.hpp TestCase.hpp ETestSet.hpp EParameter.hpp Entity.hpp ParameterSet.hpp AbsAlgorithm.hpp Log.hpp JSON.hpp JSONValue.hpp
lib_LIBRARIES = libAlgatorc.a
libAlgatorc_a_SOURCES = ParameterSet.cpp TestCase.cpp EParameter.cpp ETestSet.cpp TestSetIterator.cpp Entity.cpp Timer.cpp JSON.cpp JSONValue.cpp AbsAlgorithm.cpp
algatorc_LDADD=libAlgatorc.a

所以,我为c++ 11支持添加了-std=gnu++11,但我仍然得到这个错误:

g++ -DPACKAGE_NAME="algatorc" -DPACKAGE_TARNAME="algatorc" -DPACKAGE_VERSION="1.0" -DPACKAGE_STRING="algatorc 1.0" -DPACKAGE_BUGREPORT="" -DPACKAGE_URL="" -DPACKAGE="algatorc" -DVERSION="1.0" -I.     -g -O2 -MT algatorc.o -MD -MP -MF .deps/algatorc.Tpo -c -o algatorc.o algatorc.cpp
In file included from /usr/include/c++/4.8/thread:35:0,
                 from Log.hpp:281,
                 from algatorc.cpp:18:
/usr/include/c++/4.8/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support for the 
  ^

从这个错误我可以看到g++不使用-Wall -fPIC -std=gnu++11,但我不明白为什么。它用的是完全不同的东西。这是我的配置。Ac脚本位于我的项目的rootdir

AC_PREREQ([2.69])
AC_INIT([algatorc], [0.1], [my_mail])
AC_CONFIG_SRCDIR([src/TestCase.hpp])
#AC_CONFIG_HEADERS([config.h])
LT_INIT
AM_INIT_AUTOMAKE
# Checks for programs.
AC_PROG_CXX
# Checks for libraries.
# Checks for header files.
AC_CHECK_HEADERS([stdlib.h string.h sys/time.h unistd.h wchar.h wctype.h])
# Checks for typedefs, structures, and compiler characteristics.
AC_CHECK_HEADER_STDBOOL
AC_C_INLINE
AC_TYPE_SIZE_T
# Checks for library functions.
AC_FUNC_MALLOC
AC_FUNC_MKTIME
AC_CHECK_FUNCS([gettimeofday memset mkdir])
LIBS=-ldl
AC_CONFIG_FILES([Makefile])
AC_OUTPUT

我也尝试添加AX_CXX_COMPILE_STDCXX_11来配置。Ac脚本,但错误仍然发生。知道怎么解决这个问题吗?

我使用的是Ubuntu 12.04 x64和Eclipse (Version: Mars Release (4.5.0))

一个快速的测试显示,对于我来说,使用类似的配置,一切都工作正常,所以您将不得不通过卷起袖子,查看最终的Makefile来弄清楚Makefile发生了什么。

查看汽车-d Makefile的内部。您应该在其中找到.cpp.o的最终构建规则。搜索"。cpp.o"。它应该看起来像这样:

.cpp.o:
    $(AM_V_CXX)$(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
    $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po

验证后,下一步是查看CXXCOMPILE宏被定义为什么。它应该看起来像这样:

CXXCOMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) 
    $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS)

这就是你正在使用的AM_CXXFLAGS变量。最后,确认它在实际的Makefile中是如何定义的。

在我的例子中,对于我的简单测试,它很简单:

AM_CXXFLAGS = -std=gnu++11

在我的例子中,它只是,在你的例子中,显然你会有其他标志在那里。

就是这样。automake-d Makefile显然非常大,看起来很吓人,但是当您仔细研究它时,它根本不是很复杂。

这将是两件事中的一件。Makefile的另一部分。am修改AM_CXXFLAGS的值,或者修改CXXCOMPILE宏。关于automake的一件事是,如果宏或变量被重新定义,它通常不会报错。它将使用变量的最终值生成最终的Makefile。所以,我猜在稍后的某个地方,在你的Makefile。在这里,您将AM_CXXFLAGS设置为其他内容,而没有意识到它。

注意:实际的宏经常被调整,随着automake的每个后续版本,所以你的宏可能看起来略有不同,但一般的想法应该是一样的。.cpp.o构建规则运行CXXCOMPILE宏,它使用AM_CXXFLAGS

首先,automake在执行automake之前不会生成Makefile

所以,我添加了-std=gnu++11来支持c++ 11,但我仍然得到这个错误:

g++ -DPACKAGE_NAME="algatorc" -DPACKAGE_TARNAME="algatorc" -DPACKAGE_VERSION="1.0" -DPACKAGE_STRING="algatorc 1.0" -DPACKAGE_BUGREPORT="" -DPACKAGE_URL="" -DPACKAGE="algatorc" -DVERSION="1.0" -I.     -g -O2 -MT algatorc.o -MD -MP -MF .deps/algatorc.Tpo -c -o algatorc.o algatorc.cpp
In file included from /usr/include/c++/4.8/thread:35:0,
             from Log.hpp:281,
             from algatorc.cpp:18:
/usr/include/c++/4.8/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support for the 
^
  • 你的日志说在gcc的编译时间没有-std=gnu++11的语句。也就是说,automake没有生成包含-std=gnu++11语句的Makefile

所以,你所需要做的就是,在你的项目根目录下使用命令行:

$ autoreconf

这将自动执行aclocal, autoconf, automake等等…

  • 关于检查-std=c++11标志

最好使用ax_cxx_compile_stdcxx_11.m4
要使用它,您需要从上面的链接下载它并将其设置在$(project_top)/m4/
接下来,在configure中编写如下代码。交流:

AX_CXX_COMPILE_STDCXX_11([noext], [mandatory])

和exec,你可以检查是否可能使用c++ 11的特性在平台

$ autoreconf -vi -I m4
$ ./configure