Re: utility to intentionally mangle function names?

From: Larry Doolittle (ldoolitt_at_recycle.lbl.gov)
Date: 04/07/04


Date: Wed, 7 Apr 2004 18:04:40 +0000 (UTC)

In article <c51d92$35b$8@sunnews.cern.ch>, Dan Pop wrote:
> In <b71fe84a.0404070641.1f2405ed@posting.google.com> efish@goldengate.com (Eric) writes:
>
>>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).
>>Problem: naming collisions.
>>
>>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.
>
> That's the right idea, you just have to implement it in C. Create your
> own name space, by prefixing all the internal identifiers with a prefix
> of your choice, carefully chosen to avoid likely conflicts with other
> programs doing the same thing. Make a list of all the identifiers that
> need to be "mangled" and a simple script around a non-interactive text
> editor should solve your problem.

If all your code #includes a header file, you could get
away without mangling your source code, and mangle it
transparently in the preprocessor instead.
For every global identifier that you don't want exported,

#define common_function1 app_specific_common_function1
#define common_function2 app_specific_common_function2

The substitutions will take place transparently on your
prototypes, declarations, and invocations. The user's
code that links to this stuff must _not_ #include that
header file, so it can happily use its _own_ common_function1
without interacting with your name space.

I might take it to another level, and

#define MANGLE(x) GOLDENGATE_ ## x
#define common_function1 MANGLE(common_function1)
#define common_function2 MANGLE(common_function2)

This makes it easier to change your namespace in a hurry.

You can also play linker tricks, but that's probably harder,
non-portable, and definitely off topic for c.l.c.

      - Larry