C ++ 11和C ++ 03之间的库兼容性 [英] Libary compatibility between C++11 and C++03

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

问题描述

我在C ++ 11中开发一个应用程序,使用g ++ - 4.7和-std = c ++ 0x。

我的应用程序链接到一些使用g ++编译的共享库 - 4.7,但没有-std = c ++ 0x指令。



不幸的是,没有什么作用,意味着当使用外部库类和方法时,我有一些奇怪的行为。 (当然编译我的应用程序没有-std = c ++ 0x工作正常)。


  1. 这是一个预期的行为,编译器错误?


  2. 任何解决方法(类似于 extern C关键字) c ++ 0x 编译器标志将确定库的什么部分正在使用。通过尝试在同一程序中使用这两个版本,您打破了单一定义规则(对于标准库中的每个使用的元素,您对同一标识符有两个定义)。



    我不认为有什么简单的可以做到克服这个限制。你必须确保你只使用一个版本的库(即在包含标准头文件之前定义相应的宏以禁用这些库中的C ++ 11),甚至我不能确定生成的代码仍然不会打破ODR(如果C ++ 11扩展编译C ++ 03库代码不同)。


    I am developing an application in C++11, using g++-4.7 and -std=c++0x.
    My app is linked against some shared library compiled with g++-4.7, but without the -std=c++0x directive.

    Unfortunately, nothing works, meaning that I have some strange behaviour when using the external library classes and methods. (Of course compiling my app without -std=c++0x works fine).

    1. Is this an expected behaviour or it's a compiler bug?

    2. Any workaround (something like the extern C keyword)?

    解决方案

    The standard library has changed, and the -std=c++0x compiler flag will determine what part of the library is in use. By trying to use both versions in the same program you are breaking the One Definition Rule (for each used element in the standard library you have two definitions for the same identifier).

    I don't think there is anything simple that can be done to overcome this limitation. You would have to ensure that you only use one version of the library (i.e. define the appropriate macros before inclusion of standard headers to disable C++11 inside those libraries), and even then I am not sure that the generated code would still not break the ODR (if the C++11 extensions compile the C++03 library code differently).

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

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