I am using gcc to compile C99 code. I want to write a macro which will return a string containing the function name and line number.
This is what I have:
#define INFO_MSG __FILE__ ":"__func__"()"
However, when I compile code which attempts to use this string, for example:
char buff[256] = {'\0'}
sprintf(buff, "Something bad happened here: %s, at line: %d", INFO_MSG, __LINE__);
printf("INFO: %s\n", buff);
I get the following error message:
error: expected ‘)’ before ‘__func__’
I have tracked the problem down to the macro. as when I remove __func__
from the macro, the code compiles correctly.
How do I fix the macro, so that I can include the predefined __func__
macro in my string?
Remark that, "
__func__
is not a function so it cannot be called; in fact, it is a predefined identifier that points to a string that is the name of the function, and is only valid inside the scope of a function." - Jonathan.The following is what you are looking for:
... note that a call to
__func__
can be made inside the function itself. See this.Judging from your comments, the objective is to have a macro which combines the file name and function name (and maybe line number) into a single string that can be passed as an argument to functions such as
printf()
orstrcpy()
orsyslog()
.Unfortunately, I don't think that's possible.
The C11 standard says:
Therefore,
__func__
is not a macro, unlike__FILE__
or__LINE__
.The related question What's the difference between
__PRETTY_FUNCTION__
,__FUNCTION__
,__func__
? covers some alternative names. These are GCC-specific extensions, not standard names. Moreover, the GCC 4.8.1 documentation says:There are sound reasons why these cannot be preprocessor constructs. The preprocessor does not know what a function is and whether the text it is processing is in the scope of a function, or what the name of the enclosing function is. It is a simple text processor, not a compiler. Clearly, it would be possible to build that much understanding into the preprocessor (solely for the support of this one feature), but it is not required by the standard, and neither should it be required by the standard.
Unfortunately, though, I think it means that attempts to combine
__func__
(by any spelling) with__FILE__
and__LINE__
in a single macro to generate a single string literal are doomed.Clearly, you can generate the file name and line number as a string using the standard two-step macro mechanism:
You can't get the function name into that as part of a string literal, though.
There are arguments that the file name and line number are sufficient to identify where the problem is; the function name is barely necessary. It is more cosmetic than functional, and slightly helps programmers but not other users.
After a quick experiment I found that you cannot use
__func__
with stringification. It would not make much sense if you could as this would mean that the value would be wherever the macro is defined instead of where it is applied.The nature of
__func__
, as noted in the comments on the question, is described in this answer.Stringification is performed at pre-processor time and because of that
__func__
is unavailable as it is essentially a function local string that is defined later on the compilation process.However you can use
__func__
in a macro as long as you don't use stringification on it. I think the following performs what you're after:Note that there's no particular reason, in the question as presented, to use a string buffer. The following
main
function would achieve the same effect without the possibility of buffer overrun:Personally, I'd wrap up the whole process in the macro like this:
it is a syntax error. I try to come over with your macro specification but I didnt find a efficient way, so maybe you can try this: