Skip to content

zig c++: Doesn't support c++ modules #15496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
kassane opened this issue Apr 28, 2023 · 12 comments
Open

zig c++: Doesn't support c++ modules #15496

kassane opened this issue Apr 28, 2023 · 12 comments
Labels
contributor friendly This issue is limited in scope and/or knowledge of Zig internals. enhancement Solving this issue will likely involve adding new logic or components to the codebase. zig cc Zig as a drop-in C compiler feature
Milestone

Comments

@kassane
Copy link
Contributor

kassane commented Apr 28, 2023

LLVM 16 currently adds support for c++ modules to clang/++, in addition to clang modules.
However, zig cc/c++ does not have optimal support based on the references below.

Test

zig - version 0.11.0-dev.2868+1a455b2dd

# download fork
$> git clone https://github.com/kassane/fmt
$> cd fmt/
$> zig build # add -DTests to build all tests (default libfmt only)
$> zig cc -x c++ -std=c++2b -Iinclude test/module-test.cc -o module -lc++ -Lzig-out/lib -lfmt
test/module-test.cc:47:8: fatal error: module 'fmt' not found
import fmt;
~~~~~~~^~~
1 error generated.

$>  zig cc -x c++-module -std=c++2b -Iinclude test/module-test.cc -o module -lc++ -Lzig-out/lib -lfmt
error: language not recognized: 'c++-module'

cc: @Vexu => (please, add tag)

References

@Vexu Vexu added contributor friendly This issue is limited in scope and/or knowledge of Zig internals. zig cc Zig as a drop-in C compiler feature enhancement Solving this issue will likely involve adding new logic or components to the codebase. labels May 31, 2023
@Vexu Vexu added this to the 0.12.0 milestone May 31, 2023
@nektro

This comment has been minimized.

@Vexu Vexu changed the title zig c++: Don't support c++ modules zig c++: Doesn't support c++ modules Oct 15, 2023
@aviplayer
Copy link

aviplayer commented Nov 5, 2023

Also tried with build.zig

const std = @import("std");
pub fn build(b: *std.build.Builder) void {
  const flags: []const []const u8 = &.{
      "-std=c++20"
  };

    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const exe = b.addExecutable(.{
        .name = "run",
        .target = target,
        .optimize = optimize,
    });

     exe.addCSourceFiles(&.{
        "./src/main.cpp",
        "./src/helloworld.cpp"
    }, flags);

    exe.linkLibCpp();

    b.installArtifact(exe);
}

getting fatal error: module 'helloworld' not found

with g++-13 everything is Ok.

@the-argus
Copy link

Adding -x c++-module to the end of flags yields the following:

clang failed with stderr: zig: warning: '-x c++-module' after last input file has no effect [-Wunused-command-line-argument]

And this seems to only err because of -Wall.

So I think to implement this, the zig cc invocation just needs to have -x c++-module right at the front, before everything else? Not sure what version of clang we're using, though.

documentation for clang 18 c++ modules: https://clang.llvm.org/docs/StandardCPlusPlusModules.html#how-to-enable-standard-c-modules

@Deins
Copy link

Deins commented Dec 24, 2023

Bumped into same issue trying to build amd ROCm stuff with -x hip

 error: error(compilation): clang failed with stderr: zig: warning: '-x hip' after last input file has no effect [-Wunused-command-line-argument]

@kevyuu
Copy link

kevyuu commented Feb 15, 2025

Hi, I want to know whether zig will ever support c++ module in the future before migrating my project from cmake to zig.

@allocgator
Copy link
Contributor

Hi @kassane - I would like to work on this issue. The references look quite old and I wanted to check whether they're still valid. If they're not can you let me know how I can get started?

@kassane
Copy link
Contributor Author

kassane commented Apr 28, 2025

The problem in question is still the same as that cited in the references.

However, I don't know if the current progress of the Zig project will give relevance to this topic.

cc: @alexrp - would it be planned to improve support for clang modules?

@alexrp
Copy link
Member

alexrp commented Apr 28, 2025

Depends on how hard it would be to add, I suppose.

