C++ C/C++;带引号和反斜杠的宏错误

C++ C/C++;带引号和反斜杠的宏错误,c++,c,macros,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, "

我正在尝试用一些漂亮的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);
}
就目前而言,它创建了一个运行时,在其中执行代码(
inta;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);
}
我改变了,修改了几圈,还是一样的。。。我做错了什么

编辑: 使用
-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
因此,似乎忽略了带有
脚本\u START
宏的行中的
\
。。。还有下面几行。为什么?

EDIT2尝试并享受编译器带来的乐趣:

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

TEST_CASE("Variables", "[vm_variables]")
{
SCRIPT_START                    \\ <<------------- HERE
    int a;                      \\
    a = 2;                      \\
SCRIPT_END
误差几乎相同:

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

告诉编译器不要实际编译代码,而只运行预处理器,并查看预处理后的输出以查看真正生成的内容。例如,使用GCC或clang时,您使用
-e
选项,但不知道如何在Visual Studio(或其他IDE)中执行此操作.GCC允许您在使用
GCC-E
运行时查看预处理器的输出。我不知道您使用的是什么编译器,但如果它有一个模式可以让您查看预处理的输出,我建议您尝试查看上面可能发生的情况。目前还没有人明确说明:为什么在这里使用宏?这似乎会导致可怕的错误(就像这个)。你唯一要做的就是创建一个字符串,并将其传递到你的函数中——为什么不调用一个接受字符串的函数呢?我只想提取“fixture”"将单元测试框架的功能转换为几行宏。@多姆黑格:如果你有很多很多测试,并且宏背后的部分实际上永远不会改变,那么将这些东西隐藏在宏背后是有意义的。一旦宏背后的东西起作用并且是可靠的,那么在这个框架中访问测试用例的人只需要理解大多数情况下都是测试用例,而不是框架的其余部分。好的一点,我当然没有考虑。我将删除我的误导性答案。
test.cpp:19:5: error: missing terminating " character
test.cpp:19:5: error: stray ‘\’ in program
$ 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