5

so, C++11 has been around for a while and, given there already are compilers supporting it on most platforms, it would be nice to use it in some real software -- e.g. one that can be packaged in as-portable-as-possible package, preferably providing ./configure and so.

Because both Clang and GCC currently need -std=c++11 flag to compile c++11 source, and both sometimes require specific flags to work correctly (see for example How to compile C++11 with clang 3.2 on OSX lion? or C++11 Thread not working ), I'm quite afraid that the package won't work on some platforms that already support c++11 because of wrong invocation of compiler.

Q: Is there some standard how to correctly and portably compile c++11? E.g. autotools/autoconf check or some list of compiler/platform directives that describe all possible needed options? Or does the situation come from the fact that c++11 standard implementations are currently marked as "experimental" and the standard will eventually stabilize and become the default choice, not needing any usage of extra compiler flags?

Thanks

-exa

Community
  • 1
  • 1
exa
  • 859
  • 7
  • 27
  • Maybe [Buck](http://facebook.github.io/buck/) supports it? – Kerrek SB May 28 '13 at 08:46
  • well, "android build tool" doesn't really count for "portable"... – exa May 28 '13 at 08:49
  • 1
    Can't you explicitly add `-std=c++11` into your `Makefile.in` or `Makefile.am` line for `CXXFLAGS`? – Basile Starynkevitch May 28 '13 at 08:50
  • Of course I can, but that doesn't solve the other needed cxxflags (e.g. -pthread or -stdlib from the links) and possible change of standard. – exa May 28 '13 at 08:54
  • Are you sure other cxxflags are needed? – Basile Starynkevitch May 28 '13 at 09:22
  • @exa: It's a universal build tool. It's just pioneering on Android. – Kerrek SB May 28 '13 at 11:16
  • 1
    While not integrated with `autoconf`, there's a interesting macro in the `autoconf archive` for [C++11 testing / flags](http://git.savannah.gnu.org/gitweb/?p=autoconf-archive.git;a=blob_plain;f=m4/ax_cxx_compile_stdcxx_11.m4). It's inadequate compared to the AC_PROG_CC_C99 macros, etc. This is something I'd really like to see autoconf support for if you wish to discuss it further. – Brett Hale May 31 '13 at 07:01

2 Answers2

1

I`m using CMake to generate Makefiles of C++11 projects. The only change in CMakeLists.txt I need to do is add the following:

ADD_DEFINITIONS("-std=gnu++11")
ADD_DEFINITIONS("-D_GLIBCXX_USE_C99_STDINT_TR1")
ADD_DEFINITIONS("-D_GLIBCXX_HAS_GTHREADS")

However, as I use Qt, I re-compile QtSDK with a new gcc version 4.8 and get a complete mingw system that use gcc in version 4.8.

Makings these changes, the project compile and run in Windows XP, Windows 7 and linux both 32 and 64 bits. I didn`t test it in OSX yet.

Celino
  • 325
  • 1
  • 9
  • That would work, even without CMake, IF only there weren't the exceptions and compatibility problems e.g. from the questions I linked. Which is what I'm trying to solve. – exa Jun 15 '13 at 13:44
1

Well, if you`re trying to write portable code, i would recommend using cmake a very powerful cross-platform, open-source build system.

Using cmake you should be able to identify the compilers available in your current machine and then generate your makefiles using the flags that you want in each case.

I have been using cmake for almost a year by now and it has significantly reduced the time consumed when trying to get a project compiling in different platforms.

flazzari
  • 173
  • 5
  • This doesn't really answer the question how to test whether the compiler is c++11-capable and/or add needed compatibility workarounds. – exa Jun 15 '13 at 13:41
  • I don't think there is a automated way of test whether the compilier is c++11-capable and find the flags needed. You could find out which flags are needed to every compiler that you will need to support, then write a conditional statement in your cmakelists to use the appropriated flag depending on your compiler. – flazzari Jun 18 '13 at 21:05