Declarando executáveis e bibliotecas
Adicione artefatos ao grafo de compilação.
Declarando executáveis e bibliotecas é uma aula grátis de Zig Academy no CoddyKit. Esta é a aula 2 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Zig Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Zig Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
Artifacts to Build
The things you compile are called artifacts: an executable you run, or a library other code links against. You declare them in build.zig. 📦
Add an Executable
Create a runnable program with addExecutable. You give it a name and the root source file that holds main.
const exe = b.addExecutable(.{
.name = "app",
});Point to the Root Module
The artifact needs a root_module built from your main file, plus the target and optimize values you chose earlier.
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
}),Carry Target and Optimize
That module also takes the target and optimize fields, so the executable honors whatever the command line requested.
.target = target,
.optimize = optimize,Install the Output
Creating an exe is not enough; tell Zig to place it in zig-out with installArtifact so the default build emits it.
b.installArtifact(exe);Where Outputs Land
Installed binaries appear under zig-out/bin by convention. Run zig build and look there for your freshly compiled program.
zig-out/bin/appAdd a Library
For reusable code, use addLibrary instead. It produces a library artifact others can import and link against.
const lib = b.addLibrary(.{
.name = "mathz",
});Static or Dynamic
A library can be static (baked into the caller) or dynamic (a shared file). You set linkage when you declare the library.
.linkage = .static,Link Library into Exe
Connect them so the exe can call the library's code: use linkLibrary to add the dependency edge.
exe.linkLibrary(lib);Need libc?
If your code calls C standard functions, request libc on the artifact with linkLibC so the right system library is linked.
exe.linkLibC();A Graph of Artifacts
Each artifact is a node; linking adds edges. Together they form the build graph Zig walks when you run the build. ✅
Quick Check
Recall which call actually emits a built binary into zig-out.
Recap
You declared artifacts with addExecutable and addLibrary, set their root module and target, then used installArtifact and linkLibrary to wire them. 🎯
Perguntas Frequentes
A aula “Declarando executáveis e bibliotecas” é grátis?
Sim — o texto completo de “Declarando executáveis e bibliotecas” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Zig Academy, atualize para CoddyKit PRO. O curso de Zig Academy inclui 4 aulas no total.
O que vou aprender em “Declarando executáveis e bibliotecas”?
Adicione artefatos ao grafo de compilação. Você pratica Zig Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Zig Academy?
Nenhuma experiência prévia é necessária. Zig Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 2 de 4.
Quanto tempo leva a aula “Declarando executáveis e bibliotecas”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Zig Academy?
Sim. Cada aula de Zig Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Anatomia de um arquivo build.zig
- Declarando executáveis e bibliotecas
- Etapas de compilação e zig build run
- Opções e sinalizadores de compilação