# Associating cpu and target with C source file in my build.zig

**URL:** <https://ziggit.dev/t/associating-cpu-and-target-with-c-source-file-in-my-build-zig/3755>\
**Category:** Help\
**Tags:** build-system\
**Created:** [March 29, 2024, 11:20pm UTC](https://ziggit.dev/t/associating-cpu-and-target-with-c-source-file-in-my-build-zig/3755 "2024-03-29T23:20:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![biosbob](https://ziggit.dev/user_avatar/ziggit.dev/biosbob/32/1987_2.png) [@biosbob](https://ziggit.dev/u/biosbob)\
**Post date:** [March 29, 2024, 11:20pm UTC](https://ziggit.dev/t/associating-cpu-and-target-with-c-source-file-in-my-build-zig/3755/1 "2024-03-29T23:20:53Z")

</div>

i just moved to 0.12.0-dev.3489, where i updated my current use of `zig build-exe` as follows (scraping some lines from my `makefile`):

```zig
ZIGOPTS=\
	--name main \
	-fno-lto \
	-mcpu cortex_m0plus \
	-target thumb-freestanding-eabi \
	-O ReleaseSmall \
	-fentry=__em_program_start \
	--script etc/linkcmd.ld \

exe:
	zig build-exe $(ZIGOPTS) $(ZIGEXE) src/main.zig etc/startup.c

```

i’m in the process of moving to a `build.zig` that does the same thing, but have run into some problems… here’s what i started with:

```zig
pub fn build(b: *std.Build) void {
    b.verbose = true;
    const target = b.resolveTargetQuery(.{
        .cpu_arch = .thumb,
        .cpu_model = .{ .explicit = &std.Target.arm.cpu.cortex_m0plus },
        .os_tag = .freestanding,
        .abi = .eabi,
    });
    const optimize = std.builtin.OptimizeMode.ReleaseSmall;
    const exe = b.addExecutable(.{
        .name = "main",
        .root_source_file = .{ .path = "src/main.zig" },
        .target = target,
        .optimize = optimize,
    });
    exe.addCSourceFile(.{
        .file = .{ .path = "etc/startup.c" },

        // not sure if i need this???

        //.flags = &.{
        // "-mcpu cortex_m0plus",
        // "--target thumb-freestanding-eabi",
        //},
    });
    exe.setLinkerScript(.{ .path = "etc/linkcmd.ld" });
    exe.entry = .{ .symbol_name = "__em_program_start" };
    b.installArtifact(exe);
}

```

when i build, i get the following errors from the linker:

```sh
C:\tools\zig-dev\zig.exe build-exe -fentry=__em_program_start C:\Users\biosb\gitrepos\zigem\etc\startup.c -OReleaseSmall -target thumb-freestanding-eabi -mcpu cortex_m0plus -Mroot=C:\Users\biosb\gitrepos\zigem\src\main.zig --cache-dir C:\Users\biosb\gitrepos\zigem\zig-cache --global-cache-dir C:\Users\biosb\AppData\Local\zig --name main --script C:\Users\biosb\gitrepos\zigem\etc\linkcmd.ld --listen=-
install
+- install main
   +- zig build-exe main ReleaseSmall thumb-freestanding-eabi failure
error: warning(link): unexpected LLD stderr:
ld.lld: warning: Linking two modules of different target triples: 'C:\Users\biosb\gitrepos\zigem\zig-cache\o\c9f1fc0feff79cdfb0dfcf745fd4a745\main.o' 
is 'thumb-unknown-unknown-eabi' whereas 'ld-temp.o' is 'thumbv4t-unknown-unknown-eabi'

```

i would have thought my specification of the `target` would apply to `etc/startup.c` as well… note that in my hand-crafted call to `zig build-exe` in my `makefile` that `etc/startup.c` appears at the **END** of the command-line…

reading about the `.flags` field when adding my C source file, i fiddled around with various options here without success… it appears these are passed through to clang via `-cflags` but i could never seem to match my orignal `zig build-exe -mcpu and -target’ options…

FWIW, i passed the `--verbose-cc` option to `zig build-exe` (hoping to learn how it invokes clang) but nothing came out…

given that my original `makefile` “works” (i can actually build/load executables on my bare-metal MCU), i could use a little guidance in creating an equivalent `build.zig`…

---

<div class="post-metadata">

**Author:** ![biosbob](https://ziggit.dev/user_avatar/ziggit.dev/biosbob/32/1987_2.png) [@biosbob](https://ziggit.dev/u/biosbob)\
**Post date:** [March 29, 2024, 11:29pm UTC](https://ziggit.dev/t/associating-cpu-and-target-with-c-source-file-in-my-build-zig/3755/2 "2024-03-29T23:29:33Z")

</div>

just skimmed through [this](https://ziggit.dev/t/how-to-cross-compile-in-the-new-zig-master/2700/4) on some recent changes to cross-compilation)…

not sure i fully grok this yet, but should i be creating a separate module that encapsulates my C file – and presumably reuse the same `target` therein ???

as another approach, could i `@cInclude` my C code in some other `.zig` module that presumably **IS** built consistently with my `main.zig` ???

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [March 30, 2024, 12:00am UTC](https://ziggit.dev/t/associating-cpu-and-target-with-c-source-file-in-my-build-zig/3755/3 "2024-03-30T00:00:59Z")

</div>

There is an open issue for that:

> <https://github.com/ziglang/zig/issues/14962>
>
> Extracted from https://github.com/ziglang/zig/pull/14647.
> 
> There are two probl…ems here. One is https://github.com/ziglang/zig/issues/14908. The other looks like zig is failing to put the subarch into the llvm triple. This issue is to track the latter issue.
> 
> \`\`\`
> ld.lld: warning: Linking two modules of different target triples: '/home/andy/Downloads/zig/zig-cache/o/610cf441612621c1d4bea0844fb8daea/test.o' is 'arm-unknown-linux-musleabihf' whereas 'ld-temp.o' is 'armv6kz-unknown-linux-musleabihf'
> \`\`\`
> 
> https://github.com/ziglang/zig/blob/b4d58e93ea4d0bbfe674f80d301279d302fe8fc8/test/tests.zig#L1019-L1066

try

```zig
    exe.want_lto = false;

```
