C++ 20 Modules Update

Most Fortran compilers avoid modifying the .mod file generated for a module if the interface didn’t change. In this case Clang should write a temporary .pcm file with a different name, and then rename it to replace the real .pcm file only if the content changed. Or some equivalent replace-if-different logic.

Clang issue: Clang should not re-write .pcm for C++ module if it's unchanged · Issue #61457 · llvm/llvm-project · GitHub

Hi, I am also trying c++20 modules with clang, and came up with this discussion. My simple example compiles now. However, the compile_commands.json generated by cmake does not contain any information about modules.

I read the discussion above, and @brad.king says module information is not included before the dependencies are scanned. However, even after I successfully compile the project (which means the dependencies are scanned), I still get errors from my lsp-server. Any workaround? Thanks.

The files are generated during the build and look like @path/to/some/name.modmap on the command line.

What errors?

The Xcode generator isn’t working with C++ modules. Will it work if I setup a new Xcode “toolchain” that points to the latest Clang?

I’m trying to figure out how to build and deploy to my iPad, and eventually to the App Store.

CMake Error in AppleInterop/CMakeLists.txt:
  The "AppleInterop" target contains C++ module sources which are not
  supported by the generator

Thanks for your reply. I will present you with some details of it.

The CMakeList.txt is as follow:

cmake_minimum_required(VERSION 3.26.0)

project("nativetest")

set(CMAKE_EXPERIMENTAL_CXX_MODULE_CMAKE_API "2182bf5c-ef0d-489a-91da-49dbc3090d2a")
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP 1)
string(CONCAT CMAKE_EXPERIMENTAL_CXX_SCANDEP_SOURCE
  "${CMAKE_CXX_COMPILER_CLANG_SCAN_DEPS}"
  " -format=p1689"
  " --"
  " <CMAKE_CXX_COMPILER> <DEFINES> <INCLUDES> <FLAGS>"
  " -x c++ <SOURCE> -c -o <OBJECT>"
  " -MT <DYNDEP_FILE>"
  " -MD -MF <DEP_FILE>"
  " > <DYNDEP_FILE>")
set(CMAKE_EXPERIMENTAL_CXX_MODULE_MAP_FORMAT "clang")
set(CMAKE_EXPERIMENTAL_CXX_MODULE_MAP_FLAG "@<MODULE_MAP_FILE>")

set(CMAKE_CXX_EXTENSIONS OFF)

add_executable(my_program)
target_sources(
        my_program
        PRIVATE
        main.cxx

        PRIVATE FILE_SET CXX_MODULES
        BASE_DIRS .
        FILES
        importable.cxx
)

target_compile_features(my_program PUBLIC cxx_std_20)

main.cxx on the other hand is:

import importable;

int main(int argc, char **argv) {
    return from_import();
}

and importable.cxx is:

export module importable;

export int from_import() {
    return 0;
}

I used vscode with CMake tool plugin and clangd plugin, and it show errors Module 'importable' not foundclang(module_not_found).

The generated compile_commands.json is:

[
{
  "directory": "/home/loves/test/build",
  "command": "/usr/bin/clang++-16 -O3 -DNDEBUG -std=c++20 -o CMakeFiles/my_program.dir/main.cxx.o -c /home/loves/test/main.cxx",
  "file": "/home/loves/test/main.cxx",
  "output": "CMakeFiles/my_program.dir/main.cxx.o"
},
{
  "directory": "/home/loves/test/build",
  "command": "/usr/bin/clang++-16 -O3 -DNDEBUG -std=c++20 -o CMakeFiles/my_program.dir/importable.cxx.o -c /home/loves/test/importable.cxx",
  "file": "/home/loves/test/importable.cxx",
  "output": "CMakeFiles/my_program.dir/importable.cxx.o"
}
]

And apparently there’s nothing about modules.

No, There is no known mechanism in Xcode to express build-time discovered dependencies. It requires xcodebuild features that cannot be emulated.

This is issue 24618.

1 Like

Thanks. I will keep an eye on this issue.