Skip to content

C++20 modules and std::jthread: link failed with clang & libc++ v21.1.x #172241

@bshoshany

Description

@bshoshany

I ran into this issue when compiling my thread pool library using Clang in C++20 module mode. After some investigation I was able to pin the issue down to using std::jthread in both the module and the program that imports it. I created the following minimal working example:

// jthread_error.cppm
module;
#include <thread>
export module jthread_error;
export struct jthread_error_struct { std::jthread thread; };
// jthread_error.cpp
#include <thread>
import jthread_error;
int main() { jthread_error_struct test; }

Compile with:

clang++ jthread_error.cppm --precompile -std=c++20 -I include -o jthread_error.pcm -stdlib=libc++
clang++ jthread_error.cpp -fmodule-file="jthread_error=jthread_error.pcm" -std=c++20 -o jthread_error -stdlib=libc++

When I try this on Windows 11 25H2 build 26200.7462 with Clang and libc++ v21.1.7 installed via MSYS2 in the CLANG64 environment, I get the following error:

ld.lld: error: undefined symbol: std::__1::__atomic_unique_lock<unsigned int, 2u>::__set_locked_bit[abi:ne210107]
>>> referenced by C:/Users/barak/AppData/Local/Temp/jthread_error-c6c4a2.o:(.refptr._ZNSt3__120__atomic_unique_lockIjLj2EE16__set_locked_bitB8ne210107E)
clang++: error: linker command failed with exit code 1 (use -v to see invocation)

Similarly, when I try this on Ubuntu 25.10 with Clang and libc++ v21.1.8 installed via apt, I get the following error:

/usr/bin/ld: /tmp/jthread_error-a7e681.o: in function `std::__1::__atomic_unique_lock<unsigned int, 2u>::__lock[abi:ne210108]()':
jthread_error.cpp:(.text._ZNSt3__120__atomic_unique_lockIjLj2EE6__lockB8ne210108Ev[_ZNSt3__120__atomic_unique_lockIjLj2EE6__lockB8ne210108Ev]+0x24): undefined reference to `std::__1::__atomic_unique_lock<unsigned int, 2u>::__set_locked_bit[abi:ne210108]'  
/usr/bin/ld: jthread_error: hidden symbol `_ZNSt3__120__atomic_unique_lockIjLj2EE16__set_locked_bitB8ne210108E' isn't defined   
/usr/bin/ld: final link failed: bad value
clang++: error: linker command failed with exit code 1 (use -v to see invocation)

Note that this does not happen on any other compiler I tested, nor does it happen with Clang when using -stdlib=libstdc++ on Ubuntu or the native Windows LLVM release (using the MSVC standard library), or with libc++ if using std::thread instead of std::jthread.

Could this be an issue with libc++ itself?

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:modulesC++20 modules and Clang Header Modules

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions