3

I need to write a plugin for an application that statically includes a certain C++ library that doesn't use namespaces.

Unfortunately my plugin needs to link against a customized version of the same library.

Ideally I would like to recompile it so that its symbols don't clash with those found in the application.

One way to do that, without having to edit hundreds of files and without losing the ability to apply new updates from upstream, would be to add a namespace prefix to the library at compile time.

Apparently though there's no g++ option to do that. Alternatively is there any way to programmatically alter symbols in an already compiled .so file?


EDIT: to clarify, the problem is not at the library-plugin boundary, that's the "easy" part; the problem is that I've got this huge library and I'd like to find a way to recompile it as if it all belonged to a certain namespace without having to change the source code.

Community
  • 1
  • 1
UncleZeiv
  • 18,272
  • 7
  • 49
  • 77

4 Answers4

1

I don't see an easy solution. You could code a GCC plugin or an extension in GCC MELT to do the job, but this is probably too much work.

If the library is free, and if you are allowed to change it, I would make the patches, and push them upstream (even if that does take some time).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Thanks, we are definitely going to push the namespace patch upstream, but I can't see it being accepted in a matter of days. In the meanwhile, I'm looking for a temporary solution; even a hackish one would do. – UncleZeiv Nov 04 '11 at 18:09
  • If the library is large enough, you could make, as I said, a GCC plugin or a MELT extension to GCC. However, this is a more than a week of work. – Basile Starynkevitch Nov 04 '11 at 19:11
  • yep sorry, I should have mentioned that those pointers look really useful. I'll look into gcc plugins, they are definitely a lot of work for this specific case but it's something I can see being useful in other projects of ours. – UncleZeiv Nov 05 '11 at 01:13
1

Why not write a file that opens a namespace and then includes the file you already have?

Dabbler
  • 9,733
  • 5
  • 41
  • 64
  • See my comment on @Stowelly's answer. – Xeo Nov 04 '11 at 15:44
  • Hm, maybe my explanation was not clear, but it's not "a file", it's an entire library. And the problem is not on my plugins' side, I actually need to compile that library to include the namespace in all symbols. – UncleZeiv Nov 04 '11 at 18:09
0

If you mean you were hoping for an option to add the namespace via a GCC command-line option, it is true, as UncleZeiv points out in their answer, that there is no such direct command-line option for this.

However, if you are able to create a new file within your codebase, you can achieve this effect by the namespace code into that new file, then adding that file into your source using GCC's -include option

MarkR
  • 166
  • 1
  • 8
0

we do this for one of the libraries we link with

namespace MY_NAMESPACE
{
 #include "api\api.h"
}

then just call its functions from the new namespace

MY_NAMESPACE::api_function();

EDIT: I may have missed the point here a bit as im unsure how this will affect symbols

Stowelly
  • 1,260
  • 2
  • 12
  • 31
  • 1
    This can get problematic when `"api/api.h"` includes any other headers, as they will also get stuffed into the namespace. Talkin about standard headers mostly. – Xeo Nov 04 '11 at 15:43
  • yeah I guess, this idea mostly only works for self contained api's – Stowelly Nov 04 '11 at 15:53
  • Yep I guess this would work but the problem though is not on my side, it's that I've got this huge library and I need to compile it as if all its symbols belonged to a namespace. When that's ready, I'll do something similar to what you say to include the headers. – UncleZeiv Nov 04 '11 at 18:11