Zig“翻译c”不翻译主要功能

ice*_*000 3 c zig

我创建了一个 C 文件:

int main() {
  return 1;
}
Run Code Online (Sandbox Code Playgroud)

我使用 Zig 的translate-c命令行选项来生成一个 zig 文件,但我只得到了一些全局变量声明,例如

pub const __GCC_ATOMIC_TEST_AND_SET_TRUEVAL = 1;
pub const __FLT16_MAX_EXP__ = 15;
pub const __BIGGEST_ALIGNMENT__ = 16;
pub const __SIZEOF_FLOAT__ = 4;
pub const __INT64_FMTd__ = c"ld";
pub const __STDC_VERSION__ = c_long(201112);
... // and many
Run Code Online (Sandbox Code Playgroud)

并没有main找到任何功能。但是如果我把函数名改成myFunction这样:

int myFunction(int a) {
  return a;
}
Run Code Online (Sandbox Code Playgroud)

当我重新生成它时会出现一个函数:

pub export fn myFunction(a: c_int) c_int {
    return a;
}
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?zigtranslate-c函数的规则是什么?

and*_*wrk 9

当问到这个问题时,translate-c 还不支持带有未指定参数的函数。这可以通过使用--verbose-cimport

test.c:1:5: warning: unsupported type: 'FunctionNoProto'
test.c:1:5: warning: unable to resolve prototype of function 'main'
Run Code Online (Sandbox Code Playgroud)

在 C 中,如果您将参数留空,则实际上不是零参数,而是未指定的。您必须使用void来表示“无参数”。

所以这就是为什么第二个例子有效 - 因为参数列表不为空。

但是,从e280dce3 开始,Zig 支持翻译带有未指定参数的 C 函数,问题中的示例变成了这个 Zig 代码:

pub export fn main() c_int {
    return 1;
}
Run Code Online (Sandbox Code Playgroud)