ein*_*ica 5 c++ debugging namespaces compiler-errors nvcc
我试图找出一堆代码中哪里缺少命名空间的右大括号(即得到:
\n\nAt end of source: error: expected a "}"\nRun Code Online (Sandbox Code Playgroud)\n\n错误)。也许有更聪明的方法可以解决这个问题,但是 - 我想尝试在代码中的不同点检查当前的命名空间是什么。我不需要宏;我不需要字符串;我不需要运行时存在的东西。我只想让编译器以某种方式打印名称空间。如果它位于#error、 a#warning或某些构造中,如果编译失败,会产生其中的命名空间错误,对我来说没问题。
我想概括的例子:
\n\nnamespace ns1 { }\nnamespace ns2 {\nnamespace ns3 {\n\n// MAGIC GOES HERE\n\n}\nRun Code Online (Sandbox Code Playgroud)\n\n这个源文件缺少某个}地方。但是 - 我忘记关闭 ns1 了吗?或者也许是ns2?我不记得了——它们离我感兴趣的线很远。所以我想插入一些神奇的东西,它会告诉我我现在所在的命名空间。好吧,对于这个例子,我可以写:
namespace ns1 { }\nnamespace ns2 {\nnamespace ns3 {\n\nvoid ns_detector() { return 0; }\n\n}\nRun Code Online (Sandbox Code Playgroud)\n\n使用 GCC 6.3 时,出现错误:
\n\nb.cpp: In function \xe2\x80\x98void ns2::ns3::ns_detector()\xe2\x80\x99:\nb.cpp:5:29: error: return-statement with a value, in function returning \'void\' [-fpermissive]\n void ns_detector() { return 0; }\n ^\nb.cpp: At global scope:\nb.cpp:7:1: error: expected \xe2\x80\x98}\xe2\x80\x99 at end of input\n }\n ^\nRun Code Online (Sandbox Code Playgroud)\n\n该错误的第一行告诉我需要知道什么:它的 ns2 尚未关闭。但是——这不太可靠。对于我用 nvcc 编译的更长、更复杂的代码片段,使用“ns detector”函数只会给我类似的东西:
\n\n/path/to/file.cu(135): error: return value type does not match the function type\n\nAt end of source: error: expected a "}"\nRun Code Online (Sandbox Code Playgroud)\n\n所以,我需要更强大的东西。
\n\n笔记:
\n\n对 @Acorn 良好且简单的解决方案的(可能)改进,可以多次使用,用于在同一文件中的多个位置进行检测:
\n\n#define CONCATENATE(s1, s2) s1##s2\n#define EXPAND_THEN_CONCATENATE(s1, s2) CONCATENATE(s1, s2)\n#define UNIQUE_IDENTIFIER(prefix) EXPAND_THEN_CONCATENATE(prefix, __LINE__)\n\n#define DETECT_NAMESPACE \\\nstruct UNIQUE_IDENTIFIER(namespace_detector_on_line_) { \\\n void f() { look_at_the_class_name_above = 0; } \\\n};\nRun Code Online (Sandbox Code Playgroud)\n\n现在您应该能够在许多不同的行上写入 DETECT_NAMESPACE。此外,宏定义可以放置在单独的文件中。
\n\n对于示例中的代码,这将给出:
\n\nb.cpp: In member function \xe2\x80\x98void ns2::ns3::namespace_detector_on_line_13::f()\xe2\x80\x99:\nb.cpp:5:93: error: \xe2\x80\x98look_at_the_class_name_above\xe2\x80\x99 was not declared in this scope\n #define DETECT_NAMESPACE struct UNIQUE_IDENTIFIER(namespace_detector_on_line_) { void f() { look_at_the_class_name_above! = 0; } };\n ^\nb.cpp:13:1: note: in expansion of macro \xe2\x80\x98DETECT_NAMESPACE\xe2\x80\x99\n DETECT_NAMESPACE\n ^~~~~~~~~~~~~~~~\nb.cpp: At global scope:\nb.cpp:15:1: error: expected \xe2\x80\x98}\xe2\x80\x99 at end of input\n }\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
739 次 |
| 最近记录: |