VSCode - IntelliSense

Using C++ on Linux in VS Code


Configure C/C++ IntelliSense

Configure C/C++ IntelliSense

This article is about configuring the C/C++ extension to provide C++ specific IntelliSense suggestions in Visual Studio Code. IntelliSense is a helpful tool built into VS Code that provides various code editing features to help you code faster and more efficiently. For example, code completion, parameter info, syntax highlighting, Code Actions (light bulbs), and member lists are all generated using IntelliSense.

C/C++ IntelliSense only requires a C/C++ compiler to be installed on your system. The C/C++ compiler provides C++ specific information to IntelliSense, such as the locations of system include paths and other settings. For project level configurations, reference Project level IntelliSense configuration section.

When will the C/C++ extension configure core IntelliSense features for me?

A compiler is the only requirement to configure core IntelliSense functionality. To identify a compiler for IntelliSense, the C/C++ extension scans common paths on your machine for compilers such as Clang, GCC, MinGW, cygwin, cygwin64, and MSVC. If any of these compilers are identified and in a secure location, they are automatically configured for IntelliSense. Otherwise, a notification is displayed asking you to confirm that this compiler should be configured for IntelliSense. In either of these scenarios, the compiler selected is also set as the default compiler.

How to check whether IntelliSense is configured

If you don't have IntelliSense configured, the C/C++ extension shows a yellow indicator in the status bar with a warning sign labeled Configure IntelliSense.

To configure, select the status bar indicator, which brings up the configuration Quick Pick. The Quick Pick can help you select or install a C/C++ compiler.

If you do not see a status bar indicator, you can also check your project's c_cpp_properties.json file. This file stores all of your IntelliSense configuration settings. Navigate to this file by selecting C/C++: Edit Configurations (UI) from the Command Palette (Ctrl+Shift+P). Check the IntelliSense mode to find your configuration. To learn more about the c_cpp_properties.json file, you can review the schema reference.

How to configure IntelliSense

IntelliSense configuration is stored in the c_cpp_properties.json file, which is automatically created in your workspace. All three of the following options are different ways of editing the c_cpp_properties.json file:

Option 1. Select a configuration option through the configuration Quick Pick

Open the Quick Pick by entering Select IntelliSense Configuration in the Command Palette (Ctrl+Shift+P), which shows you a dropdown with all of the configuration options found by the C/C++ extension on your machine.

Select one of the options available. If you select a compiler, this compiler is used by IntelliSense by default. You can return to the Configure IntelliSense Quick Pick at any point to change which option is used to configure IntelliSense.

If no options are available in the Quick Pick, no compiler could be identified in your system. You can browse your machine manually or install a C/C++ compiler. To install on a Windows machine, select the Help me install a compiler option that redirects you to the step-by-step walkthrough of how to install a C/C++ compiler. On a macOS or Linux machine, select Install a compiler and navigate through the prompts to have a C++ compiler installed on your machine.

Option 2. Edit your IntelliSense configurations through the UI

Open your IntelliSense configuration by selecting C/C++: Edit Configurations (UI) from the Command Palette (Ctrl+Shift+P). This view is a user interface for the c_cpp_properties.json file.

Set the Compiler path field to the full path of the compiler you're using to build your project. For example, when using the default install path for GCC on Linux, the compiler path is /usr/bin/gcc. Set the IntelliSense mode to the architecture-specific variant of the compiler you're using.

Option 3. Edit the c_cpp_properties.json file directly

You can edit the c_cpp_properties.json file directly to customize your configuration. Use the C/C++ Edit Configurations (JSON) command from the Command Palette (Ctrl+Shift+P), then the c_cpp_properties.json file is created in the .vscode folder of your workspace.

Use the compilerPath variable to add a compiler. This variable is the full path of the compiler you're using to build your project. For example, when using the default install path for GCC on Linux, the compiler path is /usr/bin/gcc.

For more information about the c_cpp_properties.json file, review the schema reference.

Select the sample below based on your operating system for a c_cpp_configuration.json file:

Sample c_cpp_configuration.json on Windows

Using the default install path for MinGW:

