11

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

ildjarn
  • 62,044
  • 9
  • 127
  • 211
NebulaFox
  • 7,813
  • 9
  • 47
  • 65
  • 1
    Is your C++03 compiler and standard library binary compatible with your C++11 compiler and standard library? –  Sep 28 '12 at 09:56
  • @hvd - point taken; I've deleted my prior comment. Thanks. – mah Sep 28 '12 at 10:00
  • You mention the compiler you're using now, but not the standard library. clang can be used at least with GCC's libstdc++ and [libc++](http://libcxx.llvm.org/), I don't know if there are others. –  Sep 28 '12 at 10:33
  • I think it would make more sense to use libc++ – NebulaFox Sep 28 '12 at 10:34
  • In that case, I do know some of the incompatibilities that could cause problems with libstdc++ don't apply, but I don't know for sure whether there could be other problems. –  Sep 28 '12 at 10:39

2 Answers2

16

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

Richard Smith
  • 13,696
  • 56
  • 78
  • ABI compatibility issues can't be entirely avoided by keeping them out of the interface. Another place they can appear is if some other type which appears in the interface internally stores an ABI incompatible standard library object. – bames53 Nov 29 '12 at 22:21
  • I was hoping "does not involve standard library types" would cover that case, but I'll edit the answer if you think that's not clear. – Richard Smith Dec 04 '12 at 08:33
0

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.