C/C++ 带有引号和反斜杠的宏错误
C/C++ macro error with quotes and backslashes
我正在尝试用一些漂亮的 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
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 错误:未在此范围内声明'reverse'