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

时间:2013-11-15 13:07:14

标签: 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);
}

在我的脑海中这很好用,但是编译器不喜欢它...这里的HERE是给出了以下错误:

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

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

1 个答案:

答案 0 :(得分:3)

您在"宏中无法获得无与伦比的#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