为什么 Visual Studio 将我的 CPP 和 Header 文件标记为杂项文件,导致它们不受智能感知的影响

Why does Visual Studio mark my CPP and Header files as Miscellaneous Files, leading them to not be affected by intelli sense

问题

您可能已经阅读了长标题:我的 VS 2019 一直将我的 header 和 CPP 文件标记为“杂项文件”,导致它们不受智能感知的影响。我有这个问题已经有一段时间了,所以我真的很感激帮助。我也许应该补充一点,我正在使用 CMake,这确实可能是问题的根源。因此,为此,我只提供 CMake 文件,您可能会编写错误的代码并自行检查(:

这是根 CMake 文件:

# CMakeList.txt: CMake-Projektdatei der obersten Ebene. Führen Sie hier die globale Konfiguration aus,
# und schließen Sie Unterprojekte ein.
#
cmake_minimum_required (VERSION 3.8)

project ("FirstCPPEngine")

# Schließen Sie Unterprojekte ein.
add_subdirectory ("FirstCPPEngine")


这是 FirstCPPEngine 中的 CMake 文件:

# CMakeList.txt: CMake-Projekt für "FirstCPPEngine". Schließen Sie die Quelle ein, und definieren Sie
# projektspezifische Logik hier.
#
cmake_minimum_required (VERSION 3.8)

# Fügen Sie der ausführbaren Datei dieses Projekts eine Quelle hinzu.
# "src/ResourceManagement/ResourceLocation.h" "src/ResourceManagement/ResourceLocation.cpp" 
add_executable (FirstCPPEngine "src/Main.cpp" "src/subfolder/insertyourwrongcodehere.h" "src/subfolder/insertyourwrongcodehere.cpp" )


target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17)


如果我忘记了任何文件,请在评论中告诉我。我希望你能帮助我。

C++ 传统(非模块)编译器将 .cpp 个文件编译成 .o 个文件,然后链接器获取 .o 个文件并将它们链接成二进制文件。这些文件的确切扩展名不固定(您可以说服编译器使用 .foo 个源文件并生成 .bob 个目标文件)。

您可能会注意到 .h 个文件未在此处提及。

编译源文件时,C/C++ 预处理器使用头文件。它们是按文本包含的——当 #include 出现时,它们的内容按字面意思复制并粘贴到编译器版本的 thr 源文件中。

未包含的头文件是文本文件。更重要的是,从 cpp 文件中单独编译该测试文件可能根本无效。

在其他不使用文本包含模型的语言中,它们的接口声明文件可以并且正在“独立”编译。在 C++ 中,编译器这样做是在编造一些实际上并没有发生的事情。

一个体面的做法是制作存根 cpp 文件,除了包含每个头文件外什么都不做。这确保您的每个头文件依次包含其依赖项,并且应该可以解决您的智能感知问题。