这是我知识中的一个烦人的漏洞,我似乎找不到答案。
我理解为什么它在编译期间起作用( 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
以便自动完成,而不是将其显示为未定义等?
我问这个问题,部分是出于好奇,部分是因为如果我不知道这个机制,那么我就不知道我是否有破坏它的风险,也不知道当我这样做时如何修复它。我已经看到它多次中断,导致大部分有问题的 header 被标记为错误,然后在某个时候它要么自行修复,要么我接受它不会回来并添加一些额外的战略包含在标题中。
再重复一遍:我不是在问在编译期间如何找到 std::string,我是在问在编辑头文件时如何找到它。
最佳答案
How does VS Intellisense find definitions from stdafx.h while editing a header file?
在不同版本的 VS(VS2012~VS2019)中进行了一些测试后,我认为您所体验到的是 VS intellisense 的预期行为。
回答您原来的问题 How does VS Intellisense find...
:
在 MyFunction.h
中编辑时,我认为 Intellisense 在 stdafx.h
中找到定义通过读取 #include statements
中的数据或信息( MyFunction.cpp
)文件。
也就是说:当我有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 中的行为相同)
希望它能帮助解决你的难题,如果我有任何误解,请随时纠正我:)
关于c++ - VS Intellisense 如何在编辑头文件时从 stdafx.h 中找到定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58695370/