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

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

问题描述

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

我的应用程序链接到一些使用g ++-4.7编译的共享库,但没有

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.

不幸的是,没有任何效果,这意味着使用外部库类和方法时会有一些奇怪的行为。 (当然,在不使用-std = c ++ 0x的情况下编译我的应用程序也可以正常工作)。

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. 这是预期的行为还是

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

有什么解决方法(类似于 extern C关键字)?

Any workaround (something like the extern C keyword)?


推荐答案

标准库已更改,并且 -std = c ++ 0x 编译器标志将确定正在使用库的哪个部分。通过尝试在同一程序中使用两个版本,您正在打破一个定义规则(对于标准库中的每个已使用元素,您都具有相同标识符的两个定义)。

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

我认为没有什么简单的方法可以克服此限制。您将必须确保只使用该库的一个版本(即,在包含标准头以在这些库中禁用C ++ 11之前定义适当的宏),即使如此,我仍然不确定所生成的代码是否仍不会打破ODR(如果C ++ 11扩展对C ++ 03库代码的编译方式不同)。

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天全站免登陆