Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 396
NebulaFox Avatar asked Sep 28 '12 09:09

NebulaFox


1 Answers

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

like image 196
Richard Smith Avatar answered Sep 25 '22 20:09

Richard Smith