Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 language support in g++ without the library-breaking features of `-std=c++11`

Tags:

c++

c++11

g++

Is there a way to tell g++ to enable the new language features of C++11 without any breaking changes to the standard C++ library due to ABI modifications?

Adding the -std=c++11 compilation flag tells g++ to enable both the language and the library features, but object files created this way cannot be safely linked with those that used a different -std= setting. I'd like to be able to use language enhancements like rvalue references, move constructors (for my own classes), and the auto keyword in code that's linked against C++03 libraries.

EDIT:

I'm interested in having g++ enable its C++11 language features, but I want it to parse, compile, and link against the old C++03 libraries. I don't want it to use the C++11 version of the standard library. This means that in my own code, I'll be able to use auto, range foreach constructs, rvalue references, etc., but I won't be able to use the new C++11 features in the standard C++ library like std::move or rvalue-ref enhancements to the STL containers. The reason for not wanting the C++11 version of the standard library is that the layout of various objects has changed, so it's invalid to link two object files that expect different versions of the standard library into the same binary.

like image 252
Mr Fooz Avatar asked Mar 11 '13 21:03

Mr Fooz


People also ask

Does GCC 11 support C++ 20?

GCC has experimental support for the latest revision of the C++ standard, which was published in 2020. C++20 features are available since GCC 8. To enable C++20 support, add the command-line parameter -std=c++20 (use -std=c++2a in GCC 9 and earlier) to your g++ command line.

Does GCC support C++11?

According to cppreference, full support of c++11 came with gcc 4.8. 1; To have full support of c++14 (with some of the new features of c++17), instead, you need gcc 5.0 and above.

What version of C++ does g ++ support?

The g++ utility supports almost all mainstream C++ standards, including c++98 , c++03 , c++11 , c++14 , c++17 , and experimentally c++20 and c++23 . It also provides some GNU extensions to the standard to enable more useful features.

What is STD C ++ 0x?

C++0x was the working name for the new standard for C++, adding many language features that I'll cover in this series on C++11. In September 2011, C++0x was officially published as the new C++11 standard, and many compilers now provide support for some of the core C++11 features.


1 Answers

No, you can't enable C++11 language features without C++11 library features (not without editing the libstdc++ headers to remove all the C++11 parts.)

But there aren't many incompatible symbols (as long as you don't use 4.7.0 or 4.7.1 which had an incompatible std::list, reverted for 4.7.2) so you probably only need to worry about the erase() members of the RB-tree containers. You could ensure the C++11 version of the symbol is defined in your main executable, so that version of the symbol will be used by all code that needs it. Code in other libraries expecting the C++03 versions will ignore the return value, code expecting the C++11 versions will be able to use the return value.

like image 134
Jonathan Wakely Avatar answered Oct 12 '22 23:10

Jonathan Wakely