Skip to content

Zig compiles C++ jthread with errors #22989

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

Closed
inschrift-spruch-raum opened this issue Feb 23, 2025 · 6 comments
Closed

Zig compiles C++ jthread with errors #22989

inschrift-spruch-raum opened this issue Feb 23, 2025 · 6 comments
Labels
upstream An issue with a third party project that Zig uses. zig cc Zig as a drop-in C compiler feature

Comments

@inschrift-spruch-raum
Copy link

Zig Version

0.13.0

Steps to Reproduce and Observed Behavior

I found an error when compiling a C++project using jthread
So I wrote a small demo

#include <iostream>
#include <thread>

int main() {
    std::jthread t([]{
        std::cout << "Thread executed\n";
    });
    return 0;
}
const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});

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

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

    exe.addCSourceFile(.{
        .file = b.path("src/main.cpp"),
        .flags = &.{"-std=c++20"},
    });

    exe.linkLibCpp();

    b.installArtifact(exe);
}

Found the following error message

zig build
install
└─ install sac
   └─ zig build-exe sac Debug native 1 errors
C:\Users\Raum\Documents\sac\src\main.cpp:5:10: error: no member named 'jthread' in namespace 'std'
    std::jthread t([]{
    ~~~~~^
error: the following command failed with 1 compilation errors:
C:\Users\Raum\AppData\Local\Microsoft\WinGet\Packages\zig.zig_Microsoft.Winget.Source_8wekyb3d8bbwe\zig-windows-x86_64-0.13.0\zig.exe build-exe -cflags -std=c++20 -- C:\Users\Raum\Documents\sac\src\main.cpp -ODebug -Mroot -lc++ --cache-dir C:\Users\Raum\Documents\sac\.zig-cache --global-cache-dir C:\Users\Raum\AppData\Local\zig --name sac --listen=-
Build Summary: 0/3 steps succeeded; 1 failed (disable with --summary none)
install transitive failure
└─ install sac transitive failure
   └─ zig build-exe sac Debug native 1 errors
error: the following build command failed with exit code 1:
C:\Users\Raum\Documents\sac\.zig-cache\o\61e53e351dc3c14719ba6a8761e0abb6\build.exe C:\Users\Raum\AppData\Local\Microsoft\WinGet\Packages\zig.zig_Microsoft.Winget.Source_8wekyb3d8bbwe\zig-windows-x86_64-0.13.0\zig.exe C:\Users\Raum\Documents\sac C:\Users\Raum\Documents\sac\.zig-cache C:\Users\Raum\AppData\Local\zig --seed 0xa1836ee2 -Z213377d274cdf92f

My environment is

zig env
{
 "zig_exe": "C:\\Users\\Raum\\AppData\\Local\\Microsoft\\WinGet\\Packages\\zig.zig_Microsoft.Winget.Source_8wekyb3d8bbwe\\zig-windows-x86_64-0.13.0\\zig.exe",
 "lib_dir": "C:\\Users\\Raum\\AppData\\Local\\Microsoft\\WinGet\\Packages\\zig.zig_Microsoft.Winget.Source_8wekyb3d8bbwe\\zig-windows-x86_64-0.13.0\\lib",
 "std_dir": "C:\\Users\\Raum\\AppData\\Local\\Microsoft\\WinGet\\Packages\\zig.zig_Microsoft.Winget.Source_8wekyb3d8bbwe\\zig-windows-x86_64-0.13.0\\lib\\std",
 "global_cache_dir": "C:\\Users\\Raum\\AppData\\Local\\zig",
 "version": "0.13.0",
 "target": "x86_64-windows.win10_fe...win10_fe-gnu",
 "env": {
  "ZIG_GLOBAL_CACHE_DIR": null,
  "ZIG_LOCAL_CACHE_DIR": null,
  "ZIG_LIB_DIR": null,
  "ZIG_LIBC": null,
  "ZIG_BUILD_RUNNER": null,
  "ZIG_VERBOSE_LINK": null,
  "ZIG_VERBOSE_CC": null,
  "ZIG_BTRFS_WORKAROUND": null,
  "ZIG_DEBUG_CMD": null,
  "CC": null,
  "NO_COLOR": null,
  "CLICOLOR_FORCE": null,
  "XDG_CACHE_HOME": null,
  "HOME": null
 }
}

Expected Behavior

Normal compilation

@inschrift-spruch-raum inschrift-spruch-raum added the bug Observed behavior contradicts documented or intended behavior label Feb 23, 2025
@alexrp
Copy link
Member

alexrp commented Feb 23, 2025

It's not available because we're currently on LLVM 19 and libc++ only gained (non-experimental) std::jthread support in LLVM 20. This means it'll become available in Zig 0.15.0 when we upgrade to LLVM 20.

I'll close this issue because there isn't really anything in particular for us to do here; the support will come automatically with #22780.

@alexrp alexrp closed this as not planned Won't fix, can't repro, duplicate, stale Feb 23, 2025
@alexrp alexrp added upstream An issue with a third party project that Zig uses. zig cc Zig as a drop-in C compiler feature and removed bug Observed behavior contradicts documented or intended behavior labels Feb 23, 2025
@inschrift-spruch-raum
Copy link
Author

be sorry
However, I still hope to be able to compile successfully in the current situation
Can you provide me with some ideas
Sorry to bother you.

@LearnWebGitHub
Copy link

LearnWebGitHub commented Mar 2, 2025

be sorry However, I still hope to be able to compile successfully in the current situation Can you provide me with some ideas Sorry to bother you.

If you have the latest GCC (mine is 14) you can use libstdc++ instead of libc++. (Sometimes it can also solve ABI incompatibility issue if other libraries use libstc++. #18300)

Because the library is on your machine, it's not portable if you don't write a more complex build script, but it might solve the problem for now.
I'm still waiting for official support #3963

// remove this line: exe.linkLibCpp();

// libstdc++
const verion = "14.2.1";
const machine = "x86_64-pc-linux-gnu";
const lazy = std.Build.LazyPath;
exe.addIncludePath(lazy{ .cwd_relative = "/usr/include/c++/" ++ verion });
exe.addIncludePath(lazy{ .cwd_relative = "/usr/include/c++/" ++ verion ++ "/" ++ machine });
exe.addLibraryPath(lazy{ .cwd_relative = "/usr/lib/gcc/" ++ machine ++ "/" ++ verion });
exe.addObjectFile(lazy{ .cwd_relative = "/usr/lib/libstdc++.so" });
exe.addObjectFile(lazy{ .cwd_relative = "/usr/lib/libgcc_s.so" });
exe.linkLibC();

@inschrift-spruch-raum
Copy link
Author

Thank you, I am also studying this plan
However, I compile in a Windows environment, and completing this task may be quite troublesome

@LearnWebGitHub
Copy link

LearnWebGitHub commented Mar 2, 2025

Thank you, I am also studying this plan However, I compile in a Windows environment, and completing this task may be quite troublesome

Oops, I see. Hmmm...

Have you tried this?
Add -fexperimental-library when using -std=c++20

Or:
Not sure if it'd help but my solution without GCC (I'm on Debian, sorry for not having Windows environment now):
Download "jthread.hpp" and "stop_token.hpp" from Nico Josuttis' repo (He is one of the authors of std::jthread)
If you use -std=c++17 it should compile.
If you meet reference conflics when using -std=c++20 or -std=c++23 (I'm not sure about this but I guess if you meet this issue you might be able to use -fexperimental-library already), change their namespace from std to any name you want to avoid it.

const files = &[_][]const u8{
    "src/main.cpp",
};
const flags = &[_][]const u8{
    "-std=c++23",
    "-fexperimental-library",
    "-Wall",
    "-Wextra",
    "-Wpedantic",
};
exe.addCSourceFiles(.{
    .files = files,
    .flags = flags,
});
exe.addIncludePath(b.path("src"));

@inschrift-spruch-raum
Copy link
Author

Solved it! It's really amazing...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
upstream An issue with a third party project that Zig uses. zig cc Zig as a drop-in C compiler feature
Projects
None yet
Development

No branches or pull requests

3 participants