How to use C++11 features with Autoconf? [duplicate]
Asked Answered
M

4

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?

Monde answered 2/11, 2011 at 22:12 Comment(3)
Unless you want to use GCC extensions, the switch you want to use is "-std=c++0x".Adeline
This has been solved with a Autoconf macro here #8585610 . 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.Dunstable
How can this be a duplicate if it has been asked before?Monde
B
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).

Boomerang answered 3/11, 2011 at 13:43 Comment(4)
This is probably not sufficient: if the compiler does not support -std==c++0x then configure will succeed but the build will fail.Miscellany
+1 though, since it is easy enough to add a third argument with AC_MSG_ERRORMiscellany
@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.Boomerang
Excellent suggestion, especially since the OP is just beginning to use features. This is certainly the right time to use features conditionally!Miscellany
K
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.

Kolo answered 20/1, 2013 at 12:11 Comment(5)
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).Civet
This is a better solution than mine, which didn't exist when I originally answered this question.Boomerang
@Civet 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"'Chess
@Chess 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.Civet
@Civet 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.Chess
B
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).

Boomerang answered 3/11, 2011 at 13:43 Comment(4)
This is probably not sufficient: if the compiler does not support -std==c++0x then configure will succeed but the build will fail.Miscellany
+1 though, since it is easy enough to add a third argument with AC_MSG_ERRORMiscellany
@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.Boomerang
Excellent suggestion, especially since the OP is just beginning to use features. This is certainly the right time to use features conditionally!Miscellany
M
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.

Miscellany answered 4/11, 2011 at 12:47 Comment(0)
A
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.

Asthenia answered 3/11, 2011 at 22:22 Comment(1)
If the test will fail without C++11 features, then the flag will need to be in CXXFLAGS or configure will always fail.Miscellany

© 2022 - 2024 — McMap. All rights reserved.