gpt4 book ai didi

c++ - 当我使用 CMake 和 ninja 构建时,clang++ 会忽略 -MD 标志

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:39:33 25 4
gpt4 key购买 nike

我创建了一个非常简单的 C++/CMake 项目:

CMakeLists.txt(注意 -MD 标志):

cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
add_executable(moop main.cc)
target_compile_options(moop PRIVATE -MD)

主.cc:

#include "moop.hh"
int main( int, char** ) { return 0; }

moop.hh:

#pragma once

从项目根目录,我运行以下命令:

mkdir build && cd build
cmake -G Ninja -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..
cat compile_commands.json

cat compile_commands.json 输出(注意 -MD 标志):

{
"directory": "/home/zbardoo/moop/build",
"command": "/usr/bin/clang++ -MD -o CMakeFiles/moop.dir/main.cc.o -c /home/zbardoo/moop/main.cc",
"file": "/home/zbardoo/moop/main.cc"
}

如果我随后运行 ninja,可执行文件 moop 将成功构建。但是,找不到moop.cc.d。但是,如果我随后从 compile_commands.json 复制并粘贴 command 值并运行它:

/usr/bin/clang++      -MD -o CMakeFiles/moop.dir/main.cc.o -c /home/zbardoo/moop/main.cc

文件/home/zbardoo/moop/build/CMakeFiles/moop.dir/main.cc.d出现:

zbardoo@localhost:~/moop/build$ cat CMakeFiles/moop.dir/main.cc.d 
CMakeFiles/moop.dir/main.cc.o: /home/zbardoo/moop/main.cc \
/home/zbardoo/moop/moop.hh

为什么 ninja 不遵守 compile_commands.json 文件中的 -MD 标志?

最佳答案

罪魁祸首不是 Clang,而是 ninga

可以找到缺少CMakeFiles/moop.dir/main.cc.d的解释如果您查看生成的 build/rules.ninja 并找到:

rule CXX_COMPILER__moop
depfile = $DEP_FILE
deps = gcc
command = /usr/bin/clang++ $DEFINES $INCLUDES $FLAGS -MD -MT $out -MF $DEP_FILE -o $out -c $in
description = Building CXX object $out

注意:

  deps = gcc

然后看the Ninja manual: C/C++ header dependencies -> deps :

deps(Available since Ninja 1.3.)

It turns out that for large projects (and particularly on Windows, where the file system is slow) loading these dependency files on startup is slow.

Ninja 1.3 can instead process dependencies just after they’re generated and save a compacted form of the same information in a Ninja-internal database.

Ninja supports this processing in two forms.

deps = gcc specifies that the tool outputs gcc-style dependencies in the form of Makefiles.Adding this to the above example will cause Ninja to process the depfile immediately after the compilation finishes,then delete the .d file (which is only used as a temporary).

(我的重点)。

关于c++ - 当我使用 CMake 和 ninja 构建时,clang++ 会忽略 -MD 标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54154524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com