{

  "configurations": [

    {

      "name": "Win32",

      "includePath": ["${workspaceFolder}/**"],

      "defines": ["_DEBUG", "UNICODE", "_UNICODE"],

      "windowsSdkVersion": "10.0.22621.0",

      "cStandard": "c17",

      "cppStandard": "c++17",

      "intelliSenseMode": "${default}",

      "compilerPath": "C:/msys64/mingw64/bin/gcc.exe"

    }

  ],

  "version": 4

}

Sample c_cpp_configuration.json on macOS

Using the default install path for Clang:

{

  "configurations": [

    {

      "name": "Mac",

      "includePath": ["${workspaceFolder}/**"],

      "defines": [],

      "macFrameworkPath": [

        "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"

      ],

      "compilerPath": "/usr/bin/clang",

      "cStandard": "c17",

      "cppStandard": "c++17",

      "intelliSenseMode": "macos-clang-arm64"

    }

  ],

  "version": 4

}

Sample c_cpp_configuration.json on Linux

Using the default install path for GCC:

{

  "configurations": [

    {

      "name": "Linux-GCC",

      "includePath": ["${workspaceFolder}/**"],

      "defines": [],

      "compilerPath": "/usr/bin/g++",

      "cStandard": "c17",

      "cppStandard": "c++17",

      "intelliSenseMode": "gcc-x64",

      "browse": {

        "path": ["${workspaceFolder}"],

        "limitSymbolsToIncludedHeaders": true,

        "databaseFilename": ""

      }

    }

  ],

  "version": 4

}

Project level IntelliSense configuration

Configuring IntelliSense with a compiler provides you with core IntelliSense features. This setup is called the base configuration. For more complex usage scenarios, such as setting up a project that requires:

* Additional include paths, such as references to one or multiple different libraries

* Specific compiler arguments that influence the behavior of the language(and therefore IntelliSense)

There are multiple other ways to configure IntelliSense. You can provide these additional configurations either through:

* The c_cpp_properties.json file and related settings

* A custom configuration provider in the form of another VS Code extension (for example, the Makefile Tools or CMake Tools extensions)

* A compile_commands.json file

Configuration providers

A custom configuration provider is another extension in VS Code that can potentially provide more accurate C++ IntelliSense configuration than the C/C++ extension. For example, for the CMake or Make build systems, where the Makefile Tools or CMake Tools extensions can be configuration providers. To add an extension as a configuration provider, either select the extension through the configuration Quick Pick, add it to configuration UI by editing the Configuration provider field under Advanced Settings, or add the configurationProvider field to your c_cpp_properties.json file. For example, for the CMake extension, the path to add would be ms-vscode.cmake-tools.

The C/C++ extension scans your system for custom configuration providers. If it identifies only one custom configuration provider, this configuration provider is automatically configured for IntelliSense. If there are multiple configuration providers identified, you need to select which the extension should use by opening the configuration Quick Pick.

compile_commands.json file

Another option to provide IntelliSense configuration is a compile_commands.json file, which describes the exact compile commands used for every file in a project. This file is often generated by a build system, such as CMake or Bazel, by setting command line arguments when configuring your project. A compile_commands.json file can be selected for configuration through the same methods as discussed in the How to configure IntelliSense section via the configuration Quick Pick, editing configurations through the UI, or editing the c_cpp_properties.json file directly. In the configuration UI, the file can be added under Advanced Configurations and the Compile commands field. For example, if your compile_commands.json file is in the root of your workspace, enter ${workspaceFolder}/compile_commands.json in the Compile commands field. Otherwise, it can be added to the c_cpp_properties.json file directly using the compileCommands configuration property.

If the compile commands database does not contain an entry for the translation unit that corresponds to the file you opened in the editor, your base configuration (found in c_cpp_properties.json) is used instead (such as your includePath and defines). If the C/C++ extension reverts to the base configuration, the language status bar indicator shows you the label Configure IntelliSense in the status bar.

If you specified a custom configuration provider and a compile_commands.json file, the custom configuration provider is queried first for an IntelliSense configuration.

If your program includes header files that aren't in your workspace or that aren't in the standard library path, you can modify the Include Path. The C/C++ extension populates the include path by querying the compiler specified by Compiler path. If the extension can't find the path for the target system libraries, you can enter the include path manually.

Check IntelliSense activity using the language status bar

