强制 CMake 使用绝对包含路径

Force CMake to use absolute include path

我有一个目录布局如下的项目:

- src/ #Contains main source code
- ext/ #Contains external libraries and headers from GitHub
- CMakeLists.txt

问题是无论我做什么,CMake 似乎总是将 ext/ 作为相对路径传递给编译器,如下所示:

/usr/bin/c++ -I../ext mysrc.cpp

我都尝试过:

include_directories("${PROJECT_SOURCE_DIR}/ext")
include_directories("/home/user/project/ext")

不过好像也没什么关系。该目录始终作为 ../ext.

传递给 -I

为什么这很重要?在我的构建结束时,我调用 gcov -r <source file> 告诉 gcov 从我的源文件和在其中找到的任何相关路径生成覆盖率报告。因此,gcov 将进入 ext/ 并为大量我不关心的东西生成报告,这会占用大量时间。如果 CMake 改为传入 -I/home/user/project/ext,那么 gcov -r 将忽略 ext/.

中的所有内容

据我所知: https://cmake.org/cmake/help/v3.13/command/include_directories.html ...这是不可能的,但也许我只是遗漏了什么?

编辑:这似乎是 ninja 生成器的问题。使用 Unix Makefiles 生成器时,所有内容 都通过绝对路径传递。

https://gitlab.kitware.com/cmake/cmake/issues/18666

编辑2:

user@antimony:~/cmake_test$ ls
CMakeLists.txt  ext  src
user@antimony:~/cmake_test$ cat CMakeLists.txt 
project(Hello)

add_subdirectory(src)
user@antimony:~/cmake_test$ cat src/CMakeLists.txt 
include_directories(
    .
    ${PROJECT_SOURCE_DIR}/ext
)

add_executable(hello_world hello.cpp)
user@antimony:~/cmake_test$ cat src/hello.cpp 
#include <useless.h>

int main()
{
    hello h;
    return 0;
}
user@antimony:~/cmake_test$ cat ext/useless.h 
struct hello {
    int x;
};
user@antimony:~/cmake_test$ ~/Downloads/cmake-3.13.1-Linux-x86_64/bin/cmake --version
cmake version 3.13.1

CMake suite maintained and supported by Kitware (kitware.com/cmake).
user@antimony:~/cmake_test$ mkdir build && cd build
user@antimony:~/cmake_test/build$ ~/Downloads/cmake-3.13.1-Linux-x86_64/bin/cmake .. -G Ninja
-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
...
-- Build files have been written to: /home/user/cmake_test/build
user@antimony:~/cmake_test/build$ ninja -v
[1/2] /usr/bin/c++   -I../src/. -I../ext  -MD -MT src/CMakeFiles/hello_world.dir/hello.o -MF src/CMakeFiles/hello_world.dir/hello.o.d -o src/CMakeFiles/hello_world.dir/hello.o -c ../src/hello.cpp
[2/2] : && /usr/bin/c++    -rdynamic src/CMakeFiles/hello_world.dir/hello.o  -o src/hello_world   && :
user@antimony:~/cmake_test/build$ cat build.ninja
# CMAKE generated file: DO NOT EDIT!
# Generated by "Ninja" Generator, CMake Version 3.13

# This file contains all the build statements describing the
# compilation DAG.

...

#############################################
# Order-only phony target for hello_world

build cmake_object_order_depends_target_hello_world: phony || src/CMakeFiles/hello_world.dir
build src/CMakeFiles/hello_world.dir/hello.o: CXX_COMPILER__hello_world ../src/hello.cpp || cmake_object_order_depends_target_hello_world
  DEP_FILE = src/CMakeFiles/hello_world.dir/hello.o.d
  INCLUDES = -I../src/. -I../ext
  OBJECT_DIR = src/CMakeFiles/hello_world.dir
  OBJECT_FILE_DIR = src/CMakeFiles/hello_world.dir
  TARGET_COMPILE_PDB = src/CMakeFiles/hello_world.dir/
  TARGET_PDB = src/hello_world.pdb

# =============================================================================
# Link build statements for EXECUTABLE target hello_world

该示例显示了可被视为源内构建的内容。那是当构建目录与 src 文件夹相同或子目录时(并不是说有硬定义或任何东西,但这确实会触发在命令行上使用相对路径的忍者问题)。尝试 mkdir ~/cmake_build && cd ~/cmake_build && cmake ~/cmake_test 然后它应该对所有内容使用绝对路径。

无论哪种方式,确实没有一种特定的方式来强制其中一种方式。一般来说,cmake 生成器将对最终在命令行上使用的所有内容使用绝对路径。 Ninja 似乎存在问题,阻止生成器使用绝对路径进行源内构建 (https://github.com/ninja-build/ninja/issues/1251)。