Сборка для Linux, macOS и Windows
Один набор инструментов для множества платформ.
«Сборка для Linux, macOS и Windows» — бесплатный урок Zig Academy на CoddyKit. Это урок 2 из 4. Ты можешь прочитать весь урок бесплатно ниже — а потом практиковать его прямо в браузере с встроенным редактором кода и ИИ-репетитором 24/7. Это часть пути обучения Zig Academy, и твой прогресс синхронизируется между веб-версией и приложением CoddyKit. Курс Zig Academy содержит 4 уроков всего.
Части этого урока еще не переведены и отображаются на английском.
One Toolchain, Many Platforms
From a single machine you can produce binaries for every major OS. Zig treats cross-builds as first-class, not an afterthought.
Build for Linux
Target Linux by naming its triple. The musl ABI gives you a clean, portable Linux executable from any host.
zig build-exe main.zig -target x86_64-linux-muslBuild for macOS
Target Apple Silicon with the macos OS field. The output is a real Mach-O binary that runs on arm64 Macs.
zig build-exe main.zig -target aarch64-macosBuild for Windows
Target Windows and Zig emits a .exe automatically. The windows OS field switches on the right object format for you.
zig build-exe main.zig -target x86_64-windowsSame Source, Different Output
Your Zig code does not change between targets. Only the -target flag changes, and the compiler does the rest of the work.
Set the Target in build.zig
In a project, the build script reads a target instead. standardTargetOptions exposes -target through zig build.
const target = b.standardTargetOptions(.{});Attach the Target to the Exe
Pass that resolved target into your executable so the whole build graph knows which platform it is producing.
const exe = b.addExecutable(.{ .name = "app", .target = target });Override on the Command Line
With standardTargetOptions in place, users pass -Dtarget to zig build and choose any platform without editing code.
zig build -Dtarget=x86_64-windowsNo External SDKs Needed
Zig bundles the headers and start files for each OS, so you skip installing a Windows SDK or macOS toolchain separately.
Test Each Build
You can compile every target on one host, then copy each binary to the matching machine to verify it runs as expected.
Build All Three at Once
Cross-builds make release scripts simple. A short loop over three triples produces Linux, macOS, and Windows binaries in one pass.
Quick Check
Think about how you pick a platform inside a Zig project build.
Recap
You learned to build Linux, macOS, and Windows binaries from one host using -target or -Dtarget, with no extra SDKs. 🛠️
Часто задаваемые вопросы
Урок «Сборка для Linux, macOS и Windows» бесплатный?
Да — полный текст урока «Сборка для Linux, macOS и Windows» бесплатно доступен здесь в веб-версии. Чтобы практиковать его интерактивно (встроенный редактор кода и ИИ-репетитор 24/7) и разблокировать остальной курс Zig Academy, подпишись на CoddyKit PRO. Курс Zig Academy содержит 4 уроков всего.
Чему я научусь в уроке «Сборка для Linux, macOS и Windows»?
Один набор инструментов для множества платформ. Ты практикуешь Zig Academy с помощью реального кода, который запускаешь прямо в браузере, и ИИ-репетитор 24/7 отвечает на твои вопросы во время урока.
Нужен ли мне опыт, чтобы начать Zig Academy?
Предыдущий опыт не требуется. Zig Academy на CoddyKit структурирован для всех уровней — от новичков до продвинутых, поэтому ты можешь начать отсюда или с самого начала и учиться в своем темпе. Это урок 2 из 4.
Сколько времени занимает урок «Сборка для Linux, macOS и Windows»?
Большинство уроков CoddyKit занимают около 5–10 минут. Каждый из них компактный и интерактивный, поэтому ты постоянно делаешь прогресс и продолжаешь с того же места в веб-версии и приложении.
Можно ли писать и запускать код в этом уроке Zig Academy?
Да. Каждый урок Zig Academy включает встроенный редактор кода, поэтому ты пишешь и запускаешь реальный код прямо в браузере и получаешь моментальную обратную связь от AI — локальная установка не требуется.
Все уроки этого курса
- Целевые платформы и тройка -target
- Сборка для Linux, macOS и Windows
- Кросс-компиляция C с помощью zig cc
- Статические бинарные файлы и musl