7

I'm attempting to switch a large project to using C++11. I ran into large number of linker errors which seem caused by the mismatched namespace on STL classes between library compiled with C++11 and those compiled with C++03.

As an example, say library B is a dependency of A. B has following templated class as part of its interface.

template <class Type>
class VectorParameter
{
public:
    VectorParameter();
    virtual ~VectorParameter();

    ...
}

Library A instantiates the template with VectorParameter<std::pair<float, float>>.

When I recompiled A with C++11 without recompiling B, I ran into linker error that complains that

LFE::VectorParameter<std::__1::pair<float, float>>::~VectorParameter() is undefined symbol.

I think the issue here is that library A uses std::__1::pair while B still uses std::pair. Following this reasoning, I assume that I will need to recompile all dependency libraries that refers STL types in their interfaces.

If this is the case, then migrating a large project to C++11 will require all involved groups to switch at the same time, which doesn't seem very practical on a complex project. What would be the best practice for dealing this issue?

murrekatt
  • 5,961
  • 5
  • 39
  • 63
mr49
  • 1,053
  • 1
  • 8
  • 26
  • 3
    I would rather be safe that sorry - I.e. recompile the lot. It is a on e off event. – Ed Heal Jul 11 '14 at 16:47
  • 2
    In all likelihood, the standard library ABI will be different between C++03 and C++11. I wouldn't even attempt to do this without throwing the C++11 switch on all dependencies involved. – Praetorian Jul 11 '14 at 17:05
  • 2
    In general, all libraries must be compiled with very similar if not identical flags. And the C++11 is kind of a big flag... – Mooing Duck Jul 11 '14 at 17:12
  • 3
    Possibly duplicate of: http://stackoverflow.com/questions/10014042/libary-compatibility-between-c11-and-c03 – sbabbi Jul 11 '14 at 17:18
  • OSX? Linux? g++ or clang++? – murrekatt Jul 11 '14 at 17:46
  • 1
    You should rebuild everything that relies on the STL. But given the code you posted, I would like to know if you've put the template declarations in a header and the definitions in an implementation file. That would also give you the "undefined symbol" error; and the fix is to either put both declaration/definition in the header or to use extern templates (C++11 feature: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1987.htm ). – Max Lybbert Jul 11 '14 at 17:50
  • This is on OSX using clang. The template is entirely in header. – mr49 Jul 11 '14 at 18:00

2 Answers2

9

It is almost certain that the library header files have changed, therefore to remain in compliance with the One Definition Rule you must recompile everything.

Community
  • 1
  • 1
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
4

You don't specify your platform or compiler/libraries.

There are some interesting notes here (although a bit out of date) about ABI compatibility for GNU libStdc++ - there is some insulation from ABI compatibility at the expense of true compliance. It rather looks as if it's all or nothing here if you use std::pair.

libc++ (which is clang's standard library) takes another approach and intentionally inserts an extra namespace (I believe called __1) to all of its exported symbols, which means it's possible to link both libstdc++ and libc++ in the same executable. Providing you're not passing STL objects across the boundaries between your old and new libraries, you may well be able to get this to work.

marko
  • 9,029
  • 4
  • 30
  • 46