0Pricing
Zig Academy · Lección

Declarar ejecutables y bibliotecas

Añada artefactos al grafo de compilación.

Declarar ejecutables y bibliotecas es una lección gratuita de Zig Academy en CoddyKit. Esta es la lección 2 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Zig Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Zig Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en 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/app

Add 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. 🎯

Preguntas frecuentes

¿La lección «Declarar ejecutables y bibliotecas» es gratis?

Sí — el texto completo de «Declarar ejecutables y bibliotecas» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Zig Academy, actualiza a CoddyKit PRO. El curso de Zig Academy incluye 4 lecciones en total.

¿Qué aprenderé en «Declarar ejecutables y bibliotecas»?

Añada artefactos al grafo de compilación. Practicas Zig Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Zig Academy?

No se requiere experiencia previa. Zig Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 2 de 4.

¿Cuánto tiempo toma la lección «Declarar ejecutables y bibliotecas»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Zig Academy?

Sí. Cada lección de Zig Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Anatomía de un archivo build.zig
  2. Declarar ejecutables y bibliotecas
  3. Pasos de compilación y zig build run
  4. Opciones y flags de compilación
← Volver a Zig Academy