Skip to content

Commit c84dbfc

Browse files
committed
Files all building properly. Need to update readme
1 parent 8af8cb5 commit c84dbfc

14 files changed

Lines changed: 134 additions & 77 deletions

README.md

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,36 @@ well-versed in C, can leverage the power of third-party C libraries.
1111

1212
## TOC
1313

14-
[The Journey](#the-journey)
15-
- [Create a Simple C application](#create-a-simple-c-application)
16-
- [Compiling the application using `zig cc`](#compiling-the-application-using-zig-cc)
17-
- [Leverage Zig's build system to build the C application](#leverage-zigs-build-system-to-build-the-c-application)
18-
- [Create a Zig application that links to the C library](#create-a-zig-application-that-links-to-the-c-library)
19-
- [Create a Zig wrapper around a C Function](#create-a-zig-wrapper-around-a-c-function)
20-
- [Using Zig to build a C Library](#using-zig-to-build-a-c-library)
21-
- [Linking a Zig application to a Pre-built C Library](#linking-a-zig-application-to-a-pre-built-c-library)
22-
23-
[Side Quests](#side-quests)
24-
- [Testing C code in Zig](#testing-c-code-in-zig)
25-
14+
- [Utilizing This Project](#utilizing-this-project)
15+
- [Zig Build Commands](#zig-build-commands)
16+
- [The Journey](#the-journey)
17+
- [Create a Simple C application](#create-a-simple-c-application)
18+
- [Compiling the application using `zig cc`](#compiling-the-application-using-zig-cc)
19+
- [Leverage Zig's build system to build the C application](#leverage-zigs-build-system-to-build-the-c-application)
20+
- [Create a Zig application that links to the C library](#create-a-zig-application-that-links-to-the-c-library)
21+
- [Create a Zig wrapper around a C Function](#create-a-zig-wrapper-around-a-c-function)
22+
- [Using Zig to build a C Library](#using-zig-to-build-a-c-library)
23+
- [Linking a Zig application to a Pre-built C Library](#linking-a-zig-application-to-a-pre-built-c-library)
24+
- [Side Quests](#side-quests)
25+
- [Testing C code in Zig](#testing-c-code-in-zig)
26+
- [Resources](#resources)
27+
28+
## Utilizing this Project
29+
30+
### Zig Build Commands
31+
```sh
32+
zig build [steps] [options]
33+
34+
Steps:
35+
install (default) Copy build artifacts to prefix path
36+
uninstall Remove build artifacts from prefix path
37+
c_app Run a C application build with Zig's build system.
38+
zig_app Run a Zig application linked to C source code.
39+
lib_static Create a static library from C source code.
40+
lib_shared Create a shared library from C source code.
41+
zig_app_shared Run a Zig application that is linked to a shared library.
42+
zig_app_static Run a Zig application that is linked to a static library.
43+
```
2644
2745
## The Journey
2846
### Create a Simple C Application
@@ -288,6 +306,11 @@ our `zmath` C library.
288306
289307
### Using Zig to build a C Library
290308
309+
When I say a C library, I mean a static or shared library. A static library contains
310+
a single file, for me thats a `.lib` file. Shared libraries, on the other hand,
311+
contain `.lib` files and `.dll` files. Basically more stuff to link to. If you
312+
want to dig deeper on static and shared libraries, check the [Resources](#resources) section
313+
291314
We'll use our `zmath` library we wrote in C and build it into a shared libary so
292315
that we can dynamically link to rather than needing to include the source code
293316
during build time. Basically, this is the situation you'll be in when using
@@ -346,4 +369,12 @@ code.
346369
347370
Some extra thoughts I have about integrating Zig and C together.
348371
349-
### Testing C code in Zig
372+
### Testing C code in Zig
373+
374+
## Resources
375+
- https://mtlynch.io/notes/zig-call-c-simple/
376+
What initially made me want to tackle this subject, this article is a great
377+
starting point for understanding C and Zig.
378+
379+
- [Wikipedia article for "Shared Library"](https://en.wikipedia.org/wiki/Shared_library)
380+
- [Wikipedia article for "Static Library"](https://en.wikipedia.org/wiki/Static_library)

build.zig

Lines changed: 44 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,70 @@
11
const std = @import("std");
22

3-
const module_ex1 = @import("build_c_application_with_zig_build.zig");
4-
const module_ex2 = @import("build_zig_linked_to_c.zig");
3+
const module_c_app = @import("build_c_app.zig");
4+
const module_zig_app = @import("build_zig_app.zig");
55
const module_ex3 = @import("build_zig_c_wrapper.zig");
6-
const module_ex4 = @import("build_c_static_library_with_zig_build.zig");
7-
const module_ex5 = @import("build_c_shared_library_with_zig_build.zig");
8-
// const module_ex6 = @import("build_zig_linked_to_c_static_lib.zig");
9-
// const module_ex7 = @import("build_zig_linked_to_c_shared_lib.zig");
6+
const module_lib_static = @import("build_c_static_lib.zig");
7+
const module_lib_shared = @import("build_c_shared_lib.zig");
8+
const module_zig_app_static = @import("build_zig_app_static.zig");
9+
const module_zig_app_shared = @import("build_zig_app_shared.zig");
1010

1111
pub fn build(b: *std.Build) void {
1212
const target = b.standardTargetOptions(.{});
1313
const optimize = b.standardOptimizeOption(.{});
1414

15-
// ex1
16-
const ex1 = module_ex1.build(b, target, optimize);
17-
const run_ex1 = b.addRunArtifact(ex1);
15+
// c_app
16+
const c_app = module_c_app.build(b, target, optimize);
17+
const run_c_app = b.addRunArtifact(c_app);
1818

19-
b.installArtifact(ex1);
19+
b.installArtifact(c_app);
2020

21-
const run_ex1_step = b.step("ex1", "Run a C application build with Zig's build system.");
22-
run_ex1_step.dependOn(&run_ex1.step);
21+
const run_c_app_step = b.step("c_app", "Run a C application build with Zig's build system.");
22+
run_c_app_step.dependOn(&run_c_app.step);
2323

24-
// ex2
25-
const ex2 = module_ex2.build(b, target, optimize);
26-
const run_ex2 = b.addRunArtifact(ex2);
24+
// zig_app
25+
const zig_app = module_zig_app.build(b, target, optimize);
26+
const run_zig_app = b.addRunArtifact(zig_app);
2727

28-
b.installArtifact(ex2);
28+
b.installArtifact(zig_app);
2929

30-
const run_ex2_step = b.step("ex2", "Run a Zig application linked to C source code.");
31-
run_ex2_step.dependOn(&run_ex2.step);
30+
const run_zig_app_step = b.step("zig_app", "Run a Zig application linked to C source code.");
31+
run_zig_app_step.dependOn(&run_zig_app.step);
3232

33-
// ex3
34-
const ex3 = module_ex3.build(b, target, optimize);
35-
const run_ex3 = b.addRunArtifact(ex3);
33+
// lib_static
34+
const lib_static = module_lib_static.build(b, target, optimize);
35+
const install_lib_static = b.addInstallArtifact(lib_static, .{});
3636

37-
b.installArtifact(ex3);
37+
b.installArtifact(lib_static);
3838

39-
const run_ex3_step = b.step("ex3", "Run a Zig application with an abstraction layer between the C source code.");
40-
run_ex3_step.dependOn(&run_ex3.step);
39+
const install_lib_static_step = b.step("lib_static", "Create a static library from C source code.");
40+
install_lib_static_step.dependOn(&install_lib_static.step);
4141

42-
// ex4
43-
const ex4 = module_ex4.build(b, target, optimize);
44-
const install_ex4 = b.addInstallArtifact(ex4, .{});
42+
// lib_shared
43+
const lib_shared = module_lib_shared.build(b, target, optimize);
44+
const install_lib_shared = b.addInstallArtifact(lib_shared, .{});
4545

46-
b.installArtifact(ex4);
46+
b.installArtifact(lib_shared);
4747

48-
const install_ex4_step = b.step("ex4", "Create a shared library file from C source code.");
49-
install_ex4_step.dependOn(&install_ex4.step);
48+
const install_lib_shared_step = b.step("lib_shared", "Create a shared library from C source code.");
49+
install_lib_shared_step.dependOn(&install_lib_shared.step);
5050

51-
// ex5
52-
const ex5 = module_ex5.build(b, target, optimize);
53-
const install_ex5 = b.addInstallArtifact(ex5, .{});
51+
// zig_app_shared
52+
const zig_app_shared = module_zig_app_shared.build(b, target, optimize);
53+
const run_zig_app_shared = b.addInstallArtifact(zig_app_shared, .{});
5454

55-
b.installArtifact(ex5);
55+
b.installArtifact(zig_app_shared);
5656

57-
const install_ex5_step = b.step("ex5", "Create a static library file from C source code.");
58-
install_ex5_step.dependOn(&install_ex5.step);
57+
const run_zig_app_shared_step = b.step("zig_app_shared", "Run a Zig application that is linked to a shared library.");
58+
run_zig_app_shared_step.dependOn(&install_lib_shared.step); // create and install shared library
59+
run_zig_app_shared_step.dependOn(&run_zig_app_shared.step);
5960

60-
// ex6
61-
// const ex6 = module_ex6.build(b, target, optimize);
62-
// const run_ex6 = b.addInstallArtifact(ex6, .{});
61+
// zig_app_static
62+
const zig_app_static = module_zig_app_static.build(b, target, optimize);
63+
const run_zig_app_static = b.addInstallArtifact(zig_app_static, .{});
6364

64-
// b.installArtifact(ex6);
65+
b.installArtifact(zig_app_static);
6566

66-
// const run_ex6_step = b.step("ex6", "Run a Zig application that is statically linked to a C library.");
67-
// run_ex6_step.dependOn(&run_ex6.step);
68-
69-
// ex7
70-
// const ex7 = module_ex7.build(b, target, optimize);
71-
// const run_ex7 = b.addInstallArtifact(ex7, .{});
72-
73-
// b.installArtifact(ex7);
74-
75-
// const run_ex7_step = b.step("ex7", "Run a Zig application that is dynamically linked to a C library.");
76-
// run_ex7_step.dependOn(&run_ex7.step);
67+
const run_zig_app_static_step = b.step("zig_app_static", "Run a Zig application that is linked to a static library.");
68+
run_zig_app_static_step.dependOn(&install_lib_static.step); // create and install static library
69+
run_zig_app_static_step.dependOn(&run_zig_app_static.step);
7770
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ const std = @import("std");
22

33
pub fn build(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step.Compile {
44
const exe = b.addExecutable(.{
5-
.name = "c_application_with_zig_build",
5+
.name = "c_app",
66
.target = target,
77
.optimize = optimize,
88
});
99

1010
exe.addIncludePath(b.path("include"));
11-
exe.addCSourceFiles(.{ .files = &[_][]const u8{ "src/main.c", "src/zmath.c" } });
11+
exe.addCSourceFiles(.{ .files = &[_][]const u8{ "src/c_app.c", "src/zmath.c" } });
1212

1313
exe.linkLibC();
1414

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const std = @import("std");
22

33
pub fn build(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step.Compile {
44
const lib = b.addSharedLibrary(.{
5-
.name = "c_shared_library_with_zig_build",
5+
.name = "zmath-shared",
66
.target = target,
77
.optimize = optimize,
88
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const std = @import("std");
22

33
pub fn build(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step.Compile {
44
const lib = b.addStaticLibrary(.{
5-
.name = "c_static_library_with_zig_build",
5+
.name = "zmath-static",
66
.target = target,
77
.optimize = optimize,
88
});
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ const std = @import("std");
22

33
pub fn build(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step.Compile {
44
const exe = b.addExecutable(.{
5-
.name = "zig_linked_to_c",
6-
.root_source_file = b.path("src/zig_linked_to_c.zig"),
5+
.name = "zig_app",
6+
.root_source_file = b.path("src/zig_app.zig"),
77
.target = target,
88
.optimize = optimize,
99
});

build_zig_app_shared.zig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step.Compile {
4+
const exe = b.addExecutable(.{
5+
.name = "zig_app_shared",
6+
.root_source_file = b.path("src/zig_c_wrapper.zig"),
7+
.target = target,
8+
.optimize = optimize,
9+
});
10+
11+
// exe.addObjectFile(b.path("zig-out/bin/zmath-shared.dll"));
12+
exe.addObjectFile(b.path("zig-out/lib/zmath-shared.lib"));
13+
14+
return exe;
15+
}

build_zig_app_static.zig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step.Compile {
4+
const exe = b.addExecutable(.{
5+
.name = "zig_app_static",
6+
.root_source_file = b.path("src/zig_c_wrapper.zig"),
7+
.target = target,
8+
.optimize = optimize,
9+
});
10+
11+
exe.addObjectFile(b.path("zig-out/lib/zmath-static.lib"));
12+
13+
return exe;
14+
}

include/zmath.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
int add(int a, int b);
2-
int sub(int a, int b);
1+
extern int add(int a, int b);
2+
extern int sub(int a, int b);
File renamed without changes.

0 commit comments

Comments
 (0)