Microkernel
Microkernel

Reputation: 1417

MACRO expansion to block of preprocessors

I have following preprocessor (code) block in my project before each function definition to unclutter the logging Macro.

#ifdef FC_NAME
#undef FC_NAME
#endif

#define FC_NAME  "myFunctionName"

But this itself looks kind of cluttered.

So, I am looking at replacing this with something that looks simpler and tried this

#define REDEF_FC_NAME(funcName) #ifdef FC_NAME \
\                                 #undef FC_NAME \
\                               #endif \
\                               #define FC_NAME funcName \

But this gives error saying macro def within a macro. So, is there a way of achieving the same effect?

Upvotes: 0

Views: 238

Answers (1)

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272487

The short answer is: you can't do that with macros.

But:

In C++, every function has a predefined __func__ variable.

GCC offers this as an extension to C, as I'm sure do many other compilers. On Windows, there appears to be the __FUNCTION__ macro (see http://msdn.microsoft.com/en-us/library/b0084kay(v=vs.71).aspx).

Upvotes: 2

Related Questions