ziglang/zig
zig cc and the C/C++ toolchain
Purpose
zig is a drop-in C/C++ toolchain. The relevant sub-commands all come from src/main.zig:
ar Use Zig as a drop-in archiver
cc Use Zig as a drop-in C compiler
c++ Use Zig as a drop-in C++ compiler
dlltool Use Zig as a drop-in dlltool.exe
lib Use Zig as a drop-in lib.exe
ranlib Use Zig as a drop-in ranlib
objcopy Use Zig as a drop-in objcopy
rc Use Zig as a drop-in rc.exeThis page is about how zig cc, zig c++, zig ar/lib/ranlib, zig dlltool, and zig rc are wired together.
How zig cc works
zig cc accepts the clang command line. The flow is:
src/main.zigrecognizes the sub-command and invokes the bundled clang driver viasrc/zig_clang_driver.cpp.- The driver translates the clang invocation into a
Compilation.Config(e.g.-c foo.cbecomes a single-file compile to object). Compilation.createruns the compile through clang's frontend (src/zig_clang_cc1_main.cpporsrc/zig_clang_cc1as_main.cppfor assembly), producing an object via LLVM.- Linking goes through
src/link/Lld.zig(LLD) or, in the future, the in-tree linker for the target format. - Include paths are resolved from
lib/include/(clang resource),lib/libc/include/, and the libc flavor matching the target.
zig c++ is the same path with C++ on; lib/libcxx/, lib/libcxxabi/, and lib/libunwind/ get linked when needed.
Archiver and shim sub-commands
| Sub-command | Backed by |
|---|---|
zig ar |
src/zig_llvm-ar.cpp (LLVM's llvm-ar repurposed). |
zig ranlib |
Same llvm-ar codepath. |
zig lib |
Same llvm-ar codepath in lib-mode. |
zig dlltool |
Bundled LLVM dlltool glue. |
zig objcopy |
lib/compiler/objcopy.zig (Zig native). See objcopy. |
zig rc |
lib/compiler/resinator/. See resinator. |
Cross-compilation
The same machinery that powers Zig cross-compilation (see Cross compilation) backs zig cc -target ...:
- Bundled headers under
lib/libc/<flavor>/andlib/libc/include/provide the system headers. lib/compiler_rt/provides libcalls (replacinglibgcc.a).lib/libcxx/etc. provide the C++ runtime when needed.src/link/Lld.ziginvokes LLD with the right per-target flags.
Caching
zig cc caches per translation unit through std.Build.Cache. A subsequent compile with the same inputs is a cache hit, returning the existing object.
Tests
test/c_abi/— C ABI conformance tests across architectures.test/cases.zig— translate-c tests exercise C parsing/preprocessing.test/standalone/— projects that consumezig ccend-to-end.
Key source files
| File | Purpose |
|---|---|
src/main.zig |
Sub-command dispatch. |
src/zig_clang_driver.cpp |
Clang driver glue. |
src/zig_clang_cc1_main.cpp |
Clang cc1 (compile) main. |
src/zig_clang_cc1as_main.cpp |
Clang cc1as (assemble) main. |
src/zig_llvm.cpp, src/zig_llvm.h |
LLVM glue (codegen, opt). |
src/zig_llvm-ar.cpp |
llvm-ar for zig ar/ranlib/lib. |
src/link/Lld.zig |
LLD invocation. |
lib/compiler/objcopy.zig, lib/compiler/resinator/ |
Native Zig toolchain components. |
lib/libc/, lib/libcxx/, lib/libcxxabi/, lib/libunwind/, lib/include/, lib/compiler_rt/ |
Bundled runtime trees. |
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.