Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 backwards compatibility

Tags:

Is there anything from c++11 that I can use and expect compiled binaries to run on older systems? How can I tell which parts of c++11 are a part of the libstdc++.so and what actually gets compiled into the binary? Maybe I don't fully understand how such things work. Does c++11 break ABI compliance with older libraries?

An example for clarity:

Say I compile a program that uses auto and the new range based for loop. These things seem like they should work on systems with old c++ runtimes because they break down into c++ that would be valid on those machines. However, using something like regex should break on older systems because it won't be in their runtime.

I haven't tested my theory yet because I don't have access to a c++11 compatible compiler, and my google searches haven't been helpful.

like image 252
Brian Schlenker Avatar asked Nov 01 '13 15:11

Brian Schlenker


People also ask

Is C ++ 11 backwards compatible?

You are correct. The code itself compiles to work on any platform with a C++ compiler. The new language constructs (ranged for , new keywords like auto , etc.) compile with a new compiler to work just fine on older systems.

Is C ++ 11 backwards compatible with C++ 98?

Of course, C++11 is almost fully backwards compatible with C++98 and wxWidgets can be compiled with a C++11 compiler (e.g. under Unix, just pass CXX="g++ -std=c++11" to configure) but this is not very interesting.

Is C backward compatible?

C standards are not backward compatible - code written under the latest standard is not guaranteed to build with compilers that only support earlier versions.

Is G ++ backwards compatible?

In order to allow compilation of C++ written to such drafts, G++ contains some backwards compatibilities. All such backwards compatibility features are liable to disappear in future versions of G++. They should be considered deprecated.


1 Answers

You are correct. The code itself compiles to work on any platform with a C++ compiler. The new language constructs (ranged for, new keywords like auto, etc.) compile with a new compiler to work just fine on older systems.

However, if you try to link code that uses new symbols (like regex) to an old library, then you run into problems, no matter whether you link statically or dynamically, because the old library does not have the new symbols.

Assuming your code uses new symbols:

  • If you statically link to an old library, the linkage will fail because the new symbols do not exist in the old library.
  • If you dynamically link to an old library, you should still get linker problems, again because the library does not contain the new symbols.
  • If you statically link to a new library, the resulting binary will work on an older system.
  • If you dynamically link to a new library, the resulting binary will work on an older system if it already has the new library or if you distribute the new library with your binary.
    • But if you then try to replace the new dynamic library with an old dynamic library, it will not be able to link to the new symbols in that library, again because they aren't there.
like image 93
Jed Schaaf Avatar answered Sep 23 '22 20:09

Jed Schaaf