C中如何用#define宏中的字符串常量替换函数名
我希望使用预处理器指令将函数调用替换为字符串。 大概是这样的:C中如何用#define宏中的字符串常量替换函数名,c,string,macros,c-preprocessor,preprocessor-directive,C,String,Macros,C Preprocessor,Preprocessor Directive,我希望使用预处理器指令将函数调用替换为字符串。 大概是这样的: #ifdef DEBUG #define Func1(arg) "Function not supported" #endif 基本上,当有人调用这个函数时,我想要一个编译错误,这样这个函数在DEBUG模式下是不可见的,取而代之的是,在编译日志中打印以下字符串。 此方法会引发错误。 当调用func1()时,是否有其他方法可以打印我想要的特定字符串?存档此类行为的最明显方法是使用\error指令。然而,由于无法构造“条件#e
#ifdef DEBUG
#define Func1(arg) "Function not supported"
#endif
基本上,当有人调用这个函数时,我想要一个编译错误,这样这个函数在DEBUG
模式下是不可见的,取而代之的是,在编译日志中打印以下字符串。
此方法会引发错误。
当调用
func1()
时,是否有其他方法可以打印我想要的特定字符串?存档此类行为的最明显方法是使用\error
指令。然而,由于无法构造“条件#error
指令”,我想下一步是C99中引入的\u Pragma
运算符。以下是在编译期间生成消息的解决方案:
#include <stdio.h>
#define DEBUG 1
#ifdef DEBUG
#define Func1(arg) _Pragma("message \"Function not supported.\"")
#endif
void (Func1)(int arg)
{
}
int main(void)
{
Func1(1);
Func1(2);
Func1(3);
return 0;
}
我知道这不是直接的解决方案(这样的消息甚至不会被视为警告,所以-Werror
不会改变任何东西),但是您可以使用例如grep
工具或任何其他方法来扫描编译器的输出
由于GCC4.8还有,这是直接(但不可移植)的解决方案。查看更多信息。例如:
#ifdef DEBUG
#define Func1(arg) _Pragma("GCC error \"Function not supported.\"")
#endif
归档此类行为最明显的方法是使用
#error
指令。然而,由于无法构造“条件#error
指令”,我想下一步是C99中引入的\u Pragma
运算符。以下是在编译期间生成消息的解决方案:
#include <stdio.h>
#define DEBUG 1
#ifdef DEBUG
#define Func1(arg) _Pragma("message \"Function not supported.\"")
#endif
void (Func1)(int arg)
{
}
int main(void)
{
Func1(1);
Func1(2);
Func1(3);
return 0;
}
我知道这不是直接的解决方案(这样的消息甚至不会被视为警告,所以-Werror
不会改变任何东西),但是您可以使用例如grep
工具或任何其他方法来扫描编译器的输出
由于GCC4.8还有,这是直接(但不可移植)的解决方案。查看更多信息。例如:
#ifdef DEBUG
#define Func1(arg) _Pragma("GCC error \"Function not supported.\"")
#endif
归档此类行为最明显的方法是使用
#error
指令。然而,由于无法构造“条件#error
指令”,我想下一步是C99中引入的\u Pragma
运算符。以下是在编译期间生成消息的解决方案:
#include <stdio.h>
#define DEBUG 1
#ifdef DEBUG
#define Func1(arg) _Pragma("message \"Function not supported.\"")
#endif
void (Func1)(int arg)
{
}
int main(void)
{
Func1(1);
Func1(2);
Func1(3);
return 0;
}
我知道这不是直接的解决方案(这样的消息甚至不会被视为警告,所以-Werror
不会改变任何东西),但是您可以使用例如grep
工具或任何其他方法来扫描编译器的输出
由于GCC4.8还有,这是直接(但不可移植)的解决方案。查看更多信息。例如:
#ifdef DEBUG
#define Func1(arg) _Pragma("GCC error \"Function not supported.\"")
#endif
归档此类行为最明显的方法是使用
#error
指令。然而,由于无法构造“条件#error
指令”,我想下一步是C99中引入的\u Pragma
运算符。以下是在编译期间生成消息的解决方案:
#include <stdio.h>
#define DEBUG 1
#ifdef DEBUG
#define Func1(arg) _Pragma("message \"Function not supported.\"")
#endif
void (Func1)(int arg)
{
}
int main(void)
{
Func1(1);
Func1(2);
Func1(3);
return 0;
}
我知道这不是直接的解决方案(这样的消息甚至不会被视为警告,所以-Werror
不会改变任何东西),但是您可以使用例如grep
工具或任何其他方法来扫描编译器的输出
由于GCC4.8还有,这是直接(但不可移植)的解决方案。查看更多信息。例如:
#ifdef DEBUG
#define Func1(arg) _Pragma("GCC error \"Function not supported.\"")
#endif
你可以用
#error "Function not supported"
中止编译。
但是很明显,当有人能够访问可以删除的源代码时——那么你到底想做什么呢
如果你不相信那些人有你的消息来源,那么就不要把它给他们。
将它链接到一个.obj中,用一个.h定义“接口”,并只给出它。您可以使用
#error "Function not supported"
中止编译。
但是很明显,当有人能够访问可以删除的源代码时——那么你到底想做什么呢
如果你不相信那些人有你的消息来源,那么就不要把它给他们。
将它链接到一个.obj中,用一个.h定义“接口”,并只给出它。您可以使用
#error "Function not supported"
中止编译。
但是很明显,当有人能够访问可以删除的源代码时——那么你到底想做什么呢
如果你不相信那些人有你的消息来源,那么就不要把它给他们。
将它链接到一个.obj中,用一个.h定义“接口”,并只给出它。您可以使用
#error "Function not supported"
中止编译。
但是很明显,当有人能够访问可以删除的源代码时——那么你到底想做什么呢
如果你不相信那些人有你的消息来源,那么就不要把它给他们。
将它链接到一个.obj中,用一个.h定义“接口”,并只给出它。一种方法是只保留函数未定义。这将导致链接时出错 如果使用gcc,则可以使用其扩展之一,即函数属性:
#ifndef DEBUG
#define __nodebugonly
#else
#define __nodebugonly __attribute__((error("Function not supported")))
#endif
void Func1(int arg) __nodebugonly;
一种方法是不定义函数。这将导致链接时出错 如果使用gcc,则可以使用其扩展之一,即函数属性:
#ifndef DEBUG
#define __nodebugonly
#else
#define __nodebugonly __attribute__((error("Function not supported")))
#endif
void Func1(int arg) __nodebugonly;
一种方法是不定义函数。这将导致链接时出错 如果使用gcc,则可以使用其扩展之一,即函数属性:
#ifndef DEBUG
#define __nodebugonly
#else
#define __nodebugonly __attribute__((error("Function not supported")))
#endif
void Func1(int arg) __nodebugonly;
一种方法是不定义函数。这将导致链接时出错 如果使用gcc,则可以使用其扩展之一,即函数属性:
#ifndef DEBUG
#define __nodebugonly
#else
#define __nodebugonly __attribute__((error("Function not supported")))
#endif
void Func1(int arg) __nodebugonly;
请澄清“抛出错误”。这是一个编译错误吗?运行时错误?在使用站点提供错误描述和代码。确切的错误是:“错误:表达式必须具有(指向-)函数类型”。。。。此错误在调用此函数的位置抛出。消息是否如此重要?请澄清“抛出错误”。这是一个编译错误吗?运行时错误?在使用站点提供错误描述和代码。确切的错误是:“错误:表达式必须具有(指向-)函数类型”。。。。此错误在调用此函数的位置抛出。消息是否如此重要?请澄清“抛出错误”。这是一个编译错误吗?运行时错误?请提供错误说明和使用说明中的代码