强制CMake使用绝对包含路径
我有一个项目,其目录布局如下所示:强制CMake使用绝对包含路径,cmake,gcov,ninja,Cmake,Gcov,Ninja,我有一个项目,其目录布局如下所示: - 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_SO
- 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
,它告诉gcov从我的源文件和在其中找到的任何相关路径生成覆盖率报告。因此,gcov将进入ext/
,并为大量我不关心的内容生成报告,这将占用大量时间。如果CMake转而传入-I/home/user/project/ext
,那么gcov-r
将忽略ext/
中的所有内容
据我所知:
... 这是不可能的,但也许我只是错过了什么
编辑:这似乎是忍者
生成器的问题。使用Unix Makefiles
生成器时,所有内容都通过绝对路径传递
编辑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
user@antimony:~/cmake_测试$ls
CMakeLists.txt ext src
user@antimony:~/cmake_test$cat CMakeLists.txt
项目(你好)
添加子目录(src)
user@antimony:~/cmake_test$cat src/CMakeLists.txt
包含目录(
.
${PROJECT\u SOURCE\u DIR}/ext
)
添加可执行文件(hello\u world hello.cpp)
user@antimony:~/cmake_test$cat src/hello.cpp
#包括
int main()
{
你好,h;
返回0;
}
user@antimony:~/cmake_test$cat ext/无用.h
结构你好{
int x;
};
user@antimony:~/cmake_test$~/Downloads/cmake-3.13.1-Linux-x86_64/bin/cmake--version
cmake版本3.13.1
由Kitware(Kitware.com/CMake)维护和支持的CMake套件。
user@antimony:~/cmake_测试$mkdir生成和cd生成
user@antimony:~/cmake_测试/构建$~/Downloads/cmake-3.13.1-Linux-x86_64/bin/cmake-G忍者
--C编译器标识为GNU7.3.0
--CXX编译器标识为GNU 7.3.0
...
--生成文件已写入:/home/user/cmake\u test/Build
user@antimony:~/cmake_测试/构建$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/cmakfiles/hello_world.dir/hello.o-o src/hello_world&&:
user@antimony:~/cmake_测试/构建$cat build.ninja
#CMAKE生成的文件:不要编辑!
#由“忍者”生成器生成,CMake版本3.13
#此文件包含所有描述
#编译DAG。
...
#############################################
#只为hello_world订购虚假目标
构建cmake_对象_顺序_依赖_目标_hello_world:phony | | src/CMakeFiles/hello_world.dir
构建src/CMakeFiles/hello\u world.dir/hello.o:CXX\u COMPILER\uuuuuuuuuuu hello\uworld../src/hello.cpp | cmake\uobject\uorder\udependens\utarget\uhello\uworld
DEP_FILE=src/cmakfiles/hello_world.dir/hello.o.d
INCLUDES=-I../src/-I../ext
OBJECT_DIR=src/cmakfiles/hello_world.DIR
OBJECT\u FILE\u DIR=src/cmakfiles/hello\u world.DIR
TARGET\u COMPILE\u PDB=src/cmakfiles/hello\u world.dir/
TARGET_PDB=src/hello_world.PDB
# =============================================================================
#可执行目标hello_world的链接生成语句
该示例显示了可被视为源代码内构建的内容。也就是说,构建目录与src文件夹相同,或者是src文件夹的子目录(不是说有硬定义或任何东西,但这确实会触发在命令行上使用相对路径的忍者问题)。尝试mkdir~/cmake_build&&cd~/cmake_build&&cmake~/cmake_test
然后它应该为所有内容使用绝对路径
无论哪种方式,都没有一种特定的方式来强制一种或另一种。一般来说,cmake生成器将为最终在命令行上使用的所有内容使用绝对路径。Ninja似乎存在一些问题,阻止生成器为源代码内构建()使用绝对路径。该示例显示了可能被视为源代码内构建的内容。也就是说,构建目录与src文件夹相同,或者是src文件夹的子目录(不是说有硬定义或任何东西,但这确实会触发在命令行上使用相对路径的忍者问题)。尝试
mkdir~/cmake_build&&cd~/cmake_build&&cmake~/cmake_test
然后它应该为所有内容使用绝对路径
无论哪种方式,都没有一种特定的方式来强制一种或另一种。一般来说,cmake生成器将为最终在命令行上使用的所有内容使用绝对路径。Ninja似乎存在一些问题,阻止生成器为源代码内构建()使用绝对路径。如果CMake Ninja生成器中存在将绝对路径转换为相对路径的错误,您是否考虑过使用Makefile生成器作为临时解决方法。可能想报告您正在使用的cmake版本中仍然存在此错误。实际上,我无法使用cmake 3.13重现此问题<代码>构建。忍者总是为-I指令使用绝对路径。我还没有尝试过3.13,但如果是这样的话,我会遇到相反的问题!基本上,我希望src/中的路径是相对的,但ext中的路径是绝对的,这样gcov将忽略外部源文件,只计算相对源文件的覆盖率。通常,cmake在生成项目文件时会使所有路径都是绝对的。这就是为什么创建项目后,您不能移动东西的原因。例如,如果
build
和source
都是~/example
的子目录,并且您将example
重命名为actual
,则无法再使用build
中的项目。我无法使用3.13
进行复制。你有什么版本的忍者?如果是