Melio500
Melio500

Reputation: 459

Use preprocessor to "remove" prefix from function in C

I write a framework with lot of function that are named like that : ICE_ModuleType_FunctionUse() and everything else have ICE_ prefix (typename, define etc...)

And with preprocessor I would like to remove ICE_ to reduce function name lenght when the user know there is no conflict with other libs.

But the only working way I found was to write every function, type etc... by hand like that :

#define ModuleType_FunctionUse ICE_ModuleType_FunctionUse

Any Idea on how to easly do that ?

Upvotes: 1

Views: 946

Answers (2)

Joseph Quinsey
Joseph Quinsey

Reputation: 9962

You could automatically create a new header file with a name like use_namespace_ICE.h for your clients to use. This file would have the required list of #defines, and can be generated using the utilities nm or dumpbin applied to your library.

For example, if foo.c is:

void ICE_ModuleType_FunctionUse(void) { /* code */ }

then:

cc -c -o foo.o foo.c
nm foo.o | grep ' T _ICE_' | sed 's/.* T _ICE_\(.*\)/#define \1 ICE_\1/'

yields:

#define ModuleType_FunctionUse ICE_ModuleType_FunctionUse

Upvotes: 2

Paul Ogilvie
Paul Ogilvie

Reputation: 25286

As the comments tell you, there is no way, or no easy way, to shorten identifiers once written in your source code. However, you can reduce the typing for things that still need to be written:

#define ModuleType_FunctionUse ICE_ModuleType_FunctionUse

This defines that the short name will be replaced with the longer name.

Upvotes: 0

Related Questions