gpt4 book ai didi

c 字符串化和 __function__

转载 作者:太空宇宙 更新时间:2023-11-04 05:25:53 25 4
gpt4 key购买 nike

我正在尝试添加一个编译局部变量的 c 宏它被初始化为函数名..

例如。

 void foo (void)
{
stubmacro;
}


void bar (void)
{
stubmacro;
}

基本上编译为:

 void foo (void)
{
char*function_name="foo";
}


void bar (void)
{
char*function_name="bar";
}

我在使用 C 预处理器宏方面一直遇到困难, 特别是在字符串化方面

宏使用预定义的FUNCTION ..

#define stubmacro char*function_name=##_FUNCTION__

无论如何,我的 stubmacro 宏是错误的,我希望得到一些帮助 在上面

最佳答案

只需使用__func__,这是一个预定义的字符串,可以满足您的需要:

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.

关于c 字符串化和 __function__,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27779795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com