Can zig c++ compile C++23 import std hello world yet?


Hi Zig community,

I know this is a Zig forum, but I use zig c++ as my C++ compiler because it’s the easiest cross-compiler to install.

I’m trying to compile this simple C++23 program:

import std;

int main() {
    std::println("Hello, world!");
}

Using this command:

zig c++ -std=c++23 -stdlib=libc++ hello.cpp -o hello

but I get this warning:

zig: warning: argument unused during compilation: '-stdlib=libc++' [-Wunused-command-line-argument]

It seems zig c++ ignores -stdlib=libc++ and might not yet support the modular standard library.

Is it currently possible to compile this with zig c++? If yes, how? If not, any ETA?

Thanks!

— Tom


FWIW, that doesn’t even work in Clang (on macOS):

scratch ➤ clang++ -std=c++23 -stdlib=libc++ hello.cpp -o hello
hello.cpp:1:1: error: unknown type name 'import'
    1 | import std;
      | ^
hello.cpp:4:5: error: 'std' is not a class, namespace, or enumeration
    4 |     std::println("Hello, world!");
      |     ^
hello.cpp:1:8: note: 'std' declared here
    1 | import std;
      |        ^
2 errors generated.

…and when trying a ‘proper’ (e.g. not Apple’s) Clang via homebrew:

/opt/homebrew/Cellar/llvm/20.1.6/bin/clang++ -std=c++23 -stdlib=libc++ hello.cpp -o hello
hello.cpp:1:8: fatal error: module 'std' not found
    1 | import std;
      | ~~~~~~~^~~
1 error generated.

PS: looking around, it looks like a std.pcm file needs to be built first:

…maybe the same works for Zig? (since the C and C++ frontends are Clang).

…but sheesh, modern C++ is such a broken mess…

1 Like

Check


Edit: Welcome to ziggit.

2 Likes

Oh ok, good to know that this is not just zig clang. Jesus we have 2025 and C++23 is still not completely implemented.

1 Like

Thank you very much, did not find this in Github in my first search. And thought this might not be realy a zig issue, so I wanted to ask first in the community.

Currently if you want to use C++ modules, you pretty much have to use CMake with a subset of buildsystems (e.g. it will never work with Make). Currently only ninja (single and multi-config, version 1.11+) and VS Studio 17 2022 (and up) are supported. And even then Header units aren’t supported (e.g. import "myheader.h";). And all of that while keeping in mind that CMake is pretty much spearheading the implementation together with the compiler implementors.

It will take a LONG time until C++ modules are properly supported everywhere (if at all) since it requires the buildsystem and compiler to work in tandem.

I have several small C++ projects that now use C++23 with modules, using clang++ 19.1.7 and cmake 3.31.6. Since this discussion is six months old and the GH issues (#15496 linked above and the newer #25110) end about three months ago, I’m quite interested in finding out the current thinking on enabling support for C++ modules. I have zero knowledge of the Zig C++ compiler implementation, but it seems to me that in order to support C++ modules, Zig would have to do something akin to what CMake implemented, i.e., scan the source files to determine inter-module dependencies (which are saved to “.ddi” files) so that it can figure out the correct compilation order.