如果我有用C++03编写的库,并将其编译成静态库,那么我可以在C++11中使用它吗?也是反向可能的(带有C++11静态库的C++03 )。
更新:我使用的编译器是clang或LLVM。
发布于 2012-09-29 04:52:14
这主要取决于如何在库中使用C++标准库。
libstdc++,则可能会遇到一些问题:- 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.
- 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模式之间的二进制兼容。libc++,在程序中使用libstdc++,或者在程序中使用libstdc++,那么大多数不兼容将在链接时捕获。(libc++在namespace std中使用包含其大部分符号的inline namespace,如果您试图跨边界传递libstdc++的类型,则会导致链接时间不兼容)。但是,如果库的接口间接包含标准库类型(例如,如果它使用具有标准库类型的struct作为成员),则可能仍然存在运行时问题。对于libc++不进行版本的类型,它的目标是二进制兼容libstdc++ (在C++98和C++11模式下)。发布于 2012-09-28 10:08:57
取决于编译器。例如,GCC破坏了在C++11中在C++11模式下不同变化的标识符。因此,例如,如果您不使用诸如std::list之类的东西,那么您就没事了。
https://stackoverflow.com/questions/12637699
复制相似问题