C ++ 11向后兼容 [英] C++11 backwards compatibility

查看:158
本文介绍了C ++ 11向后兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么从c + + 11,我可以使用和期望编译的二进制文件在旧系统上运行?我怎么能知道c ++ 11的哪些部分是libstdc ++的一部分,所以和什么实际上被编译成二进制?也许我不完全明白这样的事情如何工作。

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?

清楚的例子:

说我编译一个使用auto和基于for循环的新范围的程序。这些东西看起来像他们应该在具有旧的c ++运行时的系统上工作,因为它们分解成在这些机器上有效的c ++。但是,使用正则表达式之类的东西应该在旧系统上断开,因为它不会在其运行时。

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.

我还没有测试我的理论,因为我没有访问一个c ++ 11兼容的编译器,我的google搜索没有帮助。

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.

推荐答案

代码本身编译为使用C ++编译器在任何平台上工作。新的语言构造(范围 ,像 auto 等新关键字)使用新的编译器编译工作

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.

但是,如果您尝试将使用新符号(如regex)的链接代码添加到旧库,

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:


  • 如果您静态链接到旧库,则链接将失败,因为旧库中不存在新符号。

  • 如果您动态连结到旧的图书馆,您仍然应该得到连结问题,因为图书馆不包含新符号。

  • 如果您静态地链接到新库,则生成的二进制文件将在旧系统上运行。

  • / em>链接到一个新的库,生成的二进制将工作在一个旧的系统,如果它已经有新的库,或者如果您分发新的库与您的二进制文件。

    • 但是如果你尝试用一个旧的动态库替换新的动态库,它将无法链接到该库中的新符号,再次因为它们

    • 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.

      这篇关于C ++ 11向后兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