#Compiling zig to C

1 messages · Page 1 of 1 (latest)

pine garnet
#

A lot of the documentation and tutorials seem to be out of date. Not quite sure how to do it.

I tried just setting the target:

const target = b.standardTargetOptions(.{ .default_target = .{ .ofmt = .c } });

and it does indeed build a C file at zig-out/bin/main.c. However, trying to build it with zig cc ./* gives me an error:

./zig.h:1762:26: error: use of undeclared identifier 'ZIG_TARGET_MAX_INT_ALIGNMENT'
 1762 |     uint16_t alignment = ZIG_TARGET_MAX_INT_ALIGNMENT;
      |                          ^
1 error generated.
./france.c:7348:2: error: non-ASM statement in naked function is not supported
 7348 |  goto zig_block_0;
      |  ^
./france.c:7342:1: note: attribute is here
 7342 | zig_naked zig_noreturn void start__start__124(void) {
      | ^
./zig.h:84:34: note: expanded from macro 'zig_naked'
   84 | #define zig_naked __attribute__((naked))

Not quite sure what's the current standard for doing this

#

Using gcc instead:

acheong@fish ~/P/h/f/z/lib> gcc ./*
./zig.h: In function ‘zig_int_bytes’:
./zig.h:1762:26: error: ‘ZIG_TARGET_MAX_INT_ALIGNMENT’ undeclared (first use in this function)
 1762 |     uint16_t alignment = ZIG_TARGET_MAX_INT_ALIGNMENT;
      |                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
./zig.h:1762:26: note: each undeclared identifier is reported only once for each function it appears in
#

Well, I copied it from main.c to zig.h.

acheong@fish ~/P/h/f/z/lib [1]> gcc ./*
/usr/bin/ld: /tmp/cc2JlVgU.o: in function `_start':
france.c:(.text+0x2059): multiple definition of `_start'; /usr/lib/gcc/x86_64-redhat-linux/14/../../../../lib64/crt1.o:(.text+0x0): first defined here
/usr/bin/ld: /usr/lib/gcc/x86_64-redhat-linux/14/../../../../lib64/crt1.o: in function `_start':
(.text+0x1b): undefined reference to `main'
collect2: error: ld returned 1 exit status

Another related issue: https://github.com/ziglang/zig/issues/13575

#

(btw exact same issues with plain zig build-exe ./src/main.zig -ofmt=c where main.zig is the default example from zig init)