14

I have a project configured via Autoconf, and I want to start using C++11 features in this project. How to have the "-std=gnu++0x" switch always enabled and support for the features checked while configuring?

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
lvella
  • 12,754
  • 11
  • 54
  • 106
  • 2
    Unless you want to use GCC extensions, the switch you want to use is "-std=c++0x". – R. Martinho Fernandes Nov 03 '11 at 00:28
  • This has been solved with a Autoconf macro here http://stackoverflow.com/questions/8585110/how-can-i-specify-that-i-want-c0x-in-makefile-am . The advantage there is that it wont fail if you don't have C++ 11 compiler support whereas AC_CHECK_COMPILE_FLAG will stop the build when fail. –  Jan 03 '13 at 09:00
  • How can this be a duplicate if it has been asked before? – lvella Oct 29 '15 at 01:12

4 Answers4

19

Have you checked ax_cxx_compile_stdcxx_11 ?

I think this is exactly what you want.

There is a big macro library on gnu website.

Jaffa
  • 12,442
  • 4
  • 49
  • 101
  • 4
    This is most likely the right solution. People must just remember to install the archive package `autoconf-archive` in Debian, and run `aclocal` before running `autoconf` (to pull the required macros into `aclocal.m4`). – carandraug Nov 27 '14 at 18:58
  • This is a better solution than mine, which didn't exist when I originally answered this question. – Flexo Mar 25 '16 at 15:52
  • @carandraug I'd appreciate it if you or the OP could spell out more details. What is the correct installation and syntax for use? Before installing autoconf-archive I would always get a syntax error (not a "not found") error at the call in configure.ac. After installing autoconf-archive I get "syntax error: near unexpected token `ac_config_headers="$ac_config_headers config.h"' – garyp Apr 15 '16 at 12:54
  • 1
    @garyp my recommendation is to create a `m4` directory in project and copy that macro into it. Then before calling `autoconf`, call `aclocal -Im4`. Many other projects provide their own autoconf macros (pkg-config for example), and it's expected that you copy them to your project. You should only expect contributors to have autoconf itself and the macros it comes with. – carandraug Apr 15 '16 at 13:35
  • @carandraug Thanks for that good idea. Does some future person need to execute aclocal? Or will reconfigure figure things out? BTW, after trial and error and searching, I found the correct syntax for me is AX_CXX_COMPILE_STDCXX_11([noext], [mandatory]). I had a hard time figuring that out. – garyp Apr 15 '16 at 14:02
17

You can do this with something like AX_CHECK_COMPILE_FLAG, e.g.:

AX_CHECK_COMPILE_FLAG([-std=c++0x], [
                        CXXFLAGS="$CXXFLAGS -std=c++0x"])

(You need to be careful here that AC_LANG is C++, not C at the point this is called because it's possible to use gcc for C and something else for C++ or vice versa).

Flexo
  • 87,323
  • 22
  • 191
  • 272
  • 1
    This is probably not sufficient: if the compiler does not support -std==c++0x then configure will succeed but the build will fail. – William Pursell Nov 04 '11 at 12:36
  • +1 though, since it is easy enough to add a third argument with AC_MSG_ERROR – William Pursell Nov 04 '11 at 12:59
  • 1
    @WilliamPursell - the more useful thing might be to use AC_DEFINE in conjunction with this so that the build can still proceed, just with different implementations of some parts in exactly the same way I handle missing `snprintf` in C today. That may be less features exposed or exposed through an alternative interface, but it's probably not a show stopper at this point. – Flexo Nov 04 '11 at 13:15
  • 2
    Excellent suggestion, especially since the OP is just beginning to use features. This is certainly the right time to use features conditionally! – William Pursell Nov 04 '11 at 13:48
5

I think the simplest way to do this is to add:

CXXFLAGS="$CXXFLAGS -std=c++0x"

in configure.ac before AC_PROG_CXX. If the compiler does not accept -std=c++0x, then configure will fail with "C++ compiler cannot create executables". It is not the best error messages, but it ensures that builds will succeed if configure succeeds. For a better error message, you can check that the compiler accepts the flag after AC_PROG_CXX. In either case, you want configure to fail if the compiler does not provide the necessary features but your software requires it.

Note that setting CXXFLAGS before AC_PROG_CXX has the undesirable side effect of preventing the default setting for CXXFLAGS in the case that the user does not set that variable when running configure. For this reason, it is normally not recommended to set CXXFLAGS in the configury, so it is probably better to check the flag after AC_PROG_CXX (eg using awoodland's solution)--just make sure you add an AC_MSG_ERROR in the third argument of AX_CHECK_COMPILE_FLAG so that configure fails if the features are not available.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
1

To enable the compiler switch (unless, of course, the user overrides it), put this in your Makefile.am:

AM_CXXFLAGS=-std=c++0x

I don't think there's a check available for the presence of C++11 features, but you should be able to write a test program fairly easily with the features you want to use, that will fail if those features are not supported. Then you can write a test as described in this section of the Autoconf manual.

ptomato
  • 56,175
  • 13
  • 112
  • 165