Build mix cpp and zig code

Hi,

I am new to zig, I was playing with zig build system to get a better understanding of how it works. I have single header CPP which gets compiled easily with “zig c++ test.cpp simdjson.cpp -o test”. now I changed the test.cpp to test.zig


const std = @import("std");

const json = @cImport({
      @cInclude("simdjson.h");
      });



pub fn main() !void{

const parser = json.ondemand.parser;
const file = json.padded_string.load("twitter.json");
const tw =  parser.iterate(file);
const stdout = std.io.getStdOut().writer();
try stdout.print("Hello, {s}!\n", .{tw});

}

and this is my build.zig

const std = @import("std");

pub fn build(b: *std.build.Builder) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const cflags = [_][]const u8{"-std=c++17", "-fno-sanitize=undefined"};

    const lib = b.addStaticLibrary(.{
        .name = "simdjson",
        .target = target,
        .optimize = optimize,
        .link_libc = true,
    });

    lib.linkLibCpp();
    lib.addCSourceFile(.{
    .file = std.Build.FileSource.relative("simdjson.cpp"),
    .flags = &cflags
    });
    lib.addIncludePath(.{ .path = "./header" });
    lib.installHeader("./header/simdjson.h", "simdjson.h");
    //b.installArtifact(lib);

    const exe = b.addExecutable(.{
    	.name = "exe",
            .root_source_file = .{ .path = "test.zig" },
            .target = target,
            .optimize = optimize,
        });

    exe.linkLibrary(lib);
    exe.linkLibCpp();
    exe.addCSourceFile(.{
            .file = std.Build.FileSource.relative("simdjson.cpp"),
            .flags = &cflags
            });
            //exe.addIncludePath(.{ .path = "./header" });
    b.installArtifact(exe);
}

this gives me " error: C import failed". I would be appreciative if anyone can help me with this.

Zig does not support importing C++ header files. The reason is that it’s just too complicated.
Currently @cImport translates the header-file into Zig. This is easy for C headers, since C is mostly a simple language. But C++ has lots of complicated features, like classes with inheritance or lambdas, which have no counterpart in zig.

Now what you can do is:

Yeah, I was a bit turned around on this myself - C and C++ have very different import/linking implications for Zig: Header H or "Zig" entry point?