skydoor
skydoor

Reputation: 25868

How could one emulate namespace in C?

In C++ one might use namespace to keep independent groups working in the same code base from inadvertently creating functions with the same name and thus creating a conflict. My question is, before the idea of namespace comes out, how might one emulate namespace in C.

Upvotes: 12

Views: 1011

Answers (3)

John Knoeller
John Knoeller

Reputation: 34148

Use a common prefix for the names of all of your public symbols, so foo::bar becomes foo_bar. tossing a prefix on all of the symbol names is essentially what namespaces do. (and also resolving symbols used without the prefix to declarations that have the prefix, which is pretty helpful)

Upvotes: 7

For symbols not exported you put each module in a separate file.

For exported symbols you generally apply a prefix. Two or three letters are common.

Upvotes: 2

Georg Fritzsche
Georg Fritzsche

Reputation: 98974

By naming things differently, e.g.:

void namespace_group_function();

gtk+ is a prime example for this conventional style:

GtkWidget* gtk_window_new(GtkWindowType type);

Upvotes: 18

Related Questions