You can determine if IntelliSense is actively working on your file using the language status bar. To invoke the language status bar, open a C++ file. The status bar shows the text {} C++. Hover over the {} symbol to open the language status bar flyout. The top item in the flyout indicates the IntelliSense status. Here are the different statuses and their meanings:

* IntelliSense: Ready = IntelliSense is configured for the C/C++ extension and automatically activates if you interact with the editor, for example, by writing code.

* IntelliSense: Updating = IntelliSense is actively working to determine any code completions, syntax highlighting, etc. based on changes you're making to your code.

You can select the pin icon on the right of any item in the language status bar flyout to permanently pin it to your status bar.


IntelliSense for cross-compiling

IntelliSense for cross-compiling

These configuration settings are stored in your project's c_cpp_properties.json file. To edit this file, in VS Code, select C/C++: Edit Configurations (UI) from the Command Palette (Ctrl+Shift+P):

Example IntelliSense configuration

The following shows configuring the C/C++ extension for a Linux x64 host machine that targets Linux Arm. It configures the following IntelliSense settings:

* Compiler path: the extension queries your compiler at this location to retrieve system libraries and compiler defines.

* IntelliSense mode: emulates the target architecture and compiler so that the extension can provide correct IntelliSense and reflect the right sizes of data types such as pointer, size_t, long, and so on.

At a minimum, setting compiler path and IntelliSense mode provides enough information for the extension to emulate your project's target architecture, although setting IntelliSense mode may not be needed if the extension is able to choose it correctly based on the defines returned from querying the compiler path.

Compiler path

Set to the full path of the compiler you are using to build your project.

For example:

IntelliSense mode

Set to the architecture-specific variant of the compiler you are using.

For example:

Include path

You only need to modify the Include path if your program includes header files that aren't in your workspace or that are not in the standard library path.

The C/C++ extension populates the include path by querying the compiler specified by Compiler path. If the extension can't find the path for the target system libraries, you can enter the include path manually:

Given the settings above, your c_cpp_configuration.json file will look something like the following. You can open it by selecting C/C++: Edit Configurations (JSON) from the Command Palette:

{

  "configurations": [

    {

      "name": "myConfigurationName",

      "includePath": ["${workspaceFolder}/**"],

      "defines": [],

      "compilerPath": "/usr/bin/arm-none-eabi-g++",

      "cStandard": "c11",

      "cppStandard": "c++14",

      "IntelliSenseMode": "gcc-arm"

    }

  ],

  "version": 4

}

