CXX-Modules: import std does still not work on OSX with g++-15

I have this simple example copied form https://www.kitware.com/import-std-in-cmake-3-30/:

# CMake 3.30 is required for C++23 `import std` support; we use 3.29.20240416
# here so that in-development versions satisfy it.
cmake_minimum_required(VERSION 4.0...4.2)

# Set experimental flag to enable `import std` support from CMake.
# This must be enabled before C++ language support.
# set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD # This specific value changes as experimental support evolves. See
#     # `Help/dev/experimental.rst` in the CMake source corresponding to
#     # your CMake build for the exact value to use.
#     "a9e1cf81-9932-4810-974b-6eccaf14e457"
# )
set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD "d0edc3af-4c50-42ea-a356-e2862fe7a444")

# C++ needs to be enabled.
project(import_std LANGUAGES CXX)

# Tell CMake that we explicitly want `import std`. This will initialize the
# property on all targets declared after this to 1
set(CMAKE_CXX_MODULE_STD 1)

# Make a library.
add_library(uses_std STATIC)
# Add sources.
target_sources(uses_std PRIVATE uses_std.cxx)
# Tell CMake we're using C++23 but only C++20 is needed to consume it.
target_compile_features(uses_std PRIVATE cxx_std_23 INTERFACE cxx_std_20)

# Make an executable.
add_executable(main)
# Note that this source is *not* allowed to `import std` as it ends up
# with only C++20 support due to the `uses_std` INTERFACE requirements.
target_sources(main PRIVATE main.cxx)
target_link_libraries(main PRIVATE uses_std)

enable_testing()
add_test(NAME main COMMAND main)

but it results in linker errors:

bash-5.3$ CXX=g++-15 cmake -G Ninja -S . -B build --fresh 
-- The CXX compiler identification is GNU 15.2.0
-- Checking whether CXX compiler has -isysroot
-- Checking whether CXX compiler has -isysroot - yes
-- Checking whether CXX compiler supports OSX deployment target flag
-- Checking whether CXX compiler supports OSX deployment target flag - yes
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/local/bin/g++-15 - skipped
-- Detecting CXX compile features
CMake Warning (dev) at /Users/clausklein/.local/share/cmake-4.2/Modules/Compiler/CMakeCommonCompilerMacros.cmake:248 (cmake_language):
  CMake's support for `import std;` in C++23 and newer is experimental.  It
  is meant only for experimentation and feedback to CMake developers.
