tags:

views:

475

answers:

4

Hello,

Compiling with gcc 4.4.2 and WinXP Visual Studio C++ 2008

#if defined ( WIN32 )
#define __FUNCTION__ __func__
#endif

As I want to use the macro to display the function name. I have done the above so I can cross-platform, and use the same func when compiling on linux or windows.

However, when I am compiling on WinXP I get the following error:

__func__ undeclared identifier

Can I not #define a macro like this?

Many thanks for any suggestions,

+1  A: 

You can of course #define such a macro. Every instance of FUNCTION is then replaced by __func__. However, obviosuly your compiler doesn't know __func__. I believe VC knows __FUNCTION__, so

#if defined ( WIN32 )
#  define __func__ __FUNCTION__
#endif

might do.

sbi
Sorry, but that didn't work either. Thanks.
robUK
`__FUNCTION__` isn't a macro, so you can't have a #ifdef on it. As with `__func__`, it's either a keyword for the compiler or it isn't.
tomlogic
@tomlogic: Yes, you're right. I fixed it.
sbi
+1  A: 

You should be able to use __func__ without any explicit macros in any compiler that supports C99.

sharth
Yes, I can use that in c89/c99. However, visual studio 2008 uses __FUNCTION__. I was trying #define so that I can use the same macro for both windows and linux. Thanks.
robUK
+2  A: 

The __FUNCTION__ macro is pre-defined in the MSVC compiler. You'll need to make it look like this:

#ifndef _MSC_VER
#define __FUNCTION__ __func__
#endif

Or the other way around, if you prefer:

#ifdef _MSC_VER
#define __func__ __FUNCTION__
#endif
Hans Passant
I tried that, and still got the same error. I also tried this as well in the condition (__MSVC__), and got the same error. Any more suggestions. Thanks.
robUK
Maybe its me, but when I try those 2 I get the error: "#if[n]def expected an identifer". Thanks.
robUK
@robUK - oops, the original snippet got me in trouble. Fixed.
Hans Passant
Thanks, I should have seen the brackets. I guess I am too used to using the if defined () with the brackets.
robUK
+2  A: 

It looks like you have your #define backward. If you want to use __func__ on both platforms, and WIN32 has __FUNCTION__ but not __func__, you need to do this instead:

#if defined ( WIN32 )
#define __func__ __FUNCTION__
#endif

There may be a better way to know whether you need to define __func__ or not, but this quick hack should do the trick.

Remember, on compilers that support the __FUNCTION__ and __func__ keywords, they're not macros so you can't do #ifndef __func__ \ #define __func__ __FUNCTION__ \ #endif.

From the C99 spec:

6.4.2.2 Predefined identifiers

1 The identifier __func__ shall be implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declaration

static const char __func__[] = "function-name";

appeared, where function-name is the name of the lexically-enclosing function.

tomlogic
Thanks that worked. But has got me thinking. When you say that __FUNCTION__ and __func__ are not macros. Why can't you define in a #define. Also how can we know if it is a macro or keyword? Many thanks.
robUK
That's a good question -- I guess you have to go to the ANSI spec (or Google) to find out. In this case, `__func__` is a "predefined identifier" and based on the description, acts like a static const variable defined in the function.In my example above, the part that won't work is `#ifndef __func__` since `__func__` isn't a defined macro visible to the macro pre-processor.
tomlogic