0Pricing
Zig Academy · Lektion

C-Funktionen aufrufen und verknüpfen

Binden Sie libc und externe Bibliotheken ein.

C-Funktionen aufrufen und verknüpfen ist eine kostenlose Zig Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Zig Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Zig Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

Importing Is Not Linking

@cImport gives you the declarations, but the actual machine code still lives in a C library you must link against.

Link Against libc

Most C calls need the C standard library. Tell the build to link libc so functions like printf resolve at link time.

exe.linkLibC();

Linking on the CLI

For a quick run, pass the flag yourself. -lc links libc when you compile a single file directly.

zig build-exe main.zig -lc

Call Like Native Code

Once linked, a C function is called just like a Zig one. You write c.printf(...) and Zig handles the calling convention.

c.printf("%d\n", 42);

Match the Signature

You must pass arguments that fit the C signature. Wrong types are a compile error, since Zig knows the translated prototype.

Link a Named Library

For other libraries, name them. linkSystemLibrary tells the linker to pull in something like the math library.

exe.linkSystemLibrary("m");

Return Values Come Back

A C function's return value arrives as a normal Zig value of the translated type, ready to store or test.

const n = c.abs(-5);

Errors Use C Conventions

C has no error unions, so failures show up as return codes or a set errno. You check them the C way, by hand.

No Hidden Marshalling

Zig does no silent conversion of your data. What you pass is what C receives, keeping the call cheap and predictable.

Build File Wiring

In build.zig you attach linking to the artifact. The same exe step that compiles your code also records its libraries.

exe.linkLibC();
exe.linkSystemLibrary("z");

Two Steps to Success

Calling C is always import plus link: bring in the declarations, then make sure the real symbols are available to the linker.

Quick Check

Think about what makes a C function actually resolve.

Recap

You learned that calling C needs both the import and linking, after which C functions are invoked just like native Zig ones. 🎯

Häufig gestellte Fragen

Ist die Lektion „C-Funktionen aufrufen und verknüpfen“ kostenlos?

Ja — der vollständige Text von „C-Funktionen aufrufen und verknüpfen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Zig Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Zig Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „C-Funktionen aufrufen und verknüpfen“?

Binden Sie libc und externe Bibliotheken ein. Du übst Zig Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Zig Academy zu starten?

Keine Vorkenntnisse erforderlich. Zig Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.

Wie lange dauert die Lektion „C-Funktionen aufrufen und verknüpfen“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Zig Academy-Lektion Code schreiben und ausführen?

Ja. Jede Zig Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. C-Header mit @cImport importieren
  2. C-Funktionen aufrufen und verknüpfen
  3. C-Zeiger, Strings und Typen
  4. zig translate-c zum Lesen von C als Zig
← Zurück zu Zig Academy