我正在尝试用 C 语言为我 future 的项目编写一个基于宏的终端字符串着色器。到目前为止,我所拥有的是:
#define ANSI_RED "\x1b[31m"
#define ANSI_GREEN "\x1b[32m"
#define ANSI_YELLOW "\x1b[33m"
#define ANSI_BLUE "\x1b[34m"
#define ANSI_MAGENTA "\x1b[35m"
#define ANSI_CYAN "\x1b[36m"
#define ANSI_RESET "\x1b[0m"
#define ANSI_COLOR(color, string) color string ANSI_RESET
#define FOREGROUND 38
#define BACKGROUND 48
#define RGB_COLOR(plane, r, g, b, string) "\033[" plane ";" r ";" g ";" b "m" string ANSI_RESET
ANSI_COLOR
宏工作得很好,但是当我尝试像这样使用 RGB_COLOR
宏时:
printf( RGB_COLOR(FOREGROUND, 248, 42, 148, "Starting the server:\n") );
我得到一个错误:
/c-http-server/main.c:17:23: error: expected ')'
printf( RGB_COLOR(FOREGROUND, 248, 42, 148, "Starting the server:\n") );
^
/c-http-server/libs/c-chalk/chalk.h:11:20: note: expanded from macro 'FOREGROUND'
#define FOREGROUND 38
^
/c-http-server/main.c:17:11: note: to match this '('
printf( RGB_COLOR(FOREGROUND, 248, 42, 148, "Starting the server:\n") );
我在 SO 上寻找过这个问题,大多数解决方案都是关于找到额外的 ')',但我在我的代码中找不到。
如果有人能帮我找到问题,我会很高兴,也许我只是瞎了眼,错过了一些明显的东西。
最佳答案
看起来您正在尝试连接字符串和整数,这是不可能的。
作为快速修复,您可以尝试
#define FOREGROUND "38"
#define BACKGROUND "48"
像这样使用它
printf( RGB_COLOR(FOREGROUND, "248", "42", "148", "Starting the server:\n") );
另一方面,stringize 应该是可能的(而且更干净)参数(未经测试):
#define xstr(a) str(a)
#define str(a) #a
#define RGB_COLOR(plane, r, g, b, string) "\033[" str(plane) ";" str(r) ";" str(g) ";" str(b) "m" string ANSI_RESET
请注意通过 xstr
和 str
绕行,因为正如 @John Bollinger 正确评论的那样,字符串化会阻止宏扩展。
关于c - 尝试使用 C 中的宏为文本着色,但得到 : error: expected ')' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55028007/