C ++ 03库和C ++ 11源代码 [英] C++03 library with C++11 source code

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

问题描述

如果我有在C ++ 03中编写的库,并将其编译为静态库,那么我可以在C ++ 11中使用它吗?也是相反的可能(C ++ 11静态库与C ++ 03)。

If I have library that was written in C++03 and I compile it to a static library, can I then use it in C++11? Also is the reverse possible ( C++11 static library with C++03 ).

更新:
编译器I am using is clang or LLVM

Update: The compiler I am using is clang or LLVM

推荐答案

这主要取决于你如何在库中使用C ++标准库。

It depends primarily on how you use the C++ standard library in your library.


  • 如果您根本不使用它,那么您不太可能遇到任何问题。

  • If you don't use it at all, then you are unlikely to encounter any problems.

如果您使用 libstdc ++ ,那么您可能会遇到一些问题:

If you use libstdc++, then you may encounter some issues:


  • 将标准库对象传入和传出库并不总是有效(例如,C ++ 11模式中的 std :: list 因为它正在增加一个 size 数据成员,以及 std :: string / code>正在更改为非引用计数的值)。 g ++开发人员有一个计划,引入一种形式的符号污染在链接时捕获这些问题,所以如果你碰到任何有问题的情况下,你会得到错误,但这还没有在g ++中实现,可能永远不会实现铛。

  • Passing standard library objects to and from your library will not always work (for instance, std::list in C++11 mode will eventually be larger than it currently is in C++98 mode, because it is growing a size data member, and the representation of std::string is changing to a non-reference-counted one). The g++ developers have a plan to introduce a form of symbol tainting to catch these issues at link time, so you will get errors if you hit any of the problematic cases, but this has not been implemented yet in g++ and may never be implemented in Clang. You can avoid this problem by ensuring that your library's interface does not involve standard library types.

某些符号可能会改变含义(例如 std :: complex :: real std :: complex :: imag 在C ++ 98模式下返回引用, C ++ 11模式,由于 constexpr 缺陷)。如果使用C ++ 98和C ++ 11形式链接到一起(未优化的)代码,可能会选择错误的实现,在运行时会出现奇怪的结果。

Some symbols may change meaning (for instance, std::complex::real and std::complex::imag return references in C++98 mode, but return by value in C++11 mode, due to a constexpr deficiency). If you link together (unoptimized) code using both the C++98 and C++11 forms, you may have the wrong implementation chosen, with strange results at runtime.


  • 如果您使用 libc ++ ,您不应该看到任何问题。 libc ++ 旨在在C ++ 98和C ++ 11模式之间进行二进制兼容。

If you use libc++, you should not see any issues. libc++ was designed to be binary-compatible between C++98 and C++11 modes.

如果在程序中使用库中的 libc ++ ,反之亦然,如果使用 libstdc ++ 在链接时。 ( libc ++ 中使用内联命名空间的符号,如果你尝试通过边界传递 libstdc ++ 的类型,导致链接时不兼容)。但是,如果库的接口间接包含标准库类型,您可能仍然遇到运行时问题(例如,如果它使用 struct 库类型作为成员)。对于 libc ++ 不是版本的类型,它的目标是与 libstdc ++ 98和C ++ 11模式)。

If you use libc++ in the library and libstdc++ in the program, or vice versa, then most incompatibilities will be caught at link time. (libc++ uses an inline namespace within namespace std containing most of its symbols, causing link-time incompatibilities if you try to pass libstdc++'s types across the boundary). However, you may still have runtime problems if your library's interface indirectly contains standard library types (for instance, if it uses a struct which has a standard library type as a member). For the types which libc++ does not version, it aims to be binary-compatible with libstdc++ (in both C++98 and C++11 modes).

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

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