Question

I would like to implement some kind of name mangling for a C interface. I will have functions and types like

struct Foo;
struct Foo* Foo_create(struct Bar* bar);
void Foo_destroy(struct Foo* obj);
struct Foo_InnerClass;
...

Now, the _ is used extensivliy in all standard c typedefs, so the scheme would not work good if these typedefs are used. GCC and MSVC seems to accept names using $. Would that work? Are there any other acceptet characters that I can use?

EDIT:

I want to do this because

  • It avoids duplicated names
  • It emphases what class an identifier belongs to
  • It makes it possible to automatically generate C++ interface

EDIT 2:

Reserve an ordinary letter will compile everywhere, but it can also be confusing:

struct Foo* FooIcreate(struct Bar* bar);

EDIT 3:

The symbol may differ depending on target platform, but on that platform, it must not collide with C++ name mangling.

EDIT 4:

It is forbidden to begin an identifier with a digit. So what about:

struct Foo* Foo_1_create(struct Bar* bar);
Was it helpful?

Solution 5

In lack of a better sulotion, i think i will use

_1

as separator

OTHER TIPS

Not sure why you are doing this, or exactly what the goal is, but most C libraries use some more or less mnemonic prefix.

A plain $ would be awful, since it's a) very uncommon, C identifiers typically start with a letter or underscore and contain only letters, digits and underscores and b) doesn't say anything about to what the symbol belongs.

A library like glib has a g_ prefix for all functions, a g for its types, and G_ for macros. GTK+ is similar, it uses gtk_ and so on.

It's fine, though I like to use struct Foo_t to show it's a type.

My preference is to keep double _ (__) for something special or a "private" indicator and not regular use. This is pure personal preference though any local coding standards may say something on the subject.

The $ is iffy... while some compilers accept it some don't. It is not a historic part of the C language. While common in some operating systems and languages, not so much in others. History: the big three historic specials of IBM mainframes are $, #, and @ where _ was NOT a member of the club and VMS loves $.

Don't use $. The C standard does not allow them in identifiers, even though C compilers such as GCC accept them for backward compatibility with 1980s C compilers (mostly VAX/VMS C, I believe).

How about using double underscores?

struct Foo__InnerClass;

EDIT: ok, this prevents your program from ever working in a mixed C/C++ program (see C++ underscore rules), so it's a bad idea. How about a trailing underscore:

struct Foo_InnerClass_

It would not use dollar sign in identifiers because it is:

  • Uncommon, I didn't see it anywhere
  • Because of the first reason it would confuse users of your code, and maybe even some tools, editors, etc.
  • It is not supported for all targets, for example:

    echo 'void foo$bar(void) { }' | arm-elf-gcc -xc -c -
    

    Outputs:

    <stdin>:1: error: stray '$' in program
    <stdin>:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'bar'
    

Personally I would use some kind of module prefixes, like in your first code snippet. If you afraid of name collisions with other libraries you can add one more global prefix, like mylib_foo_create.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top