我正在尝试使用 C++ 20 模块与 CMake 进行测试。这是我的项目:
main.cc
import mod;
int main() {
func();
return 0;
}
mod.cc
#include <iostream>
module mod;
export void func() {
std::cout << "Function in module `mod`" << '\n';
}
CMakeLists.cc
cmake_minimum_required(VERSION 3.4)
set(CMAKE_CXX_STANDARD 20)
project(module_test)
add_executable(mod_test main.cc)
target_sources(mod_test
PUBLIC
FILE_SET cxx_modules TYPE CXX_MODULES
FILES mod.cc
)
当我尝试构建时,我收到此消息
-- Selecting Windows SDK version 10.0.22000.0 to target Windows 10.0.19045.
CMake Warning (dev) at CMakeLists.txt:8 (target_sources):
Policy CMP0076 is not set: target_sources() command converts relative paths
to absolute. Run "cmake --help-policy CMP0076" for policy details. Use
the cmake_policy command to set the policy and suppress this warning.
An interface source of target "mod_test" has a relative path.
This warning is for project developers. Use -Wno-dev to suppress it.
-- Configuring done
CMake Error at CMakeLists.txt:6 (add_executable):
Cannot find source file:
FILE_SET
Tried extensions .c .C .c++ .cc .cpp .cxx .cu .mpp .m .M .mm .ixx .cppm .h
.hh .h++ .hm .hpp .hxx .in .txx .f .F .for .f77 .f90 .f95 .f03 .hip .ispc
-- Generating done
CMake Generate step failed. Build files cannot be regenerated correctly.
出于某种原因,它似乎将
FILE_SET
读取为文件名。
我发现我犯了两个错误:
在 mod.cc 中应该说
export module mod;
我将 CMakeLists.txt 更改为
cmake_minimum_required(VERSION 3.29)