Skip to content

[Bug]: Linkage error when using std::jthread and c++20 modules #2259

Open

Description

@mariusunitbv

Description

In my project i use c++20 modules but with normal includes in the global fragment.
If i define a std::jthread anywhere in my class either it be a field or a local variable I get a final linking error.

/core.version.cppm.o  C:/Users/MariusLaptop/Documents/Workspace/Proiecte/nodesforge/android/app/build/intermediates/cxx/Debug/4h1m5s3g/obj/arm64-v8a/libSDK.so  C:/Users/MariusLaptop/Documents/Workspace/Proiecte/nodesforge/android/app/build/intermediates/cxx/Debug/4h1m5s3g/obj/arm64-v8a/libSDL3.so  -latomic -lm && cd ."
  ld.lld: error: undefined hidden symbol: std::__ndk1::__atomic_unique_lock<unsigned int, 2u>::__set_locked_bit[abi:ne210000]
  >>> referenced by atomic_unique_lock.h:76 (C:/Users/MariusLaptop/AppData/Local/Android/Sdk/ndk/30.0.16248370/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/c++/v1/__stop_token/atomic_unique_lock.h:76)
  >>>               nodesforge_build/Core/CMakeFiles/Core.dir/src/services/job/core.services.job.cpp.o:(std::__ndk1::__atomic_unique_lock<unsigned int, 2u>::__lock[abi:ne210000]())
  >>> referenced by atomic_unique_lock.h:76 (C:/Users/MariusLaptop/AppData/Local/Android/Sdk/ndk/30.0.16248370/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/c++/v1/__stop_token/atomic_unique_lock.h:76)
  >>>               nodesforge_build/Core/CMakeFiles/Core.dir/src/services/job/core.services.job.cpp.o:(std::__ndk1::__atomic_unique_lock<unsigned int, 2u>::__lock[abi:ne210000]())
  clang++: error: linker command failed with exit code 1 (use -v to see invocation)
  ninja: build stopped: subcommand failed.

This happened with my project using Emscripten too but it was magically fixed by updating to the latest version.
I tried to update the ndk too to ndkVersion = "30.0.16248370", but to no avail.

As a very quick workaround I changed the __set_locked_bit in atomic_unique_lock.h from a lambda to a normal function and the linkage error is gone. This is a very bad workaround from me but does the job since I won't deploy my app anywhere with Action.

Before:

_LIBCPP_HIDE_FROM_ABI static constexpr auto __set_locked_bit = [](_State __state) { return __state | _LockedBit; };

After:

_LIBCPP_HIDE_FROM_ABI static constexpr _State __set_locked_bit(_State __state) { return __state | _LockedBit; }

Upstream bug

No response

Commit to cherry-pick

No response

I am using a supported NDK

  • I have checked and the NDK I'm using is currently supported

Affected versions

r30

Host OS

Windows

Host OS version

Windows 11

Affected ABIs

armeabi-v7a

