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

时间:2018-04-18 05:50:48

标签: c zig

我创建了一个C文件:

a[MyArrayName]

我使用Zig的int main() { return 1; } 命令行选项生成zig文件,我只获得了一些全局变量声明,如

translate-c

未找到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 个功能。但是,如果我将函数名称更改为main,请执行以下操作:

myFunction

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

int myFunction(int a) {
  return a;
}

我错过了什么吗? zig pub export fn myFunction(a: c_int) c_int { return a; } 函数的规则是什么?

1 个答案:

答案 0 :(得分:3)

询问此问题时,translate-c尚不支持带有未指定参数的函数。通过使用--verbose-cimport可见:

test.c:1:5: warning: unsupported type: 'FunctionNoProto'
test.c:1:5: warning: unable to resolve prototype of function 'main'

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

这就是第二个示例起作用的原因-因为参数列表不为空。

但是从e280dce3开始,Zig支持使用未指定的参数转换C函数,问题中的示例变成了以下Zig代码:

pub export fn main() c_int {
    return 1;
}