build zig programe that import single header file cpp

267 Views Asked by At

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

test.cpp


#include <iostream>
#include "simdjson.h"
using namespace simdjson;
int main(void) {
    ondemand::parser parser;
    padded_string json = padded_string::load("twitter.json");
    ondemand::document tweets = parser.iterate(json);
    std::cout << uint64_t(tweets["search_metadata"]["count"]) << " results." << std::endl;
}

my 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});

}

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);
}

error log

test.zig:3:14: error: C import failed
const json = @cImport({
             ^~~~~~~~
referenced by:
    main: test.zig:11:16
    callMain: /home/mhn/zig/lib/std/start.zig:583:32
    remaining reference traces hidden; use '-freference-trace' to see all reference traces
/home/mhn/zig/lib/libcxx/include/__verbose_abort:20:1: error: unknown type name '_LIBCPP_BEGIN_NAMESPACE_STD'
_LIBCPP_BEGIN_NAMESPACE_STD
^
/home/mhn/zig/lib/libcxx/include/__verbose_abort:24:17: error: expected ';' after top level declarator
_LIBCPP_NORETURN _LIBCPP_AVAILABILITY_VERBOSE_ABORT _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 1, 2)
                ^
/home/mhn/zig/lib/libcxx/include/__verbose_abort:58:1: error: unknown type name '_LIBCPP_END_NAMESPACE_STD'
_LIBCPP_END_NAMESPACE_STD
^
/home/mhn/Projects/zig-build-test/zig-out/include/simdjson.h:52:2: error: simdjson requires a C++ compiler
#error simdjson requires a C++ compiler
 ^
/home/mhn/Projects/zig-build-test/zig-out/include/simdjson.h:79:2: error: simdjson requires a compiler compliant with the C++11 standard
#error simdjson requires a compiler compliant with the C++11 standard
 ^
/home/mhn/zig/lib/libcxx/include/__type_traits/enable_if.h:18:1: error: unknown type name '_LIBCPP_BEGIN_NAMESPACE_STD'
_LIBCPP_BEGIN_NAMESPACE_STD
^
/home/mhn/zig/lib/libcxx/include/__type_traits/enable_if.h:20:9: error: expected ';' after top level declarator
template <bool, class _Tp = void>
        ^
/home/mhn/zig/lib/libcxx/include/__type_traits/enable_if.h:22:1: error: unknown type name 'template'
template <class _Tp>
^
/home/mhn/zig/lib/libcxx/include/__type_traits/enable_if.h:22:10: error: expected identifier or '('
template <class _Tp>
         ^
/home/mhn/zig/lib/libcxx/include/__type_traits/enable_if.h:27:1: error: unknown type name 'template'
template <bool _Bp, class _Tp = void>
^
/home/mhn/zig/lib/libcxx/include/__type_traits/enable_if.h:27:10: error: expected identifier or '('
template <bool _Bp, class _Tp = void>
         ^
/home/mhn/zig/lib/libcxx/include/__type_traits/enable_if.h:35:1: error: unknown type name '_LIBCPP_END_NAMESPACE_STD'
_LIBCPP_END_NAMESPACE_STD
^
/home/mhn/zig/lib/libcxx/include/__type_traits/integral_constant.h:18:28: error: expected ';' after top level declarator
_LIBCPP_BEGIN_NAMESPACE_STD
                           ^
/home/mhn/zig/lib/libcxx/include/__type_traits/integral_constant.h:31:1: error: unknown type name 'template'
template <class _Tp, _Tp __v>
^
/home/mhn/zig/lib/libcxx/include/__type_traits/integral_constant.h:31:10: error: expected identifier or '('
template <class _Tp, _Tp __v>
         ^
/home/mhn/zig/lib/libcxx/include/__type_traits/integral_constant.h:34:9: error: unknown type name 'integral_constant'
typedef integral_constant<bool, true> true_type;
        ^
/home/mhn/zig/lib/libcxx/include/__type_traits/integral_constant.h:34:26: error: expected identifier or '('
typedef integral_constant<bool, true> true_type;
                         ^
/home/mhn/zig/lib/libcxx/include/__type_traits/integral_constant.h:35:9: error: unknown type name 'integral_constant'
typedef integral_constant<bool, false> false_type;
        ^
/home/mhn/zig/lib/libcxx/include/__type_traits/integral_constant.h:35:26: error: expected identifier or '('
typedef integral_constant<bool, false> false_type;
                         ^
error: too many errors emitted, stopping now

I would be appreciative if anyone can help me with this.

this build.zig file work fine if I just make lib file but when I add const exe = b.addExecutable this issue happens.

0

There are 0 best solutions below