zig icon indicating copy to clipboard operation
zig copied to clipboard

exporting a C main function regressed

Open nektro opened this issue 3 years ago • 4 comments

Zig Version

0.10.0-dev.3665+9cf667a21

Steps to Reproduce

const string = [*:0]const u8;
const stringarray = [*:null]const ?string;

pub export fn main(argc: c_int, argv: stringarray) c_int {
    _ = argc;
    _ = argv;
    return 0;
}

Expected Behavior

successful compile and run

Actual Behavior

/home/meghan/src/zig/lib/std/start.zig:571:17: error: expected return type of main to be 'void', '!void', 'noreturn', 'u8', or '!u8'
                @compileError(bad_main_ret);
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/meghan/src/zig/lib/std/start.zig:510:12: note: called from here
    return @call(.{ .modifier = .always_inline }, callMain, .{});
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/meghan/src/zig/lib/std/start.zig:460:36: note: called from here
    return initEventLoopAndCallMain();
           ~~~~~~~~~~~~~~~~~~~~~~~~^~
/home/meghan/src/zig/lib/std/start.zig:424:17: note: called from here
    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

nektro avatar Aug 22 '22 02:08 nektro

This doesn't seem to work with stage1 either:

$ zig run a.zig -fstage1
./lib/std/start.zig:571:17: error: expected return type of main to be 'void', '!void', 'noreturn', 'u8', or '!u8'
                @compileError(bad_main_ret);
                ^

Vexu avatar Aug 22 '22 07:08 Vexu

yeah there seems to be odd conditions where it skips the check, but the minimal repro caught it so i thought it might be a regression

https://github.com/nektro/mount.ufs/blob/master/src/main.zig#L20 builds fine with stage1 even though i use the c_int main return here

nektro avatar Aug 22 '22 08:08 nektro

it already works. use export

andrewrk avatar Jul 16 '24 02:07 andrewrk

sorry, I got tricked by #20645, this is indeed a regression.

andrewrk avatar Jul 16 '24 02:07 andrewrk

Please remember to fill out Steps To Reproduce. If you had, I would have noticed that you omitted -lc and then we wouldn't have wasted everybody's time.

This is working as designed. You must use -lc if you want to export a main that libc calls.

andrewrk avatar Nov 08 '24 00:11 andrewrk