2

Suppose I have a .cpp file in which I have to include a third-party header file that declares some symbol (let's call it div). I do not need this symbol, but I need to define my own div symbol. However, the original symbol might be used by some other part of the header file. Is it possible to remove the symbol from global namespace (something similar to undefing a macro)?

I want something like this:

#include <string> //stdlib.h defines div symbol
//do some symbol removal magic here
std::string div;

to compile using gcc-4.7/clang with -std=c++11 option.

Tomasz Grobelny
  • 2,666
  • 3
  • 33
  • 43
  • 4
    Why not declare names in your lib in a namespace? – Vikas Oct 24 '12 at 10:46
  • 1
    Because then I would have to qualify it with namespace name every time and I would prefer to avoid that. – Tomasz Grobelny Oct 24 '12 at 10:52
  • 4
    Is there a reason other than laziness why you don't want to use a namespace? – John Dibling Oct 24 '12 at 10:56
  • No. But come on, all developers are lazy. That's why we have "using namespace nsname;" constructs in basically all sane languages. – Tomasz Grobelny Oct 24 '12 at 10:59
  • 3
    And that laziness is probably the reason your 3rd party header pollutes the global namespace.... – juanchopanza Oct 24 '12 at 11:03
  • I am equally disgusted that C++ standard in 2011 revision was changed the way it was and now stdlib pollutes the global namespace. But I guess we have to live with it. – Tomasz Grobelny Oct 24 '12 at 11:18
  • @TomaszGrobelny, can you explain the 'stdlib pollutes the global namespace' bit? I am not sure if anything in C++11 has done anything to do that. – Vikas Oct 24 '12 at 13:14
  • 2
    So your objection is that you can't pollute the global namespace with your names because the standard library has already done it. Sorry, very little sympathy here. Use a namespace; that's what they're for. – Pete Becker Oct 24 '12 at 13:21
  • 1
    @Pete Becker: I would gladly put my symbol into my own namespace as long as the user is able to write "using namespace customNS;" and then use the symbols without qualifying with namespace name. – Tomasz Grobelny Oct 24 '12 at 13:58
  • @Vikas: have a look here: http://stackoverflow.com/questions/13040139/namespace-issues-in-c11 – Tomasz Grobelny Oct 24 '12 at 13:59
  • @TomaszGrobelny: "*then use the symbols without qualifying with namespace name.*" That's not how namespaces work, nor is that what they are *for*. If you dump namespace symbols into the global namespace to use it, *you're doing it wrong*. – Nicol Bolas Oct 24 '12 at 14:54

3 Answers3

8

You cannot remove a symbol from the namespace in which it was defined (including the global namespace).

Wrap your own div in your own namespace.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Greg
  • 1,660
  • 13
  • 12
5

Basically, no.

But as the others say you can use a namespace instead to get the same effect with what is actually essentially a completely different symbol.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

I've never used it, but you can try with GNU objcopy (as suggested in https://stackoverflow.com/a/1950629/721929).

But I think it's something to avoid if you have control on source code. It will be time expensive to test if this works.

--redefine-sym old=new
Change the name of a symbol old, to new. This can be useful when one is 
trying link two things together for which you have no source, 
and there are name collisions. 

objcopy --redefine-sym oldNew=newNew libBlue.a
Community
  • 1
  • 1
kikeenrique
  • 2,589
  • 2
  • 25
  • 46