之前在编译一个 tweak 时,死活编译不过。今天终于找到原因了
先说结论:.x 和 .xm 后缀名文件,如果单行块注释中包含 //
会导致编译失败。单行块注释中 //
后面的内容也会被 Theos 当注释处理
如何解决:使用多行块注释,或者行注释,或者在 //
中间插入空格
用最小代码重现这个 bug,文件保存为 main.x 或 main.xm,代码如下
代码 1
#include <stdio.h>
/* https://iosre.com/ */
int main() {
/* Hello world! */
printf("Hello world!\n");
return 0;
}
make 编译,报错
main.x:8: error: fell off the face of the planet when we found a '}'
问题就出在第一个注释,如果单行块注释中包含 //
就会出现这个 bug
代码 2
#include <stdio.h>
/* https://iosre.com/ */
int main() {
printf("Hello world!\n");
return 0;
}
代码 2 可以编译,但链接失败找不到 _main
符号
logos 预编译生成的 main.x.m 文件,很明显注释后面的代码都当注释处理了
#line 1 "main.x"
#include <stdio.h>
猜想
#include <stdio.h>
// |-- 这是块注释
// |
// | |-- 这被当成了注释的注释(实际上没有注释的注释这种说法)
// | |
/* https://iosre.com/ */
int main() {
printf("Hello world!\n");
return 0;
}
代码 1 的等效代码。第一个注释的 /*
和第二个块注释的 */
组成了匹配
#include <stdio.h>
/* https:
int main() {
/* Hello world! */
printf("Hello world!\n");
return 0;
}
代码 2 的等效代码。/*
注释掉了后面所有代码,所以链接失败找不到 _main
符号
#include <stdio.h>
/* https:
int main() {
printf("Hello world!\n");
return 0;
}
等效代码编译报错和前面一样。
结案。