Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Library compatibility between C++11 and C++03

Tags:

c++

c++11

I am developing an application in C++11, using g++-4.7 and -std=c++0x.
My app is linked against some shared library compiled with g++-4.7, but without the -std=c++0x directive.

Unfortunately, nothing works, meaning that I have some strange behaviour when using the external library classes and methods. (Of course compiling my app without -std=c++0x works fine).

  1. Is this an expected behaviour or it's a compiler bug?

  2. Any workaround (something like the extern C keyword)?

like image 427
sbabbi Avatar asked Apr 04 '12 15:04

sbabbi


1 Answers

The standard library has changed, and the -std=c++0x compiler flag will determine what part of the library is in use. By trying to use both versions in the same program you are breaking the One Definition Rule (for each used element in the standard library you have two definitions for the same identifier).

I don't think there is anything simple that can be done to overcome this limitation. You would have to ensure that you only use one version of the library (i.e. define the appropriate macros before inclusion of standard headers to disable C++11 inside those libraries), and even then I am not sure that the generated code would still not break the ODR (if the C++11 extensions compile the C++03 library code differently).

like image 88
David Rodríguez - dribeas Avatar answered Nov 02 '22 02:11

David Rodríguez - dribeas