I’m trying to use the println
statement in C++23 by using the workaround as given here.
Am I missing some flags or function calls (like exe.linkLibCpp();
) to make this work?
Additional information:
build.zig
const std = @import("std");
const CPP_ROOT_DIR = "./src/";
const CPP_FLAGS = &.{
"-Wall",
"-Werror",
"-O2",
"-std=c++23",
};
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "temp",
.target = target,
.optimize = optimize,
});
exe.addCSourceFiles(.{
.root = b.path(CPP_ROOT_DIR),
.files = &.{
"main.cpp",
},
.flags = CPP_FLAGS,
});
exe.linkLibCpp();
b.installArtifact(exe);
}
src/main.cpp
#include "print_workaround.h"
int main() {
int answer { 42 };
std::println("The answer to life, the universe, and everything is {}.", answer);
return 0;
}
The workaround is src/print_workaround.h
.
Error
/path/to/cpp23zig/src/print_workaround.h:2:8: error: module 'std' not found
import std;
~~~~~~~^
/path/to/cpp23zig/src/main.cpp:1:10: note: in file included from /path/to/cpp23zig/src/main.cpp:1:
#include "print_workaround.h"
^
error: the following command failed with 1 compilation errors:
$HOME/.local/share/zig/zig build-exe -cflags -Wall -Werror -O2 -std=c++23 -- /path/to/cpp23zig/src/main.cpp -ODebug -Mroot -lc++ --cache-dir /path/to/cpp23zig/.zig-cache --global-cache-dir $HOME/.cache/zig --name temp --zig-lib-dir $HOME/.local/share/zig/lib/ --listen=-
Build Summary: 0/3 steps succeeded; 1 failed
install transitive failure
└─ install temp transitive failure
└─ zig build-exe temp Debug native 1 errors
error: the following build command failed with exit code 1:
/path/to/cpp23zig/.zig-cache/o/3cf36c1dace78d0f16a247269e0a8d09/build $HOME/.local/share/zig/zig $HOME/.local/share/zig/lib /path/to/cpp23zig /path/to/cpp23zig/.zig-cache $HOME/.cache/zig --seed 0x375f42b8 -Z1f745c60c89700d7