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?
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).
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 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.
autoconf-archive
in Debian, and run aclocal
before running autoconf
(to pull the required macros into aclocal.m4
). –
Civet 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 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).
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 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.
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.
© 2022 - 2024 — McMap. All rights reserved.