gpt4 book ai didi

c - 相对于调用它的源文件映射宏

转载 作者:行者123 更新时间:2023-11-30 17:33:30 25 4
gpt4 key购买 nike

我正在使用宏(debug_macro)来记录 log.h 文件中定义的调试信息。我需要映射此 debug_macro 来处理特定的调试宏。我在下面的例子中解释了我的期望。

   log.h
=======
#if callee==process1
#define debug_macro proc1_debug_macro
#if callee==process2
#define debug_macro proc2_debug_macro

process1.c
===========
#include log.h
debug_macro <<<<<====== this one should call proc1debug_macro

process2.c
===========
#include log.h
debug_macro <<<<==== this one should call proc2_debug_macro

我是 C 编程新手。请为我提供有关如何实现此功能的任何建议吗?任何帮助将不胜感激。

谢谢

最佳答案

从表面上看,您缺少一些定义,需要在包含 header 之前定义callee:

日志.h

#define process1 10
#define process2 20

#if callee==process1
#define debug_macro proc1_debug_macro
#elif callee==process2
#define debug_macro proc2_debug_macro
#else
#error callee not defined
#endif

process1.c

#define callee process1
#include "log.h"
debug_macro <<<<<====== this one should call proc1debug_macro

process2.c

#define callee process2
#include "log.h"
debug_macro <<<<==== this one should call proc2_debug_macro

我不相信这是最好的方法,但它与您的要求密切相关。

我可能会直接使用不同的调试函数(宏),这样就可以清楚地看出不同的代码在不同的进程中实现调试。或者,更有可能的是,我会在两个进程中使用相同的调试代码。不过,我的要求不一定和你的一样。

请注意,宏名称通常全部大写。此外,一般来说,类函数宏优于类对象宏。也就是说,最好有:

#define debug_macro(a, b c) proc1_debug_macro(a, b, c)

关于c - 相对于调用它的源文件映射宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23734670/

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