C++Modules: Cannot import private module from same target
I ran into this problem while trying to convert my library to modules. Here is a small repro:
$ cat CMakeLists.txt
cmake_minimum_required(VERSION 3.26)
project(std_module_example CXX)
set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP 1)
set(CMAKE_EXPERIMENTAL_CXX_MODULE_CMAKE_API "2182bf5c-ef0d-489a-91da-49dbc3090d2a")
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD 20)
add_library(lib)
target_sources(lib
PUBLIC FILE_SET pub TYPE CXX_MODULES FILES lib/lib.cpp
PRIVATE FILE_SET priv TYPE CXX_MODULES FILES lib/priv.cpp
)
add_executable(exe exe/main.cpp)
target_link_libraries(exe PRIVATE lib)
$ cat lib/lib.cpp
export module lib;
import lib.priv; // comment this line to build successfully
$ cat lib/priv.cpp
export module lib.priv;
$ cat exe/main.cpp
import lib;
import lib.priv;
int main(int argc, char const *argv[]) {
return 0;
}
$ cmake.exe --version
cmake version 3.26.1
CMake suite maintained and supported by Kitware (kitware.com/cmake).
$ cmake.exe -S . -B build -G Ninja
CMake Warning (dev) at CMakeLists.txt:10 (target_sources):
CMake's C++ module support is experimental. It is meant only for
experimentation and feedback to CMake developers.
This warning is for project developers. Use -Wno-dev to suppress it.
-- Configuring done (0.2s)
CMake Warning (dev):
C++20 modules support via CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP is
experimental. It is meant only for compiler developers to try.
This warning is for project developers. Use -Wno-dev to suppress it.
-- Generating done (0.2s)
-- Build files have been written to: C:/Users/IVAN/Desktop/c++modules/test/build
$ cmake.exe --build build
[2/5] Generating CXX dyndep file CMakeFiles\lib.dir\CXX.dd
FAILED: CMakeFiles/lib.dir/CXX.dd
C:\Dev\CMake\bin\cmake.exe -E cmake_ninja_dyndep --tdi=CMakeFiles\lib.dir\CXXDependInfo.json --lang=CXX --modmapfmt=msvc --dd=CMak
eFiles\lib.dir\CXX.dd @CMakeFiles\lib.dir\CXX.dd.rsp
CMake Error: Public C++ module source `C:/Users/IVAN/Desktop/c++modules/test/lib/lib.cpp` requires the `lib.priv` C++ module which
is provided by a private source
ninja: build stopped: subcommand failed.
Is this working correctly?
I was expecting this error not when building lib.cpp
(which belong to the same target as priv.cpp
), but when building main.cpp
(which is a different target).
If I comment the line import lib.priv;
in lib.cpp
, then CMake builds the project without any error. But I think that's wrong too, the exe
target should not have access to private modules from lib
.
I've tested with both MSVC and Clang.