c - 尝试使用 C 中的宏为文本着色,但得到 : error: expected ')'

标签 c compiler-errors

我正在尝试用 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

请注意通过 xstrstr 绕行,因为正如 @John Bollinger 正确评论的那样,字符串化会阻止宏扩展。

关于c - 尝试使用 C 中的宏为文本着色,但得到 : error: expected ')' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55028007/

相关文章:

java - System.getProperty ("java.classpath") = null?

c - 如何恢复被 sigwaitinfo/sigwait 挂起的线程?如何检查信号队列?

c - 未定义对 'gnutls_...' 函数的引用

c - 为什么我们需要针对不同的平台(例如 Windows/Linux)进行编译?

c - Kernighan 和 Ritchie 练习 2-2 调试?

java - 如何在hadoop map reduce中设置使用JNI创建的库文件的路径

c++ - fatal error : auc_.cpp:2:10: fatal error: omp.h: No such file or directory

c# - 无法从 ObservableCollection<string> 转换为 ObservableCollection`1<string>

java - 错误: cannot find symbol of Parser

visual-studio - 使用.net native 工具链进行编译时,Visual Studio报告System.Reflection.MissingMetadataException