Include another library in a constructed namespace

Nov 23, 2008 at 5:41pm
Hello!

I want to include two external libraries that defines functions with the same signature, causing link problems. Is it possible to include these allready compiled libraries in different namespaces, and by that avoiding the name trouble?

For example:

1
2
3
4
5
6
7
8
namespace library1
{
  #include "library1.h"
}
namespace library2
{
  #include "library2.h"
}


The problem with this naiv implementation is that the linking won't work, as the library is not compiled with that namespace.

Any "easy" workarounds?

Many thanks in advance

Johan
Nov 23, 2008 at 6:24pm
Do you need to call functions of both libraries in the same file?
Nov 23, 2008 at 7:03pm
I think you could do something like that by creating new files:
1
2
3
4
5
6
7
8
9
//mylibrary1.cpp
#include "library1.h"
namespace lib1
{
    type name(arguments)
    {
        return ::name(arguments);
    }
}

Do this for all the functions you will need and create a header with the declarations to be incuded, same for the other library
Last edited on Nov 23, 2008 at 7:05pm
Nov 23, 2008 at 7:27pm
Hello!

@helios:
No I do not have to do that, but I have to link the resulting files into one library, causing linking problem.

@Bazzy:
I think your suggestion will help me creating namespaces I can use in my code for the two libraries. This will be very doable as the public interface to these libraries are very small.
The problem with this solution is that it is not the public interface of these libraries that shares signatures, it is a a quite large "private" interface. These function all call eachothers and I suppose I have to change these signatures too.
In addition I do not think this would help me in the linking stage, as I have to link to both original libraries anyway, exposing the dual signatures.

The "best" solution would probably be to add a namespace declaration in the original library files. The licens is OK, but then I have to keep my own version of the library, which is suboptimal.

Btw, the two libraries are Triangle and Tetgen, two meshgenerating programs.

Johan
Nov 23, 2008 at 7:45pm
Hmm... Well this is a very rare problem. Most libraries have a sort of namespace in the identifiers themselves (e.g. FreeImage_*, SDL_*, etc.) so that this kind of thing never happens.
I think just surrounding the definitions with a namespace won't solve anything. If there are duplicate symbols, you'll get duplicate definitions when you link, anyway.
Nov 23, 2008 at 8:20pm
I agree that it is a strange problem.

I think the two libraries was developed first and formost as two standalone command prompt applications, with capabilities to be compiled as libraries. The developers then probably did not care too much of attaching any library specific prefix to the names.

Johan
Nov 23, 2008 at 8:27pm
If the libraries are OS, I'd say your best bet is to find the conflicting symbols and refactor them.
Nov 23, 2008 at 8:39pm
Many thanks for your replies helios!

They are not GPL or LGPL, but the code is released to the public for _non_ commercial use.

Would it be possible to wrap the excisting code in the libraries in a namespace instead of refactoring each function, e.g.:

1
2
3
4
5
//library1.h
namespace lib1 
{
// The definitions
}


1
2
3
4
5
6
//library1.cpp
#include "library1.h"
namespace lib1 
{
// The implementations
}


Johan
Nov 23, 2008 at 9:01pm
Yeah... I think so... Yeah, I think it should work.
Nov 23, 2008 at 9:37pm
Ok, then i think I try that, and report any results

thanks again!

Johan
Topic archived. No new replies allowed.