ef0daa4b5e46 — Nathan Michaels 4 years ago
initial commit
8 files changed, 97 insertions(+), 0 deletions(-)

A => Makefile
A => add.c
A => add.h
A => build.zig
A => main.c
A => mul.c
A => mul.h
A => src/main.zig
A => Makefile +25 -0
@@ 0,0 1,25 @@ 
+# Makefile
+CC=gcc
+CFLAGS=-Wall -Werror -Wextra -Os -fPIE
+LD=ld
+LDFLAGS=-melf_x86_64 -r --whole-archive
+LPATH=-L.
+ZIGFLAGS=-Drelease-fast
+
+.PHONY: clean libadd.a
+
+%.o:: %.c
+	$(CC) -o $@ -c $(CFLAGS) $^
+
+libadd.a:
+	zig build
+	touch libadd.a
+
+libi32math.a: libadd.a mul.o
+	$(LD) $(LDFLAGS) -o $@ $^
+
+main: main.o libi32math.a
+	$(CC) -o main $(CFLAGS) $(LPATH) $< -li32math
+
+clean:
+	rm -f *.o *.a main

          
A => add.c +6 -0
@@ 0,0 1,6 @@ 
+#include "add.h"
+
+int32_t add(int32_t a, int32_t b)
+{
+    return a + b;
+}

          
A => add.h +9 -0
@@ 0,0 1,9 @@ 
+#ifndef ADD_H
+#define ADD_H
+
+#include <stdint.h>
+
+// Add two numbers together and return the sum.
+int32_t add(int32_t a, int32_t b);
+
+#endif

          
A => build.zig +20 -0
@@ 0,0 1,20 @@ 
+const Builder = @import("std").build.Builder;
+
+pub fn build(b: *Builder) void {
+    const mode = b.standardReleaseOptions();
+    const lib = b.addStaticLibrary("add", "src/main.zig");
+    lib.setBuildMode(mode);
+    switch (mode) {
+        .Debug, .ReleaseSafe => lib.bundle_compiler_rt = true,
+        .ReleaseFast, .ReleaseSmall => lib.disable_stack_probing = true,
+    }
+    lib.force_pic = true;
+    lib.setOutputDir(".");
+    lib.install();
+
+    var main_tests = b.addTest("src/main.zig");
+    main_tests.setBuildMode(mode);
+
+    const test_step = b.step("test", "Run library tests");
+    test_step.dependOn(&main_tests.step);
+}

          
A => main.c +11 -0
@@ 0,0 1,11 @@ 
+#include <stdio.h>
+#include <inttypes.h>
+#include "add.h"
+#include "mul.h"
+
+int main(void)
+{
+    printf("7 + 3 = %"PRIi32"\n", add(7, 3));
+    printf("7 * 3 = %"PRIi32"\n", mul(7, 3));
+    return 0;
+}

          
A => mul.c +7 -0
@@ 0,0 1,7 @@ 
+// mul.c
+#include "mul.h"
+
+int32_t mul(int32_t a, int32_t b)
+{
+    return a * b;
+}

          
A => mul.h +9 -0
@@ 0,0 1,9 @@ 
+#ifndef MUL_H
+#define MUL_H
+
+#include <stdint.h>
+
+// Multiply two numbers together and return the product.
+int32_t mul(int32_t a, int32_t b);
+
+#endif

          
A => src/main.zig +10 -0
@@ 0,0 1,10 @@ 
+const std = @import("std");
+const testing = std.testing;
+
+export fn add(a: i32, b: i32) i32 {
+    return a + b;
+}
+
+test "basic add functionality" {
+    testing.expect(add(3, 7) == 10);
+}