4 ms·
I've been confused by the statement that "Zig can compile C Code" for quite some time and reading a couple of blog posts hasn't made it much clearer. Does the
by quirino 4y ago
I've been confused by the statement that "Zig can compile C Code" for quite some time and reading a couple of blog posts hasn't made it much clearer.
Does the Zig Project include a full blown C Compiler? Is it the Zig Compiler with some sort of adaptation to compile C code? Or does it use something like Clang behind the curtains? (In this case it would be responsible for some other parts of the compilation process)
- lionkor 4y agoI believe the answer is two-fold: 1. It uses LLVM as the default backend, in which case that handles C as well 2. Optionally, and in the future by default, the Zig backend (a different one from the LLVM one) includes a C compiler (?) Something like that. Its very powerful and it has the best C integration of any language in my experience (better than C++ since it effectively namespaces C headers).
- quirino 4y agoThanks for the answer but some things are still not entirely clear. Who would be doing the parsing of C code for instance? Clang is also based LLVM but it is responsible for a load of C-specific stuff like parsing the language and feeding it into LLVM for instance. I've got little experience with this stuff so I'm not sure if my questions even make that much sense. (Edit: I believe ptato has answered my question above.)
- kristoff_it 4y agoYou can invoke `zig cc` with the same flags that you would pass to `clang`. Zig cc takes your arguments, inspects them and applies some transformations and then Zig invokes its internal copy of clang with the resulting flags. One example of transformation is enabling ubsan when building in debug mode, another is `-target` which makes Zig add some sysroot flags to enable cross-compilation. In this case all the file operations are handled by clang, Zig basically just sets up all the advanced flags for you when it makes sense to do so. One last example of CLI rewriting is related to the caching system: Zig cc knows how to cache a build by passing to clang the same kind of flags that cmake (or other build systems) would. There's another C-related feature of Zig that works differently: `@cImport()`, which is a builtin that allows you to import C header files directly into a Zig script, in order to use from Zig all the types exposed by the header file. This one translates the C syntax in equivalent Zig syntax. I believe it uses clang's code to parse the C code into an AST, but then it's all Zig logic from there. Lastly, we have a C frontend project going (arocc, linked by ptato) on that we plan to eventually upstream into Zig. This would be a replacement for clang and would work by parsing the C code and translating it into Zig IR, similarly to how the D programming language does it. The only limitation of this approach is that it would only support C, not C++, so we would either have to still keep clang around for C++, or ditch clang but then lose C++ compilation support. That said, even in the case where we keep clang around for C++ support, it would be worth having a custom C frontend for the Zig compiler in order to have more fine-grained control over the compilation process than what we can get from clang, plus the fact that it would make debug builds faster, since we could avoid invoking LLVM completely in that case (ie debug builds) even if you depend on C code.
- whitehexagon 4y agoHow does this work on the assembly support side? I can see inline assembly, something called global assembly, but is Zig build also able to build standalone assembly main.asm type files?
- kristoff_it 4y agoFrom the `zig build-exe` help menu: Supported file types: .zig Zig source code .o ELF object file .o Mach-O (macOS) object file .o WebAssembly object file .obj COFF (Windows) object file .lib COFF (Windows) static library .a ELF static library .a Mach-O (macOS) static library .a WebAssembly static library .so ELF shared object (dynamic link) .dll Windows Dynamic Link Library .dylib Mach-O (macOS) dynamic library .tbd (macOS) text-based dylib definition .s Target-specific assembly source code .S Assembly with C preprocessor (requires LLVM extensions) .c C source code (requires LLVM extensions) .cxx .cc .C .cpp .stub C++ source code (requires LLVM extensions) .m Objective-C source code (requires LLVM extensions) .mm Objective-C++ source code (requires LLVM extensions) .bc LLVM IR Module (requires LLVM extensions) .cu Cuda source code (requires LLVM extensions)
- whitehexagon 4y agoAh! thanks, I'd been using 'zig --help build-obj' vs 'zig build-obj -h'. And just to say the WebAssembly support is amazing, well done and thanks!
- flohofwoe 4y ago> in which case that handles C as well And not just C, but also C++ and Objective-C - including all the hairy stuff to create macOS executables without the Apple toolchain.
- ptato 4y agoZig embeds clang to compile C code. This doesn't add a new dependency since Zig already depends on LLVM. If there is a future where the self-hosted Zig backend is good enough to not depend on LLVM anymore, there might be a reason to use a C compiler written in Zig (possibly https://github.com/Vexu/arocc https://github.com/Vexu/arocc)
- richardwhiuk 4y agoIt adds a new dependency - the c language frontend - aka clang - surely the zig compiler emits LLVM IR, not C code?
- daurnimator 4y agozig supports importing C headers via the special built-in `@cImport`. That feature requires a C language front end to operate. https://ziglang.org/documentation/0.10.1/#Import-from-C-Header-File https://ziglang.org/documentation/0.10.1/#Import-from-C-Head...
- deleted 4y ago[deleted]
- tristan957 4y agoZig has a C backend that supports something like 99% of all Zig if I'm remembering the number correctly. Zig has almost completely solved the bootstrap problem.
- trashburger 4y agoThis isn't quite correct. Yes, it can output C code; however the result is not very readable at all, and fails the DFSG on generated code. It _is_ useful for compiling Zig code to targets which aren't supported by LLVM, however.
- haberman 4y ago
- acqq 4y agoOthers answered direct questions. I'd like to add the link to the use examples demonstrating features of zig for c and c++ compilation available with the default zig installation which aren't directly available after installing clang: https://andrewkelley.me/post/zig-cc-powerful-drop-in-replacement-gcc-clang.html https://andrewkelley.me/post/zig-cc-powerful-drop-in-replace... Previous discussions: https://news.ycombinator.com/item?id=22679138 https://news.ycombinator.com/item?id=22679138 https://news.ycombinator.com/item?id=27872596 https://news.ycombinator.com/item?id=27872596
- 2fast4you 4y agoZig actually transpiles the C code into Zig code, then compiles that
- wyldfire 4y agoNo, that is not true. `zig cc` uses libclang to compile it in the same way that clang would.
- 2fast4you 4y agoSorry, I wasn’t specific. I was talking about how cImport works in Zig. I didn’t know `zig cc` worked differently. “ Zig’s @cImport builtin is unique in that it takes in an expression, which can only take in @cInclude, @cDefine, and @cUndef. This works similarly to translate-c, translating C code to Zig under the hood.” https://ziglearn.org/chapter-4/ https://ziglearn.org/chapter-4/