Skip to content

Commit

Permalink
example4 (mix C++/D) added
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Apr 18, 2024
1 parent 9944b65 commit 5bed7c8
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 453 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ jobs:
zig build run-example1
zig build run-example2
zig build run-example3
zig build run-cpp
zig build run-example4
40 changes: 23 additions & 17 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,14 @@ pub fn build(b: *std.Build) !void {
},
});

// C++ build test

const exe = b.addExecutable(.{
.name = "cpp_tests",
const lib = b.addStaticLibrary(.{
.name = "example4",
.target = target,
.optimize = optimize,
});
exe.addCSourceFiles(.{
lib.addCSourceFiles(.{
.root = bdwgc_path,
.files = if (exe.rootModuleTarget().abi != .msvc)
.files = if (lib.rootModuleTarget().abi != .msvc)
&.{
"gc_cpp.cc",
"gc_badalc.cc",
Expand All @@ -98,27 +96,35 @@ pub fn build(b: *std.Build) !void {
"-Wextra",
},
});
exe.addCSourceFile(.{
.file = b.path("tests/cpp.cc"),
lib.addCSourceFile(.{
.file = b.path("examples/example4.cc"),
.flags = &.{
"-Wall",
"-Wpedantic",
"-Wextra",
},
});
for (bdwgc_artifact.root_module.include_dirs.items) |dir| {
exe.addIncludePath(dir.path);
lib.addIncludePath(dir.path);
}
exe.linkLibrary(bdwgc_artifact);
if (exe.rootModuleTarget().abi != .msvc) {
exe.linkLibCpp();
lib.linkLibrary(bdwgc_artifact);
if (lib.rootModuleTarget().abi != .msvc) {
lib.linkLibCpp();
} else {
exe.linkLibC();
lib.linkLibC();
}
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
const run_step = b.step("run-cpp", "Run the C++ app");
run_step.dependOn(&run_cmd.step);
try buildExe(b, .{
.name = "example4",
.target = target,
.optimize = optimize,
.betterC = true, // need D runtimeGC
.artifact = lib,
.sources = &.{"examples/example4.d"},
.dflags = &.{
"-w",
"-Isrc",
},
});
}
fn buildExe(b: *std.Build, options: abs.DCompileStep) !void {
const exe = try abs.ldcBuildStep(b, options);
Expand Down
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
.dependencies = .{
.abs = .{
.url = "git+https://github.com/kassane/anotherBuildStep#main",
.hash = "12201a8ca860ea3f27538562f1862891242699679edde2a6159b73177b3c495ff6de",
.hash = "12200d68b7fe7b0c3b63c703ee20eb919fe15760b62e794946e99fd11c4f2e738378",
},
.bdwgc = .{
.url = "git+https://github.com/ivmai/bdwgc#df1a787929aa550ec5343442fd2212a2717ac0cd",
Expand Down
17 changes: 17 additions & 0 deletions examples/example4.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <gc_cpp.h>

struct Person {
char* name;
int age;
Person* people;
};

Person createPerson(const char* name, int age) {
Person p;
p.name = GC_strdup(name);
p.age = age;
p.people = static_cast<Person*>(GC_malloc(sizeof(Person)));
// FIXME: undefined reference to `__gxx_personality_v0'
// p.people = gc_allocator<Person>().allocate(1);
return p;
}
27 changes: 27 additions & 0 deletions examples/example4.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import bdwgc;

extern (C++)
{
struct Person
{
char* name;
int age;
Person* people;
}

Person createPerson(const(char)* name, int age);
}
extern (C):
void main()
{
GC_init();
GC_set_all_interior_pointers(1);

Person p1 = createPerson("John", 42);
Person p2 = createPerson("Sarah", 35);
GC_gcollect();
GC_printf("%s, have %d years old.\n", p1.name, p1.age);
GC_printf("%s, have %d years old.\n", p2.name, p2.age);
}
pragma(printf)
void GC_printf(const(char)* format, ...);
Loading

0 comments on commit 5bed7c8

Please sign in to comment.