10

I've looked hard for this question - it seems an obvious one to ask - but I haven't found it: Is a module compiled with "-std=c++11" (g++) binary compatible with modules that are not compiled with the option? (That is, can I link them together safely?) Both compilations would use the exact same version of g++.

To be more precise, using gcc 4.9.0, can I only use the "-std=c++11" on specific compilation units and then let the others compile without the option.

John
  • 2,326
  • 1
  • 19
  • 25
  • I expect it won't, but it's hard to be 100% sure. Any reason you can't just recompile it all? – Mats Petersson May 26 '14 at 16:35
  • well, C++11 is not compatible with previous standards, I'm not sure about the ABIs or the binaries, but I wouldn't do that anyway . If you are going for C++11 you should use C++11 only. – user2485710 May 26 '14 at 16:35
  • I once had [a similar question](http://stackoverflow.com/q/10717106/596781). – Kerrek SB May 26 '14 at 16:48

1 Answers1

9

An authoritative reference can be found in gcc's C++11 ABI Compatibility page.

The short summary is: the are no language reasons the ABI gets broken but there are a number of mandated changes which cause the standard C++ library shipping with gcc to change.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • for example http://stackoverflow.com/questions/23047198/can-c-code-be-valid-in-both-c03-and-c11-but-do-different-things – user2485710 May 26 '14 at 16:39
  • unless you enjoy using the deprecated `auto` before 11, in which case they should crawl out of their cave. – Syntactic Fructose May 26 '14 at 16:42
  • 1
    @user2485710: Note that ABI changes and changed behavior are two different things. Although, changed behavior is a problem, changing ABIs can be seen as a bigger problem, especially if the ABI is changed in ways causing the result to still link. – Dietmar Kühl May 26 '14 at 16:42
  • I honestly think that if 2 specifications of the same language are designed to do different things, it doesn't really matter if you keep ABI compatibility or not, you are basically getting into troubles either way. – user2485710 May 26 '14 at 16:44
  • I'm sure I'm just being dense here, but your answer confuses me. Is it safe - in gcc 4.9.0, for instance - to change just one of a .cpp's compile flags to "-std=c++11" and leave the others alone. – John May 26 '14 at 17:07
  • 1
    @John: according to the linked page, if you do not use anything from standard C++ library in the different teanslation units it should be OK. The moment you touch anything touch the standard C++ library you'll need to be rather careful about what you are doing. The simple answer is: do not mix object files built with different option. This may not be viable, though, and the moment youmix build options there are no simple answers. – Dietmar Kühl May 26 '14 at 17:17
  • 3
    @user2485710: sadly, with large code bases it isn't always possible to use consistent setting. In these cases it certainly matters what works rogether and what doesn't. – Dietmar Kühl May 26 '14 at 17:20
  • @DietmarKühl yes, indeed, we are also focusing just on the standard itself, in practice there are techniques that can break ABI compatibility even if the standard says otherwise, like playing with the frame pointer. So you can't even be sure about the compatibility of a given binary just because the standard says so. – user2485710 May 26 '14 at 17:23
  • @Dietmar: I think I get it now. Under "-std=c++11" parts of the standard library have different components than they do when "-std=c++11" is *not* specified. – John May 26 '14 at 17:24
  • @John in the case of `gcc` the default actions depend on the specs or the configuration used, for example `g++ -dumpspecs` is one bit of the default configuration, you also need to take into account the configuration used to build `gcc` itself and probably other variables that you can master after spending some time with the `gcc` sources . The default version of the language doesn't change that often, but it's just part of the original gcc configuration, nothing special about that, if you don't specify anything the defaults kick in. – user2485710 May 26 '14 at 17:32