#No warnings when using build.zig to compile a pure C project.

1 messages · Page 1 of 1 (latest)

real cairn
#

Hello,

I've tried to write a build.zig file to get rid of autotools of an old C project. While the project compiles fine, I have absolutely zero warnings when building my project with zig build. Can you tell me what I've done wrong with my build.zig file, please?

Removing the cache before compilation doesn't help.

I'm using Zig version 0.11.0-dev.3316+ec58b475b.

Here is a minimal working example:

main.c

#include <stdio.h>

int global = 1;

int main() {
  int x;
  int global = global;
  printf("x = %d, and global = %d", x, global);
  return 0;
}

build.zig

const std = @import("std");
const Build = std.build;

pub fn build(b: *Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const exe = b.addExecutable(.{
        .name = "test",
        .optimize = optimize,
        .target = target,
    });
    exe.addCSourceFiles(&.{"main.c"}, &.{
        "-std=c17",
        "-Wpedantic",
        "-Wall",
        "-Wextra",
        "-Wshadow",
    });

    exe.linkSystemLibrary("c");

    b.installArtifact(exe);

    const run = b.addRunArtifact(exe);
    run.step.dependOn(b.getInstallStep());
}

Compiling with gcc -Wall -Wextra -Wpedantic -Wshadow main.c returns the following warnings:

main.c: In function ‘main’:
main.c:7:7: warning: declaration of ‘global’ shadows a global declaration [-Wshadow]
    7 |   int global = global;
      |       ^~~~~~
main.c:3:5: note: shadowed declaration is here
    3 | int global = 1;
      |     ^~~~~~
main.c:8:3: warning: ‘x’ is used uninitialized [-Wuninitialized]
    8 |   printf("x = %d, and global = %d", x, global);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.c:6:7: note: ‘x’ was declared here
    6 |   int x;
      |       ^

Compiling with zig build produces nothing.

Best regards.

valid estuary
#

Have you tried --fverbose-clang or whatver it is called when you run your zig build?

#

Not at my computer so can't check exactly

real cairn
fiery burrow
#

zig build -h gives me these, maybe one will be what you need?

real cairn
#

Hello @fiery burrow,
I've seen this, but that doesn't solve my case unfortunately.

#

If you need more context, here is a minimal working example:

main.c

#include <stdio.h>

int global = 1;

int main() {
  int x;
  int global = global;
  printf("x = %d, and global = %d", x, global);
  return 0;
}

build.zig

const std = @import("std");
const Build = std.build;

pub fn build(b: *Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const exe = b.addExecutable(.{
        .name = "test",
        .optimize = optimize,
        .target = target,
    });
    exe.addCSourceFiles(&.{"main.c"}, &.{
        "-std=c17",
        "-Wpedantic",
        "-Wall",
        "-Wextra",
        "-Wshadow",
    });

    exe.linkSystemLibrary("c");

    b.installArtifact(exe);

    const run = b.addRunArtifact(exe);
    run.step.dependOn(b.getInstallStep());
}

Compiling with gcc -Wall -Wextra -Wpedantic -Wshadow main.c returns the following warnings:

main.c: In function ‘main’:
main.c:7:7: warning: declaration of ‘global’ shadows a global declaration [-Wshadow]
    7 |   int global = global;
      |       ^~~~~~
main.c:3:5: note: shadowed declaration is here
    3 | int global = 1;
      |     ^~~~~~
main.c:8:3: warning: ‘x’ is used uninitialized [-Wuninitialized]
    8 |   printf("x = %d, and global = %d", x, global);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.c:6:7: note: ‘x’ was declared here
    6 |   int x;
      |       ^

Compiling with zig build produces nothing.

#

Description has been updated.

prime relic
#

Zig does caching in the background for you, so won't recompile artifacts if they haven't changed. There's a chance you're getting confused by that - maybe the warnings happened first time, but you didn't notice, and since then no recompilation has been actually happening, so the warnings haven't printed? If you delete the zig-cache directory it should force your C objects to get rebuilt on the next run.

unkempt sinew
#

ITS MLUGG! THAT ONE GUY FROM PORTAL 2

real cairn
valid estuary
#

Yeah, I don't remember actually getting any compiler warnings when compiling C files, but that might also be because it automatically turns them off

#

Yeah no warnings even with flags, so it's probably not an issue specific to you

real cairn
#

@valid estuary should I open an issue then?

real cairn
#

I've opened an issue.