Ani
Ani

Reputation: 1526

Get function name as symbol (not as string) - C preprocessor

Is there a way in C to get the function name on which I can use token-pasting (I know __FUNCTION__ and __func__, but they don't expand to name at pre-processing, and I do not want the name as a string).

I want to be able to do something like: prefix_ ## __func_name__ , so that, in a function by name func1(), I can access the symbol prefix_func1

(maybe I can still use string, and then use dlsym, but want to know if there are simpler alternatives in GCC, not worrying about portability).

Upvotes: 1

Views: 1282

Answers (1)

Jens
Jens

Reputation: 72609

You could make the function identifier a macro, as in

#define FUNC1 func1

void FUNC1(void)
{
   ...
}

and then use FUNC1 for token pasting.

Upvotes: 2

Related Questions