我真的应该将头文件指定为 target_sources() 吗?

Should I really specify header files as target_sources()?

在他的 Meeting C++ 2019 talk(以及 2018 年)中,主持人 Deniz Bahadir 建议我们使用 target_sources() 指定目标的 C++ 头文件,其中一些是 PUBLIC 或 INTERFACE .

但是 - 当我尝试这样做时(使用相对路径,就像我以前使用旧 add_target() 时那样),我得到了这种错误:

CMake Warning (dev) at CMakeLists.txt:26 (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 "cuda-api-wrappers" has a relative path.
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Configuring done
CMake Error in CMakeLists.txt:
  Target "cuda-api-wrappers" INTERFACE_SOURCES property contains relative
  path:

    "src/cuda/api/miscellany.hpp"

那么,我应该将头文件名放在生成器表达式中还是什么?我当然不希望我的源目录的绝对路径出现在库的界面中。这对我来说似乎很奇怪。也许我毕竟应该为 target_sources() 中的头文件烦恼?

PS - 在 Devuan 3 上使用 CMake 3.13.4。

如果 CMakeLists.txt 指定的版本高于 3.13.0:

,则此错误消失
cmake_minimum_required(VERSION 3.13.0)

来自官方文档:

  • here

    Relative source file paths are interpreted as being relative to the current source directory...

  • here

    In CMake 3.13 and above, the target_sources() command now converts relative source file paths to absolute paths...