共用方式為


為 IntelliSense 設定 C++ 專案

在某些情況下,您可能需要手動設定C++專案,讓IntelliSense正常運作。 For MSBuild projects (based on .vcxproj files), you can adjust settings in project properties. For non-MSBuild projects, you adjust settings in the CppProperties.json file in the root directory of the project. In some cases, you might need to create a hint file to help IntelliSense understand macro definitions. Visual Studio IDE 可協助您找出並修正 IntelliSense 問題。

Single-file IntelliSense

When you open a file that isn't included in a project, Visual Studio provides some IntelliSense support, but by default no error squiggles are shown. If the Navigation Bar says Miscellaneous Files, then that probably explains why you don't see error squiggles under incorrect code, or why a preprocessor macro isn't defined.

檢查錯誤清單

If a file isn't open in single-file mode, and IntelliSense isn't working correctly, the first place to check is the Error List window. To see all the IntelliSense errors for the current source file together with all included header files, choose Build + IntelliSense in the dropdown:

Screenshot of the Error List window. Build + IntelliSense is selected in the filter dropdown.

IntelliSense produces a maximum of 1,000 errors. If there are more than 1,000 errors in the header files included by a source file, then the source file shows only a single error squiggle at the very start of the source file.

確定 #include 路徑正確

MSBuild 專案

If you run your builds outside of the Visual Studio IDE, and your builds are succeeding but IntelliSense is incorrect, it's possible that your command line is out of sync with the project settings for one or more configurations. 以滑鼠右鍵按兩下 [方案總管] 中的項目節點,並確定目前組態和平臺的所有 #include 路徑都正確。 如果路徑在所有組態和平臺中都相同,您可以選取 [所有組態] ,然後 [所有平臺],然後確認路徑正確無誤。

Screenshot that shows the Include Directories setting in the Configuration Properties section.

To see the current values for build macros such as VC_IncludePath, select the Include Directories dropdown. Then choose <Edit> and select the Macros button.

Makefile 專案

For Makefile projects that are based on the NMake project template, choose NMake under Configuration Properties, and then choose Include search path in the IntelliSense category:

Screenshot that shows the Configuration Properties > N Make > Include Search Path setting.

CMake projects

For CMake projects, make sure that #include paths are specified correctly for all configurations in CMakeLists.txt. Other project types might require a CppProperties.json file. For more information, see Configure code navigation with CppProperties.json. 請確定檔案中所定義之每個組態的路徑都正確無誤。

If there's a syntax error in the CppProperties.json file, IntelliSense in the affected files is incorrect. Visual Studio displays the error in the Output Window.

Tag parser issues

The tag parser is a fuzzy C++ parser that's used for browsing and navigation. It's fast but doesn't attempt to completely comprehend every code construct.

For example, it doesn't evaluate preprocessor macros, and therefore it might incorrectly parse code that makes heavy use of them. When the Tag Parser encounters an unfamiliar code construct, it might skip that entire region of code.

在Visual Studio中,有兩種常見的方式會說明此問題:

  1. The IDE offers to create a function definition for a function that is already defined.

  2. If the navigation bar shows an innermost macro, then the current function definition was skipped:

    Screenshot that shows the tag parser skipping the function definition.

    The code snippet shows a macro definition for do_if that is used inside of the function main. The tag parser doesn't understand the macro, so instead of the navigation dropdown showing that the name of the current function is main, it shows the name of the macro: do_if.

To fix these kinds of problems, add a file named cpp.hint to the root of your solution directory. 如需詳細資訊,請參閱 提示檔案

標籤剖析器錯誤會出現在 錯誤清單 視窗中。

使用診斷記錄來驗證項目設定

To check whether the IntelliSense compiler is using correct compiler options, including Include Paths and Preprocessor macros, turn on Diagnostic Logging of IntelliSense command lines in Tools > Options > Text Editor > C/C++ > Advanced > Diagnostic Logging. Set Enable Logging to True, Logging Level to 5 (most verbose), and Logging Filter to 8 (IntelliSense logging).

The Output Window now shows the command lines that are passed to the IntelliSense compiler. 以下是範例輸出:

[IntelliSense] Configuration Name: Debug|Win32
[IntelliSense] Toolset IntelliSense Identifier:
[IntelliSense] command line options:
/c
/I.
/IC:\Repo\Includes
/DWIN32
/DDEBUG
/D_DEBUG
/Zc:wchar_t-
/Zc:forScope
/Yustdafx.h

This information might help you understand why IntelliSense is providing inaccurate information. For example, if your project's Include directory contains $(MyVariable)\Include, and the diagnostic log shows /I\Include as an Include path, it means that $(MyVariable) wasn't evaluated, and was removed from the final include path.

關於 IntelliSense 建置

Visual Studio 會使用專用C++編譯程式來建立和維護支援所有 IntelliSense 功能的資料庫。 為了讓 IntelliSense 資料庫與程式代碼保持同步,Visual Studio 會自動啟動僅限 IntelliSense 的組建作為背景工作,以響應專案設定或來源檔案中所做的特定變更。

不過,在某些情況下,Visual Studio 可能不會及時更新 IntelliSense 資料庫。 For example, when you run a git pull or git checkout command, Visual Studio might take up to an hour to detect changes in the files. 您可以在方案總管 中,以滑鼠右鍵按兩下專案 節點,然後選擇 [重新掃描方案],以強制重新掃描方案中的所有檔案。

Troubleshoot IntelliSense build failures

An IntelliSense build doesn't produce binaries, but it can still fail. One possible cause for failure is custom .props or .targets files. 在 Visual Studio 2017 15.6 版及更高版本中,僅限 IntelliSense 的建置錯誤會被記錄到 [輸出] 視窗。 To see them, set Show output from to Solution:

Screenshot that shows the Output window. The Show output from dropdown is set to Solution.

錯誤訊息可能會指示您啟用設計時間追蹤:

error: Designtime build failed for project 'E:\src\MyProject\MyProject.vcxproj',
configuration 'Debug|x64'. IntelliSense might be unavailable.
Set environment variable TRACEDESIGNTIME=true and restart
Visual Studio to investigate.

If you set the environment variable TRACEDESIGNTIME to true and restart Visual Studio, you see a log file in the %TEMP% directory, which might help diagnose the build failure.

To learn more about TRACEDESIGNTIME environment variable, see Roslyn and Design-time builds. 這些文章中的資訊與C++項目有關。