How can I build cppm in cmake environment?

The code is simple.

├── **CMakeLists.txt**
├── **Hello.cppm**
├── Hello.out
├── Hello.pcm
├── README.md
├── **Use.cpp**
└── **build**
  • Hello.cppm
module;
#include <iostream>
export module Hello;
export void hello() {
  std::cout << "Hello World!\n";
}
  • Use.cpp
// use.cpp
import Hello;
int main() {
  hello();
  return 0;
}
  • command line build is OK
clang++ -std=c++20 Hello.cppm --precompile -o Hello.pcm
clang++ -std=c++20 use.cpp -fprebuilt-module-path=. Hello.pcm -o Hello.out
./Hello.out 
Hello World!
  • However, if you build after creating CMakeLists.txt, the Hello module’s not found.

  • CMakeLists.txt

cmake_minimum_required(VERSION 3.10)

project(HelloProject)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)

set(CMAKE_CXX_COMPILER clang++)

set(COMMON_COMPILE_OPTIONS -std=c++20)

add_custom_command(
    COMMAND ${CMAKE_CXX_COMPILER} ${CMAKE_SOURCE_DIR}/Hello.cppm ${COMMON_COMPILE_OPTIONS} --precompile
    OUTPUT ${CMAKE_BINARY_DIR}/Hello.pcm
    COMMENT "Precompiling Hello.cppm to Hello.pcm"
)

add_custom_target(HelloModule DEPENDS ${CMAKE_BINARY_DIR}/Hello.pcm)

add_executable(UseApp Use.cpp)

add_dependencies(UseApp HelloModule)

target_compile_options(UseApp PRIVATE ${COMMON_COMPILE_OPTIONS} -fprebuilt-module-path=.)

  • result
Consolidate compiler generated dependencies of target UseApp
[ 66%] Building CXX object CMakeFiles/UseApp.dir/Use.cpp.o
clang: warning: argument unused during compilation: '-fprebuilt-module-path=.' [-Wunused-command-line-argument]
/mnt/d/workspace/modern-cpp/ProC++/cppm/Use.cpp:2:8: fatal error: module 'Hello' not found
import Hello;
~~~~~~~^~~~~
1 error generated.
make[2]: *** [CMakeFiles/UseApp.dir/build.make:76: CMakeFiles/UseApp.dir/Use.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:110: CMakeFiles/UseApp.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

If anyone has any advice on writing cmake regarding the cppm module, please respond.
Thank you.

See