我在使用宏函数时遇到以下问题。这一直有效,直到我添加了 print_heavyhitters 函数。我在 m61.h 中有这个:
#if !M61_DISABLE
#define malloc(sz) m61_malloc((sz), __FILE__, __LINE__)
#define free(ptr) m61_free((ptr), __FILE__, __LINE__)
#define realloc(ptr, sz) m61_realloc((ptr), (sz), __FILE__, __LINE__)
#define calloc(nmemb, sz) m61_calloc((nmemb), (sz), __FILE__, __LINE__)
#define print_heavyhitters(sz) print_heavyhitters((sz), __FILE__, __LINE__)
#endif
在 m61.c 中,除了 print_heavyhitters(sz) 之外,所有这些函数都很好。我得到“- 函数 print_heavyhitters 的宏使用错误:
- Macro usage error for macro:
print_heavyhitters
- Syntax error
m61.c:
#include "m61.h"
...
void *m61_malloc(size_t sz, const char *file, int line) {...}
void print_heavyhitters(size_t sz, const char *file, int line) {...}
最佳答案
您对宏及其要扩展到的函数使用相同的名称。
关于c - 为函数使用宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12711158/