Issues compiling basic C++ project using build.zig

Having some issues with compiling a basic c++ program using zig (single main.cpp file that prints “hi” to std::cout).
I’m getting this error when I run zig build:

error: root source file struct 'std' has no member named 'Builder'
pub fn build(b: *std.Builder) !void {
                 ~~~^~~~~~~~
/usr/lib/zig/std/std.zig:1:1: note: struct declared here
pub const ArrayHashMap = array_hash_map.ArrayHashMap;
^~~

And my build.zig file looks like this:

const std = @import("std");

pub fn build(b: *std.Builder) !void {
    const mode = b.standardReleaseOptions();
    const exe = b.addExecutable("my_cpp_project", "main.cpp");
    exe.setBuildMode(mode);
    exe.linkLibC(); // Link against the C standard library
    exe.addCSourceFile("main.cpp", &[_][]const u8{});
    b.installArtifact(exe);
}

Have been searching for a similar problems and cannot find an in-date solution that works. Any ideas what I could be doing wrong here?

Please make sure you’re looking at documentation and examples that correspond to the version of Zig that you are using. It looks like you are using older documentation and newer Zig.

3 Likes

Aside:

    exe.linkLibC(); // Link against the C standard library

You seem to be compiling C++ code, so you probably want this to be exe.linkLibCpp() instead.