6

I'm trying to port an old library (that doesn't use namespaces as far as I can tell) to modern compilers. One of my targets can't tell the difference between System::TObject and ::TObject (without a namespace). System::TObject is native to the compiler.

I've tried a using directive, i.e. using ::TObject;

But that doesn't do it.

The obvious solution is to wrap all the original library in a namespace and then calling it by name- that should avoid the ambiguity. But is that the wisest solution? Is there any other solution? Adding a namespace would require changing a bunch of files and I don't know if it would have unwanted repercussions later.

Logomachist
  • 169
  • 1
  • 6

5 Answers5

3

You can do as Dib suggested, with a slight modification:

// In a wrapper header, eg: include_oldlib.h...

namespace oldlib
{
   #include "oldlib.h"
};

#ifndef DONT_AUTO_INCLUDE_OLD_NAMESPACE
using namespace oldlib;
#endif

This allows you to #define the exclusion in only the files where you're getting conflicts, and use all the symbols as global symbols otherwise.

Nick
  • 6,808
  • 1
  • 22
  • 34
1

You could make a wrapper for all the old functions and package them up into a DLL or static library.

Adam Pierce
  • 33,531
  • 22
  • 69
  • 89
0

If you have the source to the library, maybe include a header file at the top of each source where that header file has only:

#define TObject TMadeUpNameObject
Jim Buck
  • 20,482
  • 11
  • 57
  • 74
0

Try this:

namespace oldlib
{
   #inclcude "oldlib.h"
};
John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • this will cause the compiler to create symbols prefixed by oldlib, which won't be present in the old library, resulting in `unresolved external symbol "public: __thiscall oldlib::A::~A(void)" (??1A@oldlib@@QAE@XZ)` – xtofl Oct 10 '08 at 09:33
0

I have used the following in the past while encapsulating a third party header file containing classes colliding with the code:

#ifdef Symbol
#undef Symbol
#define Symbol ThirdPartySymbol
#endif
#include <third_party_header.h>
#undef Symbol

This way, "Symbol" in the header was prefixed by ThirdParty and this was not colliding with my code.

David Segonds
  • 83,345
  • 10
  • 45
  • 66