C++11 backwards compatibility

后端 未结 2 815
礼貌的吻别
礼貌的吻别 2020-12-28 15:53

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

2条回答
  •  半阙折子戏
    2020-12-28 16:26

    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.

提交回复
热议问题