编辑 header 文件时,VS Intellisense 如何从 stdafx.h 中查找定义?

How does VS Intellisense find definitions from stdafx.h while editing a header file?

这是我知识中的一个恼人漏洞,我似乎找不到答案。

我明白为什么它在编译期间工作(header 从来没有单独编译过,所以 stdafx.h 已经包含在内),但不明白为什么它在编辑时工作。

假设我有以下文件:

stdafx.h

#pragma once

#include <string>

MyFunction.h

#pragma once

void MyFunction( std::string Text );

MyFunction.cpp

#include "stdafx.h"
#include "MyFunction.h"

void MyFunction( std::string Text )
{
    // implementation here
}

当我编辑 MyFunction.h 时,Intellisense 如何找到 std::string 以达到 auto-complete 的目的,而不是将其显示为未定义等?

我之所以问,部分是出于好奇,部分是因为如果我不知道这个机制,那么我就不知道我是否有破坏它的风险,也不知道当我这样做时如何修复它。我已经看到它多次中断,导致大部分 header 有问题被标记为错误,然后在某个时候它要么自行修复,要么我接受它不会回来并添加header.

中包含一些额外的战略内容

重复一遍:我不是在问如何在编译期间找到 std::string,我是在问在编辑 header 文件时如何找到它。

How does VS Intellisense find definitions from stdafx.h while editing a header file?

在不同版本的 VS(VS2012~VS2019) 中进行了一些测试后,我认为您遇到的是 VS 智能感知的预期行为。

回答你原来的问题How does VS Intellisense find...:

MyFunction.h 中编辑时,我认为 Intellisense 通过读取 MyFunction.cpp 文件中的数据或信息 (#include statements) 来找到 stdafx.h 中的定义。

也就是说:当我在当前项目中有a.h, b.h and b.cpp时,如果b.cpp有语句#include "a.h"#include "b.h",那么Intellisense会搜索这三个中的定义文件。因此,即使您将 #include <string>stdafx.h 移动到 MyFunction.cpp 文件,MyFunction.h 也可以识别定义。

所以如果一个test.h被一个test.cpp文件引用(包含),并且这个test.cpp文件也使用#include引用(包含)test2.h, test3.h...编辑test.h时,Intellisense会自动搜索test.cpp, test2.h, test3.h...中的定义在我看来,就是这样VS Intellisense 在这种情况下如何工作。 (我机器上的 VS2012、VS2015、VS2017、VS2019 中的行为相同)

希望它能帮助解决你的困惑,如果我有任何误解,请随时纠正我:)