8

This is a very uninformed question, but:

I would like to start using C++11. Can I continue to use my large collection of libraries which were compiled with my old gcc 4.2.1 compiler or do I need to recompile them all with a new compiler? I would think (or hope) the answer is no, but I am only a dabbler.

So that I may have at least part of my ignorance removed, can you explain why in either case?

Thanks

user487100
  • 918
  • 1
  • 11
  • 22
  • 2
    No... Objects are objects. If that was the case then everything would have to be compiled with the same compiler. Object formats can change, but the linker is a separate issue from the compiler. Also see: http://gcc.gnu.org/onlinedocs/gcc/Compatibility.html – EdH Feb 23 '12 at 07:18
  • IIRC, gcc had to introduce a breaking change to support the C++11 requirements for `std::list` - it added a data member. I don't know what gcc version added it (before or after 4.2.1), and you probably don't use `std::list` anyway. But once there's one breaking change you have to worry that there will be others. – Steve Jessop Feb 23 '12 at 09:33

5 Answers5

9

Yes, you should.

The weaker reason is not binary compatibility, the problem is about expectations. A C++11 enabled compiler will expect a number of features to be available (move constructors among them) and use them when appropriate. And that's only scratching the tip of the iceberg, there are several other incompatibilities (auto, 0 and its interaction with pointers, ...).

It means that any inline method in a header may suddenly be interpreted differently, in light of the C++11 Standard.

The stronger reason is that each version of the compiler comes with its own Standard Library implementation. You don't really want start mixing various versions around and especially not when they have undergone such major changes (once again, rvalue references...).

Believe me, it's simpler to recompile now rather than having that nagging thought that each bug that appear may be due to an incompatibility between old and new libraries...

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • I think because of inlines you are actually compelled to recompile. Since some semantics in the inline can change from what the library has, the one definition rule would actually be violated if you don't recompile. – edA-qa mort-ora-y Feb 23 '12 at 09:41
  • @edA-qamort-ora-y: I don't know. ODR is about tokens, and the tokens did not change... but then the Standard probably does not worry much about various compilers, it's an implementation detail... so :x At least, with the Standard Library, it's much more clear-cut. – Matthieu M. Feb 23 '12 at 10:15
  • But the meaning of the tokens could have changed. The introduction of move means that code actually does something differently now. Consider that a signature `void func( vector a)` while the same tokens, actually has a different meaning now (give the move constructor). Thus I'd say this is different function and thus violating the ODR. I suppose this is just a fine point, and in practice mixing version is probably not going to work for several reasons as you mention. – edA-qa mort-ora-y Feb 23 '12 at 11:44
  • @edA-qamort-ora-y: we agree that it may have changed, but the wording of ODR is based on tokens regardless. – Matthieu M. Feb 23 '12 at 12:11
3

The answer totally depends on the API of your library and its implementation dependencies.

The conditions that guarantee that you don't need to recompile are:

-- Your library doesn't use C++ specific features in its public API.

That implies:

  1. Your library doesn't provide classes/class-templates/function-templates/other C++ specific stuff.

  2. You don't accept/return C++ classes to/from your library functions.

  3. You don't pass function parameters by reference.

  4. You don't provide public inline functions with C++ specific implementations.

  5. You don't throw exceptions from your functions.

  6. You don't (because you have no reason to) include C++ specific library headers in your public headers. (It wouldn't hurt if you do include them but everything must be OK if you remove such includes. It is like an indicator.)

-- Your library depends only on the libraries that binary-compatible with those available in your new build environment.

If those conditions are not satisfied then there is no guarantee that your library will work correctly. In most cases it is much easier to recompile than to make sure that everything works correctly.

Either way if you are going to make binary compatible API that satisfies the conditions above then it is much better to design and implement C language API. This way you automatically satisfy the above conditions and don't fall into sin of writing C-style C++ code.

Community
  • 1
  • 1
Serge Dundich
  • 4,221
  • 2
  • 21
  • 16
3

It's a compiler question. For instance, if you have a single compiler which supports both C++03 and C++11 depending on a compiler switch, you can very likely mix libraries. There's nothing new in C++11 that forces an incompatibility with C++03.

Howeve, you mentioned that your libraries were compiled with GGC 4.2.1. Since C++11 was just an idea back then, it's quite likely that GCC back then was implemented in ways that turned out to be incompatible with C++11.

For instance, std::list::size() must be O(1) in C++11, but it could be O(N) in C++03. GCC chose a O(N) implementation back then, not knowing the future requirements. The current GCC std::list::size implementation is compatible with both C++11 and C++03, since O(1) is better than O(N).

MSalters
  • 173,980
  • 10
  • 155
  • 350
1

You can use large parts of C++11 without recompiling (assuming ABI compatability), but a particular important part, for me atleast, will not be accessible to the already compiled code - move semantics.

Move semantics can make your code faster just by recompiling with a C++11 compiler (and preferably, a C++11 stdlib).

There are also other reasons. Maybe your preferred library has become C++11 aware since you last compiled it, and is now more efficient, safer or easier to use if compiled with a C++11 compiler?

For your own libraries, with C++11 you surely can make it them more efficient, safer and easier to use? :)

Xeo
  • 129,499
  • 52
  • 291
  • 397
1

If your interfaces to the compiled code uses any template which was modified by C++11, then yes, you have to recompile. Otherwise you probably can continue to use your old libraries (unless the compiler provider also decided to make changes of the ABI at the same time, because it's a great opportunity to fix long-standing ABI bugs which are otherwise often refrained from fixing because of binary incompatibilities).

celtschk
  • 19,311
  • 3
  • 39
  • 64