Search code examples
czig

Zig "translate c" doesn't translate main function


I created a C file:

int main() {
  return 1;
}

I used Zig's translate-c command line option to generate a zig file, and I only get some global variable declarations like

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

And no main function is found. But if I change the function name to myFunction like this:

int myFunction(int a) {
  return a;
}

A function appears when I re-generate it:

pub export fn myFunction(a: c_int) c_int {
    return a;
}

Am I missing something? What's the rule of zig's translate-c function?


Solution

  • When this question was asked, translate-c did not yet support functions with unspecified parameters. This was visible by using --verbose-cimport:

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

    In C, if you leave the parameters empty, it's not actually zero parameters, it's unspecified. You have to use void to mean "no parameters".

    So that's why the second example worked - because the parameter list was not empty.

    However as of e280dce3, Zig supports translating C functions with unspecified parameters, and the example from the question turns into this Zig code:

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