Search code examples
c++cmakeheader-filesbuildconfiguration

Should I really specify header files as target_sources()?


In his Meeting C++ 2019 talk (and also in 2018), presenter Deniz Bahadir recommends that we specify targets' C++ header files using target_sources(), with some of them being PUBLIC or INTERFACE.

But - when I try to do that (using relative paths, like I used to with the good old add_target()), I get this kind of error:

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"

So, what, am I supposed to put the header file names in generator expressions or something? I certainly don't want absolute paths to my source dir ending up in the library's interface. This seems weird to me. Maybe I just should bother with the header files in target_sources() after all?

PS - Using CMake 3.13.4 on Devuan 3.


Solution

  • This error goes away if CMakeLists.txt specifies version above 3.13.0:

    cmake_minimum_required(VERSION 3.13.0)
    

    From official docs:

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