Do you have some relevant links for how this stuff works with the vanilla clang driver?

@allocgator
Copy link
Contributor

allocgator commented Apr 28, 2025

Relevant discussion on Ziggit using vanilla clang: https://ziggit.dev/t/how-to-use-zig-build-for-importing-c-23s-std/9866/5


Edit 2: The last thread on that post was done on Fedora 42

@alexrp
Copy link
Member

alexrp commented Apr 28, 2025

I'm more curious about how the CLI works; CMake obscures too much.

@kassane
Copy link
Contributor Author

kassane commented Apr 28, 2025

@JoMazM
Copy link

JoMazM commented May 29, 2025

Is anyone working on this issue?

I'm trying following the instructions provided in the clang documentation using clang++-20 and zig c++ (0.15.0-dev.635+7dbd21bd5).

CPP Modules Example

// Hello.cppm
module;
#include <iostream>
export module Hello;
export void hello() {
  std::cout << "Hello World!\n";
// use.cpp
import Hello;
int main() {
  hello();
  return 0;
}

I tried to build with different commands and fails in different ways with zig, however clang-20 is successful:

clang++-20 is successful.

clang++-20 --verbose -H -std=c++20 Hello.cppm --precompile -o Hello.pcm > clang20_hello.log 2>&1
clang20_use.log

Zig using target x86_64-linux-gnu

I used triple x
zig c++ -target x86_64-linux-gnu --verbose -H -std=c++20 --stdlib=libc++ Hello.cppm --precompile -o Hello.pcm > zig_hello.log 2>&1

zig_hello.log

Error snippet:

In file included from Hello.cppm:3:
In file included from /usr/include/c++/v1/iostream:45:
In file included from /usr/include/c++/v1/ios:223:
In file included from /usr/include/c++/v1/__locale:17:
In file included from /usr/include/c++/v1/__locale_dir/locale_base_api.h:140:
In file included from /usr/include/c++/v1/__locale_dir/locale_base_api/bsd_locale_fallbacks.h:16:
In file included from /usr/include/locale.h:28:
/usr/include/c++/v1/stddef.h:38:17: fatal error: 'stddef.h' file not found
   38 | #  include_next <stddef.h>
      |                 ^~~~~~~~~~

Zig using libc++ libraries provided by clang-20

zig c++ -target x86_64-linux-gnu --verbose -H -std=c++20 --stdlib=libc++ -resource-dir /usr/lib/llvm-20/lib/clang/20 -isystem /usr/lib/llvm-20/bin/../include/c++/v1 -isystem /usr/lib/llvm-20/lib/clang/20/include Hello.cppm --precompile -o Hello.pcm > zig_hello.log 2>&1

The previous command is successful, however next step fails:
zig c++ -target x86_64-linux-gnu --verbose -H -std=c++20 --stdlib=libc++ -resource-dir /usr/lib/llvm-20/lib/clang/20 -isystem /usr/lib/llvm-20/bin/../include/c++/v1 -isystem /usr/lib/llvm-20/lib/clang/20/include use.cpp -fmodule-file=Hello=Hello.pcm -o Hello.out

Error snippet:

error: stack protector mode differs in AST file 'Hello.pcm' vs. current file
error: AST file 'Hello.pcm' was compiled for the target 'x86_64-unknown-linux-gnu' but the current translation unit is being compiled for target 'x86_64-unknown-linux5.10.0-gnu2.31.0'
error: module file Hello.pcm cannot be loaded due to a configuration mismatch with the current compilation [-Wmodule-file-config-mismatch]
use.cpp:4:3: error: use of undeclared identifier 'hello'
    4 |   hello();
      |   ^
4 errors generated.

What are the possible solutions here?
What are the files/directories of interest to look into the zig compiler to enable modules?
Should zig package libc++ library as well to have an standalone compiler instead of relying on the system library?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor friendly This issue is limited in scope and/or knowledge of Zig internals. enhancement Solving this issue will likely involve adding new logic or components to the codebase. zig cc Zig as a drop-in C compiler feature
Projects
None yet
Development

No branches or pull requests

10 participants