我有一个项目,其目录布局如下:
- 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")
但这似乎无关紧要。该目录始终以-I
的形式传递给../ext
。
为什么这很重要?在构建结束时,我调用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
Edit2:
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
答案 0 :(得分:1)
该示例显示了可以视为源代码内部版本的内容。那就是当构建目录与src文件夹相同或位于其子目录时(不是硬定义或任何东西,但这确实触发了在命令行上使用相对路径的忍者问题)。尝试mkdir ~/cmake_build && cd ~/cmake_build && cmake ~/cmake_test
,然后对所有内容都使用绝对路径。
这两种方法实际上都没有强制一种方法。通常,cmake生成器将对命令行中最终使用的所有内容使用绝对路径。 Ninja似乎存在一些问题,导致生成器无法使用绝对路径进行源内构建(https://github.com/ninja-build/ninja/issues/1251)。