c++ - 带有引号和反斜杠的 C/C++ 宏错误

标签 c++ c macros

我正在尝试用一些不错的 C 宏来为脚本语言的测试框架增添趣味,这样就不必多次编写相同的代码......所以,我有代码:

TEST_CASE("Variables", "[vm_variables]")
{
nap_runtime* runtime = nap_runtime_create(0);
REQUIRE(runtime != 0);

nap_bytecode_chunk* bytecode = nap_runtime_compile(runtime, 
                 "                            \
                  int a;                      \
                  a = 2;                      \
                 "
                );
REQUIRE(bytecode != 0);
int t = nap_runtime_execute(runtime, bytecode);
REQUIRE(1 == t);
REQUIRE(2 == nap_runtime_get_int(runtime, "a"));
free(runtime);
}

现在它创建了一个运行时,在其中执行代码 (int a; a=2;),这可以工作......

我正在考虑将创建部分提取到宏中,如下所示,以一种我只需编写脚本的方式......所以我想出了:

#define SCRIPT_START nap_runtime* runtime = nap_runtime_create(0);              \
                 nap_bytecode_chunk* bytecode = nap_runtime_compile(runtime,    \
                 "

#define SCRIPT_END  "  \
                ); \
                int t = nap_runtime_execute(runtime, bytecode); 

TEST_CASE("Variables", "[vm_variables]")
{
SCRIPT_START                    \ <<------------- HERE
    int a;                      \
    a = 2;                      \
SCRIPT_END

REQUIRE(2 == nap_runtime_get_int(runtime, "a"));
free(runtime);
}

在我看来,这工作得很好,但编译器不喜欢它......在哪里它给了我以下错误:

test.cpp: error: missing terminating " character

我已经改了,修改了好几轮,还是一样...我做错了什么?

编辑: 使用 -E 编译后,相关部分如下:

44633     {                                                                           
44634     nap_runtime* runtime = nap_runtime_create(0); nap_bytecode_chunk* bytecode = nap_runtime_compile(runtime, "
44635         int a;                                                                  
44636         a = 2;                                                                  
44637     "                      );                     int t = nap_runtime_execute(runtime, bytecode);                     REQUIRE(1 == t);
44638

因此,带有 SCRIPT_START 宏的行中的 \ 似乎被忽略了......以及以下其他行。为什么?

EDIT2 实验并享受编译器的乐趣:

现在,我放了两个反斜杠:

TEST_CASE("Variables", "[vm_variables]")
{
SCRIPT_START                    \\ <<------------- HERE
    int a;                      \\
    a = 2;                      \\
SCRIPT_END

通过-E的输出是:

44634     nap_runtime* runtime = nap_runtime_create(0); nap_bytecode_chunk* bytecode = nap_runtime_compile(runtime, " \
44635         int a; \                                                                
44636         a = 2; \                                                                
44637     "                      );                     int t = nap_runtime_execute(runtime, bytecode);                     REQUIRE(1 == t);

错误几乎相同:

test.cpp:19:5: error: missing terminating " character
test.cpp:19:5: error: stray ‘\’ in program

无论如何,使用两个反斜杠它会生成“正确”的代码,但仍然无法编译:)

最佳答案

#define 宏中不能有不匹配的 "。宏的内容必须是可标记的。如果满足以下条件,则字符串文字标记不是完整标记:它没有开始和结束引号。

关于反斜杠等的其他答案是错误的。宏中不能有不匹配的引号。请参阅此示例程序,该程序无法编译:

$ cat test.c
#include <stdio.h>

#define BEGIN_QUOTE "
#define END_QUOTE "

int main() {
    printf(BEGIN_QUOTE hello world!\n END_QUOTE);
    return 0;
}

$ gcc -Wall test.c
test.c:3:21: warning: missing terminating " character [enabled by default]
test.c:4:19: warning: missing terminating " character [enabled by default]
test.c: In function ‘main’:
test.c:7:5: error: missing terminating " character
test.c:7:24: error: ‘hello’ undeclared (first use in this function)
test.c:7:24: note: each undeclared identifier is reported only once for each function it appears in
test.c:7:30: error: expected ‘)’ before ‘world’
test.c:7:30: error: stray ‘\’ in program
test.c:7:30: error: missing terminating " character

您必须将引号留在宏之外并明确地编写它们:

SCRIPT_START
"            \
    int a;   \
    a = 2;   \
"
SCRIPT_END

关于c++ - 带有引号和反斜杠的 C/C++ 宏错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20001833/

相关文章:

c - 在 C 中调用函数而不是宏

c++ - GLSL 类型不一致

c++ - cmake 引用了以下导入的目标,但丢失了

c - 保存一个结构以供后期获取

c - 我需要一些关于 C 语言的基本游戏想法的帮助。 [初学者]

使用其他宏作为参数调用 C 宏

c++ - 如何更改 C 函数中参数的地址?

c++ - 工程 bool 比较等于真假,为什么?

C 程序跳过用户输入?

c++ - 编译器之间的宏扩展顺序混淆