ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
33.7k stars 2.47k forks source link

`zig cc` cannot compile from stdin #10389

Open konsolas opened 2 years ago

konsolas commented 2 years ago

Zig Version

0.9.0

Steps to Reproduce

Running this command to compile a program from stdin

echo 'int main(){}' | zig cc -xc -

Expected Behavior

The same command succeeds with clang.

echo 'int main(){}' | clang -xc -

produces an executable a.out that does nothing.

Actual Behavior

zig cc gives the following output and nothing else:

error: FileNotFound

This means that zig cc is not a drop-in solution for projects that dynamically generate c/c++ source code during the build process which is directly piped to the compiler.

This issue was fixed previously but in a different context: https://github.com/ziglang/zig/issues/6271

iacore commented 2 years ago

I'm not sure what kind of shell you are using that uses > as pipe symbol.

In fish:

> echo 'int main() {}' | zig cc -xc /dev/stdin -o out
ld.lld: error: undefined symbol: main

It seems like zig cc does read from /dev/stdin, but cannot understand it.

The following works:

> echo 'int main() {}' > a.c
> zig cc a.c
konsolas commented 2 years ago

The > was a typo, but the results are the same. Also note that /dev/stdin isn't available on e.g. Windows.

iacore commented 2 years ago

You can create a temp file and work around this. This is a weird use case.

praschke commented 1 year ago

#14089 incompatibility in the wild

# echo 'extern void b(void);int a(void){b();return 0;}' | zig cc -c -x c - -o tmpunwind.o
-:1:1: error: unable to build C object: FileNotFound

Dupe of #12777