<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/80639>80639</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [libc++] modules: missing "initializer for module std" symbol in `libc++.so`
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            libc++
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          jiixyj
      </td>
    </tr>
</table>

<pre>
    I'm testing the new support for installed modules by created an "imported target" in CMake that points to `libc++.so` and the installed importable module units (`std.cppm`) with the `Findcxx.cmake` script listed below.

The reason I want to use an imported target is: I don't want every consumer of the `std` module to have to build its own `std` CMake target with `add_library(std)`. This won't scale I think. Of course, CMake will probably figure out a way to make using the `std` module easier for consumers, but I guess they will stumble upon this issue as well.

When I link against that imported target, I'm running into the issue that the "initializer for module std" symbol is missing from `libc++.so`, resulting in linker errors:

```
ld.lld: error: undefined symbol: initializer for module std
```

This means I have to hack in this symbol in some way, for example by adding this file to my build:

```c++
extern "C" void _ZGIW3std()
{
}
```

Could `libc++.so` provide `_ZGIW3std`, or are there reasons against it?

`Findcxx.cmake`:

```cmake
if(TARGET cxx::std)
 return()
endif()

set(PATH_TO_LIBCXX "...")

add_library(cxx::std STATIC IMPORTED)

set_target_properties(
  cxx::std
  PROPERTIES CXX_EXTENSIONS "OFF"
             IMPORTED_CXX_MODULES_COMPILE_FEATURES "cxx_std_23"
             INTERFACE_COMPILE_FEATURES "cxx_std_23"
 IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
 IMPORTED_LOCATION
 "${PATH_TO_LIBCXX}/lib/x86_64-unknown-linux-gnu/libc++.so")

target_sources(
 cxx::std INTERFACE FILE_SET CXX_MODULES BASE_DIRS "${PATH_TO_LIBCXX}" FILES
 "${PATH_TO_LIBCXX}/share/libc++/v1/std.cppm")
set_property(
  SOURCE "${PATH_TO_LIBCXX}/share/libc++/v1/std.cppm"
  APPEND
  PROPERTY COMPILE_OPTIONS
 "-Wno-reserved-module-identifier;-Wno-reserved-user-defined-literal")

# This is needed to provide the "initializer for module std" symbol:
#
#     extern "C" void _ZGIW3std()
#     {
# }
#
target_sources(cxx::std INTERFACE "${CMAKE_CURRENT_LIST_DIR}/std.cpp")
```

Eventually, I want to soup up this find script by parsing the new `libc++.modules.json`, but this is what I got so far.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVttv4j4W_mvMyxEoOG2ABx4oDbPRtKWCVNPdl8iJDXhq7MgXLvvXr-yEAm1n1JV-qAIazuX7zvl8fIgxfC0ZG6PbO3R73yHObpQe_-b8cPzdKRU9jjOEB1uwzFgu12A3DCTbg3F1rbSFldLApbFECEZhq6gTzEB5hEozYhkFIgFhzLfemlGwRK-ZRRgDlzB9JG8M7IZYqBWX1oBVgJJI8LJC-A7hu55RKImASBoynzM1AUkpWJsUnOTWAMJDlETG0l5V11uURAiPYM_tJvijJJpxSavDoVdtyRvzsU2leW1BcOPxlUyofQ9F9yiaNO_5hoFmxCgJGeyJtB6kM8wz-0ALuEHxBDKgSiI8sI052zF9hEpJ47ZMg1qdoBhLPYAWv1WwIbvwWTouKHg6ai8vLNt6NbkCKZREhNJC8FITfUR46C3xCCVRD_INN7BvkZiKCAYZ2A2Xbz2Yr6BSThuG8LQNu-dCQK1VSUpxhBVfO81AOQsE9uToYfmKgTMnGXxiwIjhTAdJnNgaH790FjJYO2aMdzw2qYx1W98-VyvpYRngxjgGxMCeCXHVg18b5qsvuHwDsiZeBo1sPulqCo1etZPSA-XSqkY6IXhwCuAx5pJbTgT_b4u5ZREqiMEct6USwA1suQmcV1ptv1KnT6qZccI2CQNMpoFprbQXxCUTb9_8hX8F7QlBvWiCtf_iJGUrLhltIfhnf4H6VdSTcD14RqSB7F1aG1K9eYyh4CeOEozaMt9mz8UnYAeyrQXzB5lQ2nScG1jxRqjbY6PRP5Fr69M8ZQfLdJgCU1_YneIUiv_8y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