Calling C Functions and Linking
Bind to libc and external libraries.
Calling C Functions and Linking is a free Zig Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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 -lcCall 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. 🎯
Frequently asked questions
Is the “Calling C Functions and Linking” lesson free?
Yes — the full text of “Calling C Functions and Linking” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.
What will I learn in “Calling C Functions and Linking”?
Bind to libc and external libraries. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Zig Academy?
No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Calling C Functions and Linking” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Zig Academy lesson?
Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Import C Headers with @cImport
- Calling C Functions and Linking
- C Pointers, Strings, and Types
- zig translate-c to Read C as Zig