Call Stack (most recent call first):
  /Users/clausklein/.local/share/cmake-4.2/Modules/CMakeDetermineCompilerSupport.cmake:113 (cmake_create_cxx_import_std)
  /Users/clausklein/.local/share/cmake-4.2/Modules/CMakeTestCXXCompiler.cmake:83 (CMAKE_DETERMINE_COMPILER_SUPPORT)
  CMakeLists.txt:15 (project)
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Detecting CXX compile features - done
-- Configuring done (1.0s)
-- Generating done (0.0s)
-- Build files have been written to: /Users/clausklein/Workspace/cpp/cxx20/fmt-module/example/build
bash-5.3$ ninja -C build -v
ninja: Entering directory `build'
[1/12] /usr/local/bin/g++-15   -std=gnu++23 -fmodule-only -E -x c++ /usr/local/Cellar/gcc/15.2.0/include/c++/15/bits/std.compat.cc -MT CMakeFiles/__CMAKE__CXX23@synth_8e2c785ad87e.dir/1ccb32d972a2.bmi.ddi -MD -MF CMakeFiles/__CMAKE__CXX23@synth_8e2c785ad87e.dir/1ccb32d972a2.bmi.ddi.d -fmodules-ts -fdeps-file=CMakeFiles/__CMAKE__CXX23@synth_8e2c785ad87e.dir/1ccb32d972a2.bmi.ddi -fdeps-target=CMakeFiles/__CMAKE__CXX23@synth_8e2c785ad87e.dir/1ccb32d972a2.bmi -fdeps-format=p1689r5 -o CMakeFiles/__CMAKE__CXX23@synth_8e2c785ad87e.dir/1ccb32d972a2.bmi.ddi.i
[2/12] /usr/local/bin/g++-15   -std=gnu++23 -fmodule-only -E -x c++ /usr/local/Cellar/gcc/15.2.0/include/c++/15/bits/std.cc -MT CMakeFiles/__CMAKE__CXX23@synth_8e2c785ad87e.dir/67f69854274c.bmi.ddi -MD -MF CMakeFiles/__CMAKE__CXX23@synth_8e2c785ad87e.dir/67f69854274c.bmi.ddi.d -fmodules-ts -fdeps-file=CMakeFiles/__CMAKE__CXX23@synth_8e2c785ad87e.dir/67f69854274c.bmi.ddi -fdeps-target=CMakeFiles/__CMAKE__CXX23@synth_8e2c785ad87e.dir/67f69854274c.bmi -fdeps-format=p1689r5 -o CMakeFiles/__CMAKE__CXX23@synth_8e2c785ad87e.dir/67f69854274c.bmi.ddi.i
[3/12] /Users/clausklein/.local/bin/cmake -E cmake_ninja_dyndep --tdi=CMakeFiles/__CMAKE__CXX23@synth_8e2c785ad87e.dir/CXXDependInfo.json --lang=CXX --modmapfmt=gcc --dd='CMakeFiles/__CMAKE__CXX23@synth_8e2c785ad87e.dir/CXX.dd' @'CMakeFiles/__CMAKE__CXX23@synth_8e2c785ad87e.dir/CXX.dd'.rsp
[4/12] /usr/local/bin/g++-15   -std=gnu++23 -E -x c++ /Users/clausklein/Workspace/cpp/cxx20/fmt-module/example/uses_std.cxx -MT CMakeFiles/uses_std.dir/uses_std.cxx.o.ddi -MD -MF CMakeFiles/uses_std.dir/uses_std.cxx.o.ddi.d -fmodules-ts -fdeps-file=CMakeFiles/uses_std.dir/uses_std.cxx.o.ddi -fdeps-target=CMakeFiles/uses_std.dir/uses_std.cxx.o -fdeps-format=p1689r5 -o CMakeFiles/uses_std.dir/uses_std.cxx.o.ddi.i
[5/12] /Users/clausklein/.local/bin/cmake -E cmake_ninja_dyndep --tdi=CMakeFiles/uses_std.dir/CXXDependInfo.json --lang=CXX --modmapfmt=gcc --dd=CMakeFiles/uses_std.dir/CXX.dd @CMakeFiles/uses_std.dir/CXX.dd.rsp
[6/12] /usr/local/bin/g++-15   -std=gnu++20 -E -x c++ /Users/clausklein/Workspace/cpp/cxx20/fmt-module/example/main.cxx -MT CMakeFiles/main.dir/main.cxx.o.ddi -MD -MF CMakeFiles/main.dir/main.cxx.o.ddi.d -fmodules-ts -fdeps-file=CMakeFiles/main.dir/main.cxx.o.ddi -fdeps-target=CMakeFiles/main.dir/main.cxx.o -fdeps-format=p1689r5 -o CMakeFiles/main.dir/main.cxx.o.ddi.i
[7/12] /Users/clausklein/.local/bin/cmake -E cmake_ninja_dyndep --tdi=CMakeFiles/main.dir/CXXDependInfo.json --lang=CXX --modmapfmt=gcc --dd=CMakeFiles/main.dir/CXX.dd @CMakeFiles/main.dir/CXX.dd.rsp
[8/12] ccache /usr/local/bin/g++-15   -std=gnu++20 -MD -MT CMakeFiles/main.dir/main.cxx.o -MF CMakeFiles/main.dir/main.cxx.o.d -fmodules-ts -fmodule-mapper=CMakeFiles/main.dir/main.cxx.o.modmap -MD -fdeps-format=p1689r5 -x c++ -o CMakeFiles/main.dir/main.cxx.o -c /Users/clausklein/Workspace/cpp/cxx20/fmt-module/example/main.cxx
[9/12] /usr/local/bin/g++-15   -std=gnu++23 -fmodule-only -MD -MT 'CMakeFiles/__CMAKE__CXX23@synth_8e2c785ad87e.dir/67f69854274c.bmi' -MF CMakeFiles/__CMAKE__CXX23@synth_8e2c785ad87e.dir/67f69854274c.bmi.d -fmodules-ts -fmodule-mapper=CMakeFiles/__CMAKE__CXX23@synth_8e2c785ad87e.dir/67f69854274c.bmi.modmap -MD -fdeps-format=p1689r5 -x c++ -o 'CMakeFiles/__CMAKE__CXX23@synth_8e2c785ad87e.dir/67f69854274c.bmi' -c /usr/local/Cellar/gcc/15.2.0/include/c++/15/bits/std.cc
[10/12] ccache /usr/local/bin/g++-15   -std=gnu++23 -MD -MT CMakeFiles/uses_std.dir/uses_std.cxx.o -MF CMakeFiles/uses_std.dir/uses_std.cxx.o.d -fmodules-ts -fmodule-mapper=CMakeFiles/uses_std.dir/uses_std.cxx.o.modmap -MD -fdeps-format=p1689r5 -x c++ -o CMakeFiles/uses_std.dir/uses_std.cxx.o -c /Users/clausklein/Workspace/cpp/cxx20/fmt-module/example/uses_std.cxx
[11/12] : && /Users/clausklein/.local/bin/cmake -E rm -f libuses_std.a && /usr/bin/ar qc libuses_std.a  CMakeFiles/uses_std.dir/uses_std.cxx.o && /usr/bin/ranlib libuses_std.a && /Users/clausklein/.local/bin/cmake -E touch libuses_std.a && :
[12/12] : && /usr/local/bin/g++-15  -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/main.dir/main.cxx.o -o main  libuses_std.a && :
FAILED: [code=1] main 
: && /usr/local/bin/g++-15  -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/main.dir/main.cxx.o -o main  libuses_std.a && :
Undefined symbols for architecture x86_64:
  "initializer for module std", referenced from:
      __static_initialization_and_destruction_0() in libuses_std.a[2](uses_std.cxx.o)
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
bash-5.3$ 

What is wrong or is missing?

I’ve seen such errors with g+±15 on Linux, when tried to use symbol hiding. Even found a g++ bug for it.

Here is the GCC bug:

Until this is fixed, you cannot use -fvisibility=hidden with PUBLIC modules in shared libraries.

1 Like

However, I don’t see the flag used here. Something else is going wrong.

I brought this up as I wasn’t sure what could be hidden in the *.rsp files

Those are given to the collation steps; the compiler lines are fully present here.