__FUNCTION__在另一个宏中 [英] __FUNCTION__ in another macro
问题描述
我使用#define如下:
#include< stdio.h>
#define LOG_PREFIX"当前功能:<" __FUNCTION__">:"
int main()
{
printf(LOG_PREFIX" some thing.\\ \\ n);
返回0;
}
我编译并得到错误:
%cc func.c
func_test.c:在函数main中:
func_test.c:6:错误:语法错误之前 __FUNCTION __"
我想用LOG_PREFIX宏自动扩展。
有什么办法吗?
提前致谢。
-
问候。
Mockey Chen
你好
为什么这么复杂
只是这样做
#include< stdio.h>
int main()
{
printf("当前函数<%s> \ n",__ FUNCTION__);
返回0;
}
按照自己的方式做事不行,字符串无法添加到
海誓山盟在C
必须有永远的记忆才能拥有它!
更好的是制作这样的错误功能
void error_logger(char * functionName,char * mess)
{
printf(" Current function<%s%s \ n",functionName) ,混乱);
}
并像这样称呼
error_logger(__ FUNCTION __,某事);
这个你可以放一个像这样的宏
#define ERROR_LOG(乱)error_logger(__ FUNCTION __,乱七八糟)
现在你可以打电话了它就像
ERROR_LOG(" someThing");
我希望现在你的排名是anserd
问候奥拉夫
Mockey Chen schreef:
我使用#define如下:
#include< stdio .h>
#define LOG_PREFIX"当前功能:<" __FUNCTION__">:"
int main()
{
printf(LOG_PREFIX" some thing.\\ \\ n);
返回0;
}
我编译并得到错误:
%cc func.c
func_test.c:在函数main中:
func_test.c:6:错误:语法错误之前 __FUNCTION __"
我想使用LOG_PREFIX宏自动扩展。
有没有办法做到这一点?
提前致谢。
-
问候。
Mockey Chen
Mockey Chen在07/27/06 10:06写道:
我使用#define如下:
#include< stdio.h>
#define LOG_PREFIX"当前函数:<" __FUNCTION__">:"
int main()
{
printf(LOG_PREFIX" some thing.\\ \\ n);
返回0;
}
我编译并得到错误:
%cc func.c
func_test.c:在函数main中:
func_test.c:6:错误:语法错误之前 __FUNCTION __"
可能是因为__FUNCTION__尚未定义。
从使用情况来看,您希望__FUNCTION__为$ / b
a宏扩展为字符串文字(然后
与每边的文字结合起来制作一个更大的
字面值)。 C不会定义任何这样的宏,但是,你必须(1)自己定义它或(2)使用编译器
自动定义__FUNCTION__作为额外的功能
超出标准C提供的
。 (我相信一些gcc
版本,如果在某些模式下调用,会执行此操作。)
我想使用LOG_PREFIX宏自动化扩展。
有什么办法吗?
如果你能找到支持标准的C99版本
的编译器,你可以使用预定义的标识符__func__
用于类似目的。但请注意,__ func__不是
宏,也不会扩展为字符串文字;它引用一个
自动生成的const char数组,其中包含
函数名作为字符串。这意味着你不能像你试图用
__FUNCTION__那样连接它的邻居,而是需要做更多的事情。 />
printf("功能:<%s>:some thing。\ n",__ func__);
-
Er*********@sun.com
(更正了帖子。)
mdler写道:
Mockey Chen schreef:
[...]
我使用#define如下:
#include< stdio.h>
#define LOG_PREFIX"当前函数:<" __FUNCTION__">:"
int main()
{
printf(LOG_PREFIX" some thing.\\ \\ n);
返回0;
}
我编译并得到错误:
%cc func.c
func_test.c:在函数main中:
func_test.c:6:错误:语法错误之前 __FUNCTION __"
我想用LOG_PREFIX宏自动扩展。
它已扩展。但是,如果您的编译器不支持
__FUNCTION__宏(根据我的理解,它是C99功能,
,某些C90编译器支持) ,这将导致
错误,因为扩展变为:
printf("当前功能:<" __FUNCTION__">: "" some thing.\\\
;
作为快速测试,添加:
#define __FUNCTION__" foo" ;
如果它然后编译,并显示函数< foo>,那么那就是问题。
问题。
有什么办法吗?
如果你的编译器不支持它,唯一的选择就是做一些其他的东西,或者得到一个确实支持它的编译器。
[...]
以你不会工作的方式做事,字符串无法添加到
每个C
字符串文字可以添加很好:
printf(" Hello"" world""。\ n");
或
#define LONG_STRING"这是一个长字符串" \ n>
不适合 \
单行。
-
+ ---------- --------------- + -------------------- + ------------- ---------- +
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody / at\spamcop.net | www.fptech.com | < std_disclaimer.h |
+ ------------------------- + --------- ----------- + ----------------------- +
不要 - 邮寄给我:< mailto:Th ************* @ gmail.com>
I using #define as following:
#include <stdio.h>
#define LOG_PREFIX "Current function: <" __FUNCTION__ ">: "
int main()
{
printf(LOG_PREFIX "some thing.\n");
return 0;
}
I complie and got error:
% cc func.c
func_test.c: In function `main'':
func_test.c:6: error: syntax error before "__FUNCTION__"
I want to use LOG_PREFIX macro automatice expand.
Is there any way to do it?
Thanks in advance.
--
Regards.
Mockey Chen
Hello
why so complicated
just do it like this
#include <stdio.h>
int main ()
{
printf("Current function <%s>\n",__FUNCTION__);
return 0;
}
doing things your way it won''t work, strings can not be added to
eachother in C
There has to be always memory to hold it!
better is it to make a error function like this
void error_logger(char *functionName,char *mess)
{
printf("Current function <%s%s\n",functionName,mess);
}
and call it like this
error_logger(__FUNCTION__,"something");
this You can put in a macro like this
#define ERROR_LOG(mess) error_logger(__FUNCTION__,mess)
Now you can call it like
ERROR_LOG("someThing");
I hope that now your quetion is anserd
Greetings Olaf
Mockey Chen schreef:
I using #define as following:
#include <stdio.h>
#define LOG_PREFIX "Current function: <" __FUNCTION__ ">: "
int main()
{
printf(LOG_PREFIX "some thing.\n");
return 0;
}
I complie and got error:
% cc func.c
func_test.c: In function `main'':
func_test.c:6: error: syntax error before "__FUNCTION__"
I want to use LOG_PREFIX macro automatice expand.
Is there any way to do it?
Thanks in advance.
--
Regards.
Mockey Chen
Mockey Chen wrote On 07/27/06 10:06,:I using #define as following:
#include <stdio.h>
#define LOG_PREFIX "Current function: <" __FUNCTION__ ">: "
int main()
{
printf(LOG_PREFIX "some thing.\n");
return 0;
}
I complie and got error:
% cc func.c
func_test.c: In function `main'':
func_test.c:6: error: syntax error before "__FUNCTION__"Probably because __FUNCTION__ has not been defined.
From the usage, it appears you expect __FUNCTION__ to be
a macro that expands to a string literal (which would then
combine with the literals on each side to make one larger
literal). C does not define any such macro, though, so
you must either (1) define it yourself or (2) use a compiler
that defines __FUNCTION__ automatically as an extra feature
beyond what Standard C provides. (I believe that some gcc
versions, if invoked in some modes, will do this.)
I want to use LOG_PREFIX macro automatice expand.
Is there any way to do it?If you can find a compiler that supports the C99 version
of the Standard, you can use the predefined identifier __func__
for a similar purpose. Note, though, that __func__ is not a
macro and does not expand to a string literal; it refers to an
automatically-generated array of const char containing the
function name as a string. That means you can''t concatenate
__func__ with its neighbors the way you''re trying to do with
__FUNCTION__, but will need to do something more like
printf ("Function: <%s>: some thing.\n", __func__);
--
Er*********@sun.com
(Top-posting corrected.)
mdler wrote:Mockey Chen schreef:[...]
I using #define as following:
#include <stdio.h>
#define LOG_PREFIX "Current function: <" __FUNCTION__ ">: "
int main()
{
printf(LOG_PREFIX "some thing.\n");
return 0;
}
I complie and got error:
% cc func.c
func_test.c: In function `main'':
func_test.c:6: error: syntax error before "__FUNCTION__"
I want to use LOG_PREFIX macro automatice expand.
It is expanded. However, if your compiler doesn''t support the
__FUNCTION__ macro (which, as I understand it, is a C99 feature,
which is supported by some C90 compilers), then this will cause
an error, as the expansion becomes:
printf("Current function: <" __FUNCTION__ ">: " "some thing.\n");
As a quick test, add:
#define __FUNCTION__ "foo"
If it then compiles, and show the function "<foo>", then that''s
the problem.
Is there any way to do it?
If your compiler doesn''t support it, the only option is to do
something else, or get a compiler that does support it.
[...]doing things your way it won''t work, strings can not be added to
eachother in CString literals can be "added" just fine:
printf("Hello" " world" ".\n");
or
#define LONG_STRING "This is a long string " \
"that won''t fit on a " \
"single line."
--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don''t e-mail me at: <mailto:Th*************@gmail.com>
这篇关于__FUNCTION__在另一个宏中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!