CMake: `Cannot find source file: FILE_SET` with target_sources

56 views Asked by At

I'm trying to test out using C++ 20 modules with CMake. Here's my project:

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
)

When I attempt to build I get this message

-- 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.

It seems to be reading FILE_SET as a file name for some reason.

1

There are 1 answers

0
MBR-6161 On

I found two errors I made:

In mod.cc it should say

export module mod;

and I changed CMakeLists.txt to say

cmake_minimum_required(VERSION 3.29)