How to compile C programs on Mac?

I’m following NixOS - Nix Pills to build a derivation for a simple C program.

Derivation definition:
simple = derivation { name = “simple”; builder = “${bash}/bin/bash”; args = [./]; gcc = gcc; coreutils = coreutils; src = ./simple.c; system = builtins.currentSystem; }

export PATH="$coreutils/bin:$gcc/bin"
mkdir $out
gcc --version
gcc -o $output/simple $src

When building the derivation, I’m getting this error. Can someone help? Does “gcc” not work on Mac? I tried to print out the version of gcc in the builder script but I didn’t see it in the output. Where can I see it? Thanks!

nix-store -r /nix/store/mcj1dsyfhrcspbh5nq11za20lc87z5if-simple.drv
these derivations will be built:
building '/nix/store/mcj1dsyfhrcspbh5nq11za20lc87z5if-simple.drv'...
/nix/store/h2ig4svfh8xlc2z0p3hjrzll0fq7yfb5-simple.c: In function 'main':
/nix/store/h2ig4svfh8xlc2z0p3hjrzll0fq7yfb5-simple.c:2:5: warning: implicit declaration of function 'puts' [-Wimplicit-function-declaration]
    2 |     puts("Simple!");
      |     ^~~~
ld: can't open output file for writing '/simple.ld_o9FBa5', errno=30 for architecture x86_64
collect2: error: ld returned 1 exit status
builder for '/nix/store/mcj1dsyfhrcspbh5nq11za20lc87z5if-simple.drv' failed with exit code 1
error: build of '/nix/store/mcj1dsyfhrcspbh5nq11za20lc87z5if-simple.drv' failed


gcc -o $out/simple $src

Thus out in place of output? (You seem to have made a typo :wink: .)

1 Like

OMG! :sweat_smile:

That totally fixed it. I was going down the rabbit hole of debugging gcc since I haven’t worked with C/C++ for years and thought I did something wrong there.