<think>我们之前已经提供了详细的解决方案,但用户现在遇到了具体的问题:在WSL中运行`find ~/.vscode-server/extensions/ms-vscode.cpptools-* -name "cpptools" | xargs ldd`时,提示找不到文件或目录。用户希望解决这个问题,以便继续调试或运行相关工具。 问题分析: 1. 错误信息显示:`find: ‘/home/wb_wsl/.vscode-server/extensions/ms-vscode.cpptools-*’: No such file or directory` 这表明在`~/.vscode-server/extensions/`目录下没有以`ms-vscode.cpptools-`开名的目录,即C++扩展没有安装或者安装失败。 2. 后续的`ldd`命令也因没有输入文件而失败。 因此,我们需要确保C++扩展正确安装,并且找到`cpptools`文件,然后才能用`ldd`检查其依赖。 解决方案: 步骤1:确认扩展是否安装 在WSL终端中运行: ```bash ls ~/.vscode-server/extensions/ms-vscode.cpptools-* ``` 如果输出“No such file or directory”,则说明扩展未安装。 步骤2:安装扩展 由于是在WSL中,我们可以通过VS Code的扩展面板安装,但也可以使用命令行安装。这里提供命令行方法: 方法A:使用VS Code的Marketplace在线安装(需要网络) ```bash code --install-extension ms-vscode.cpptools ``` 方法B:如果在线安装失败,可以手动下载并安装(推荐) - 前往发布页面:https://github.com/microsoft/vscode-cpptools/releases - 下载最新版本的`cpptools-linux-x64.vsix`(根据你的架构选择,WSL一般是x64) - 将下载的vsix文件放到WSL中(例如放在用户主目录) - 安装: ```bash code --install-extension ~/cpptools-linux-x64.vsix ``` 步骤3:验证安装 安装完成后,再次检查目录: ```bash ls ~/.vscode-server/extensions/ms-vscode.cpptools-* ``` 应该能看到一个以版本号结尾的目录。 步骤4:查找cpptools文件 进入该目录,并查找`cpptools`文件(实际上,在C++扩展中,主要的可执行文件在`bin`目录下,例如`Microsoft.VSCode.CPP.Extension.exe`,但这是一个Windows可执行文件?不,在WSL中安装的扩展应该是Linux版本的。注意:在WSL中,扩展的二进制文件是Linux格式的,所以会有Linux的可执行文件或共享库)。 实际上,在Linux版本的C++扩展中,主要的二进制文件是: - `bin/Microsoft.VSCode.CPP.Extension` - `bin/Microsoft.VSCode.CPP.IntelliSense.Msvc` 因此,我们可以用以下命令查找: ```bash find ~/.vscode-server/extensions/ms-vscode.cpptools-* -name "Microsoft.VSCode.CPP.Extension" ``` 或者,如果你想检查所有可执行文件,可以: ```bash find ~/.vscode-server/extensions/ms-vscode.cpptools-* -type f -executable ``` 步骤5:运行ldd检查依赖 找到文件后,运行ldd,例如: ```bash ldd ~/.vscode-server/extensions/ms-vscode.cpptools-<version>/bin/Microsoft.VSCode.CPP.Extension ``` 如果发现缺少依赖,则安装相应的库(如libssl等)。 步骤6:如果安装扩展后仍然找不到文件? 可能是扩展安装不完整。我们可以尝试手动下载依赖包(从同一发布页面下载`cpptools-linux-dependencies.vsix`),然后解压到扩展目录。 例如: ```bash # 假设扩展版本是0.28.3 unzip cpptools-linux-dependencies.vsix -d ~/.vscode-server/extensions/ms-vscode.cpptools-0.28.3/ ``` 然后再次查找可执行文件。 步骤7:设置权限 如果权限有问题,可以运行: ```bash chmod -R +x ~/.vscode-server/extensions/ms-vscode.cpptools-*/bin ``` 步骤8:如果还是不行,考虑检查VS Code的日志。在VS Code中,打开命令面板(Ctrl+Shift+P),运行“Remote Development: Show Log”查看日志。 根据以上步骤,用户应该能够解决找不到文件的问题,并继续使用C++扩展。 注意:在WSL中,VS Code的扩展分为两部分:客户端(安装在Windows上)和服务器端(安装在WSL中)。我们这里操作的是WSL中的服务器端扩展。 引用说明: 在解决VS Code远程扩展问题时,收集正确的日志非常重要[^1]。 下面我们按照步骤详细说明: 详细步骤: 1. 安装扩展: 如果之前没有安装,请先安装。如果已经安装但可能损坏,先卸载再安装。 卸载命令: code --uninstall-extension ms-vscode.cpptools 2. 确保下载正确的版本。注意,WSL需要Linux版本,所以选择`cpptools-linux-x64.vsix`(如果是ARM架构,则选择aarch64版本)。 3. 安装后,扩展目录应该出现在`~/.vscode-server/extensions/ms-vscode.cpptools-<version>`。 4. 进入该目录,检查`bin`目录下的文件是否完整,并具有可执行权限。 5. 运行ldd检查依赖,并安装缺失的库。 例如,常见的依赖缺失: 缺少libssl:`sudo apt install libssl1.1` 缺少libicu:`sudo apt install libicu70`(具体版本号可能不同,根据错误提示安装) 6. 如果遇到其他问题,可以尝试重启VS Code和WSL。 最后,如果用户按照这些步骤操作,应该可以解决问题。 生成相关问题: 1. 如何修复VS Code C++扩展在WSL中无法启动的问题? 2. 如何更新WSL中的VS Code服务器扩展? 3. 如何为VS Code的C++扩展安装缺失的Linux依赖库? 4. 如何调试VS Code远程扩展的安装问题? 5. 在WSL中,如何手动安装VS Code扩展? 6. 如何解决VS Code远程开发中的扩展兼容性问题?</think>### 🔍 解决 VS Code C++ 工具扩展在 WSL 中缺失的问题 以下是针对 `ms-vscode.cpptools` 扩展在 WSL 中缺失的完整解决方案: --- ### 🔧 分步解决方案 #### 1️⃣ **验证扩展安装状态** ```bash # 检查扩展目录是否存在 ls -la ~/.vscode-server/extensions/ | grep ms-vscode.cpptools # 检查全局扩展目录 (Windows 侧) ls -la "/mnt/c/Users/$(whoami)/.vscode/extensions/" | grep cpptools ``` - ➤ **目录不存在**:说明扩展未安装(跳到步骤2) - ➤ **目录存在但内容不全**:手动补全文件(跳到步骤3) #### 2️⃣ **强制重新安装扩展** ```bash # 卸载现有扩展 code --uninstall-extension ms-vscode.cpptools # 下载 Linux 版扩展 (x64架构) wget https://github.com/microsoft/vscode-cpptools/releases/latest/download/cpptools-linux-x64.vsix # 安装扩展 (使用完整路径) code --install-extension $(pwd)/cpptools-linux-x64.vsix # 验证安装 ls ~/.vscode-server/extensions/ms-vscode.cpptools-* ``` #### 3️⃣ **手动补全依赖文件** 当目录存在但文件缺失时: ```bash # 创建目标目录 (替换为实际版本号) VER=$(ls ~/.vscode-server/extensions | grep ms-vscode.cpptools | cut -d'-' -f5) TARGET_DIR="$HOME/.vscode-server/extensions/ms-vscode.cpptools-$VER" # 下载依赖包 wget https://github.com/microsoft/vscode-cpptools/releases/latest/download/cpptools-linux-dependencies.vsix # 解压到目标目录 unzip -o cpptools-linux-dependencies.vsix -d "$TARGET_DIR" # 修复权限 find "$TARGET_DIR" -type f -exec chmod +x {} \; # 创建锁定文件 touch "$TARGET_DIR/install.lock" ``` #### 4️⃣ **验证二进制文件** ```bash # 查找 cpptools 相关二进制 find ~/.vscode-server/extensions/ms-vscode.cpptools-* -name "Microsoft.VSCode.CPP.Extension" # 检查依赖 (示例路径) ldd ~/.vscode-server/extensions/ms-vscode.cpptools-*/bin/Microsoft.VSCode.CPP.Extension ``` 常见缺失库的修复: ```bash sudo apt-get install -y \ libssl1.1 \ # 解决 libssl.so.1.1 缺失 libicu70 \ # 解决 libicuuc.so.70 缺失 zlib1g \ # 解决 libz.so.1 缺失 libkrb5-3 # 解决 libkrb5.so.3 缺失 ``` #### 5️⃣ **架构兼容性处理** ```bash # 检查 WSL 架构 uname -m # ARM 设备专用安装 (如 Jetson/M1) wget https://github.com/microsoft/vscode-cpptools/releases/latest/download/cpptools-linux-aarch64.vsix code --install-extension cpptools-linux-aarch64.vsix ``` --- ### ⚠️ 关键注意事项 1. **路径权限修复**: ```bash sudo chown -R $(whoami):$(id -gn) ~/.vscode-server ``` 2. **版本匹配原则**: ```bash # 查看已安装版本 code --list-extensions --show-versions | grep cpptools ``` 确保依赖包版本与主扩展一致[^1] 3. **日志诊断**: ```bash code --log debug 2> vscode-debug.log grep "cpptools" vscode-debug.log ``` --- ### 📌 预防措施 1. **离线安装包**: ```bash # 下载完整套件 wget https://github.com/microsoft/vscode-cpptools/releases/latest/download/cpptools-linux.vsix \ https://github.com/microsoft/vscode-cpptools/releases/latest/download/cpptools-linux-dependencies.vsix ``` 2. **环境快照**: ```bash # 创建扩展备份 tar -czvf cpptools_backup.tar.gz ~/.vscode-server/extensions/ms-vscode.cpptools-* ``` --- > 通过以上步骤,98%的 `cpptools` 缺失问题可得到解决。若仍存在问题,建议收集完整的 VS Code 日志进行诊断[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜流冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值