Eric wrote:
I've got a pretty large C program with global variables and function
names strewn about (i.e. no "static" declarations in front of them).
Now I want to expose the ability for user's to supply their own shared
object / dll code which will link with my large ugly program.
Problem: naming collisions.
If my program has:
void common_function_name (void)
{
...
}
and now the caller has a similar function, they will get a link error.
What I really would like to do is "mangle" all of my names
intentionally, once I've built my code, except for those few functions
that are legitimate entry points back into my code that the caller
will need.
Thought about wrapping all my code in a namespace and compiling in C++
but this doesn't appear feasible now that I've tried it (many errors).
The other method would be to do a bunch of code conversion, which is
highly risky.
Thoughts?
First thought: I hope you've now learned the drawbacks
of wholesale namespace pollution.
Second thought: Gather up all the objectionable names
(using ad-hoc tools -- a good place to start might be by
examining your executable's symbol table, if it has one)
and then build yourself a "mangle.h" header file:
#define common_function_name mangled_function_name
#define common_variable_name mangled_variable_name
...
#include this header at the start of each source file,
recompile, and clean up the (few, one hopes) problems.
Third thought: The success of the above approach depends
on your ability to dream up mangled names nobody else will
ever invent. That's a pretty high standard of originality,
and may even create problems where none existed before.
What you really need to do is reduce the size of the problem
(you can't eliminate it entirely) by hiding the names you
really didn't need to export in the first place. The C way
to do this is to go around sticking in `static', and the
same ad-hoc methods you used to collect the offending symbols
for "mangle.h" would also serve as a help for locating all
the right sites for `static'. Alternatively, your platform
may offer a non-C means of hiding externally-linked symbols,
typically after linking a library; consult your documentation.
Fourth thought: There really is no 100% reliable Standard
C solution to this problem, even if the code was designed from
the very beginning to be minimally intrusive of the name space.
--
Er*********@sun.com