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

C/C++ macro error with quotes and backslashes

本文关键字:错误 C++      更新时间:2023-10-16

我正在尝试用一些漂亮的 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