Activity

  1. Sharjeel-Khan commented on Nov 2, 2026

    @Sharjeel-Khan
    Collaborator

    This is a clang bug (llvm/llvm-project#172241). When a file includes <thread> before importing a module that also includes <thread> in its global module fragment, clang drops that file's definition of __set_locked_bit and keeps only an undefined reference to it. Since the symbol is hidden, the link fails.

    The fix (llvm/llvm-project@6e1ab3a4310d) landed upstream already and it is included in r614150, which will be the compiler for r31. I tested a reproducer with r614150 and the linkage error is gone. I will try to cherrypick it back into r30's compiler (r574158) if possible. Until then, you can work around it by moving #include <thread> after the import statements in files that import the module.

    If curious, the reproducer is below:
    CMakeLists.txt

    cmake_minimum_required(VERSION 3.28)
    project(nodesforge CXX)
    set(CMAKE_CXX_STANDARD 20)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    add_subdirectory(Core)
    add_library(main SHARED main.cpp)
    target_link_libraries(main PRIVATE Core atomic m)

    Core/CMakeLists.txt

    add_library(Core STATIC)
    target_sources(Core
      PUBLIC FILE_SET CXX_MODULES FILES
        src/version/core.version.cppm
        src/services/job/core.services.job.cppm
      PRIVATE
        src/services/job/core.services.job.cpp)

    Core/src/version/core.version.cppm

    module;
    #include <string>
    export module core.version;
    export inline std::string version() { return "1.0"; }

    Core/src/services/job/core.services.job.cppm

    module;
    #include <functional>
    #include <thread>
    export module core.services.job;
    export class JobService {
    public:
      void start(std::function<void()> fn);
      void stop();
    private:
      std::jthread worker_;   // std::jthread as a field
    };
    export void start_jobs();
    export void stop_jobs();

    Core/src/services/job/core.services.job.cpp

    module;
    #include <functional>
    #include <thread>
    module core.services.job;
    void JobService::start(std::function<void()> fn) {
      worker_ = std::jthread([fn](std::stop_token st) { while (!st.stop_requested()) fn(); });
    }
    void JobService::stop() { worker_.request_stop(); }
    static JobService* g_jobs = nullptr;
    void start_jobs() { g_jobs = new JobService; g_jobs->start([] {}); }
    void stop_jobs() { g_jobs->stop(); delete g_jobs; }

    main.cpp

    import core.services.job;
    import core.version;
    extern "C" int run() { start_jobs(); stop_jobs(); return (int)version().size(); }

    Case 1: NDK r30 fails to link

    NDK=/path/to/android-ndk-r30
    cmake -S . -B build-r30 -G Ninja \
      -DCMAKE_TOOLCHAIN_FILE=$NDK/build/cmake/android.toolchain.cmake \
      -DANDROID_ABI=armeabi-v7a -DANDROID_PLATFORM=android-24 -DCMAKE_BUILD_TYPE=Debug
    ninja -C build-r30
    ld.lld: error: undefined hidden symbol: std::__ndk1::__atomic_unique_lock<unsigned int, 2u>::__set_locked_bit[abi:ne210000]
    >>> referenced by atomic_unique_lock.h:0 (.../sysroot/usr/include/c++/v1/__stop_token/atomic_unique_lock.h:0)
    >>>               core.services.job.cpp.o:(std::__ndk1::__atomic_unique_lock<unsigned int, 2u>::__lock[abi:ne210000]()) in archive Core/libCore.a
    clang++: error: linker command failed with exit code 1 (use -v to see invocation)
    ninja: build stopped: subcommand failed.
    

    arm64-v8a fails the same way.

    Case 2: NDK r30 with a newer compiler

    NDK=/path/to/android-ndk-r30
    R=/path/to/prebuilts/clang/host/linux-x86/clang-r614150
    SHIM=/tmp/ndk-r30-with-r614150
    P=$SHIM/toolchains/llvm/prebuilt/linux-x86_64
    rm -rf $SHIM && mkdir -p $P
    for e in $NDK/*; do [ "$(basename $e)" = toolchains ] || ln -s $e $SHIM/; done
    for e in $NDK/toolchains/llvm/prebuilt/linux-x86_64/*; do
      case $(basename $e) in bin|lib) ;; *) ln -s $e $P/;; esac
    done
    ln -s $R/bin $P/bin && ln -s $R/lib $P/lib
    
    # r614150's resource dir has no libatomic.a, so borrow r30's for the link.
    cmake -S . -B build-r614150 -G Ninja \
      -DCMAKE_TOOLCHAIN_FILE=$SHIM/build/cmake/android.toolchain.cmake \
      -DANDROID_ABI=armeabi-v7a -DANDROID_PLATFORM=android-24 -DCMAKE_BUILD_TYPE=Debug \
      -DCMAKE_SHARED_LINKER_FLAGS="-L$NDK/toolchains/llvm/prebuilt/linux-x86_64/lib/clang/21/lib/linux/arm"
    ninja -C build-r614150
    [10/12] Building CXX object Core/CMakeFiles/Core.dir/src/services/job/core.services.job.cpp.o
    [11/12] Linking CXX static library Core/libCore.a
    [12/12] Linking CXX shared library libmain.so
    
  2. moved this from Unconfirmed to Triaged in NDK r31on Nov 2, 2026
  3. self-assigned this
    on Nov 2, 2026
  4. moved this from Triaged to Merged in NDK r31on Nov 3, 2026
  5. Sharjeel-Khan commented on Dec 1, 2026

    @Sharjeel-Khan
    Collaborator

    NDK r31 Beta 1 released today and includes the fix but I will not close this bug until r30b gets it also.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

  • Status
    Merged

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions