23

I use AX_CXX_COMPILE_STDCXX_0X(can look on autoconf-archive) to check for c++11 capabilities of the compiler. It correctly determines that -std=c++0x required, but does not add it to CXXFLAGS. I took a look at the macro source and it actually checks but then restores previous flags.

What should I do to get CXXFLAGS set to be able to compile c++11 source?

Just adding -std=c++0x to AM_CXXFLAGS is not nice solution, because I'd like to put the burden of making the compiler compile in C++11 mode on the autoconf developers, not me.

uckelman
  • 25,298
  • 8
  • 64
  • 82
KAction
  • 1,977
  • 15
  • 31

2 Answers2

31

What you're looking for has already been made as AX_CXX_COMPILE_STDCXX_11, part of autoconf-archive. It will add the required option to the environment (formerly through CXXFLAGS, now through CXX) and error out if no C++11 support is available.

  • `configure.ac:15: error: AC_LANG_ASSERT: current language is not C++: C ../../lib/autoconf/lang.m4:156: AC_LANG_ASSERT is expanded from... m4/ax_cxx_compile_stdcxx_11.m4:48: AX_CXX_COMPILE_STDCXX_11 is expanded from... configure.ac:15: the top level autom4te: /usr/bin/m4 failed with exit status: 1 aclocal: error: /usr/bin/autom4te failed with exit status: 1 autoreconf: aclocal failed with exit status: 1` I put it just after `AC_PROG_CXX`. What I do wrong? – KAction Aug 11 '12 at 05:10
  • Sorry for long comment. I found no way to format it. Well, I added `AC_LANG([C++])` just before `AX_CXX_COMPILE_STDCXX_11`, but I assumed it is called by `AC_PROG_CXX`. – KAction Aug 11 '12 at 05:16
  • 1
    @illusionoflife `AC_PROG_CXX` shouldn't do that because it would mess up configure scripts that have both C and C++ checks. Most autoconf checks rely on C. Adding AC_LANG as you have is the right thing to do. –  Aug 11 '12 at 08:53
  • How is this macro installed? Does it come with autoconf itself? If I add this macro to my configure.ac the -std compilerflags are not added. I can also only add the macro without any arguments (i.e. as AX_CXX_COMPILE_STDCXX_11() .. the documentation on autoconf-archive states that it should accept 2 parameters but when I try that it bails out with a syntax error when I run configure. – Lieuwe Oct 24 '14 at 11:12
  • @Lieuwe Please be more specific than "a syntax error". :) Like most macros, this one should end up in your project's m4 directory, and your project should be set up to use that directory as the option argument to the `-I` option. –  Oct 24 '14 at 17:17
  • I can't be a lot more specific: "./configure: line 2308: syntax error near unexpected token `ext,mandatory'" for instance – Lieuwe Oct 27 '14 at 10:44
  • 1
    question here: should ax_cxx_compile_stdcxx_11.m4 set CXXFLAGS (as it does) or AM_CXXFLAGS? The problem is that I cannot do a user-level setting of CXXFLAGS on the make line, for example: make CXXFLAGS="-g -O0 -Wall" fails because the -std=c++11 flag gets lost. – markgalassi Mar 02 '16 at 18:20
  • @markgalassi The latest version doesn't clobber CXXFLAGS, it now clobbers CXX instead. I add `CXX$1_FLAGS=$switch` and `AC_SUBST(CXX$1_FLAGS)` to have it instead set e.g. `CXX11_FLAGS` – Yossarian Oct 31 '16 at 14:21
  • 2
    To get it to work, I also installed autoconf-archive, which is where the macro 'lives', as far as I understand it. – MicroVirus Jan 19 '17 at 18:08
  • 1
    @MicroVirus Right, that's why I linked to documentation from autoconf-archive, but it's good to mention it explicitly. Thanks for the comment. –  Jan 19 '17 at 18:49
1

In general you can compile a simple code and set a variable based the outcome of your compilation

DIALECT="-std=c++14"
echo 'int main() {return 0;}' > ./log.cpp && $CXX -std=c++14 ./log.cpp || $DIALECT="no"

if test $DILAECT = no; then
    AC_MSG_ERROR([c++ compiler does not support c++14])
else
    echo $DILAECT
fi
apramc
  • 1,346
  • 1
  • 15
  • 30