C++03 library with C++11 source code

房东的猫 提交于 2019-11-27 16:19:14

问题


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

Update: The compiler I am using is clang or LLVM


回答1:


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.

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

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

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

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




回答2:


Depends on the compiler. GCC, for example, mangles the identifiers whose ABI changed in C++11 differently in C++11 mode. So, for example, if you don't use things such as std::list, then you're fine.



来源:https://stackoverflow.com/questions/12637699/c03-library-with-c11-source-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!