Visual Studio Code的 IntelliSense无法识别std::cout

编程语言 2026-07-10

在Visual Studio Code中,使用以下代码时出现以下错误。

#include <iostream> // Included header iostream is not used directly (fix available)clangdunused-includes 

int main() {
    std::cout << "Hello, world!" << std::endl; // No member named 'cout' in namespace 'std'clang(no_member)  No member named 'endl' in namespace 'std'clang(no_member)
}

供参考如下:

  • 我使用Windows 10 21H2
  • 我使用 "g++" MSYS2作为编译器,在Visual Studio Code上使用clangd作为语言服务器(我也尝试过IntelliSense,并且遇到了一些问题)

现在,这是我在尝试修复问题时设置的配置

用户设置(VS Code):

{
    "clangd.path": "c:\\Users\\soul\\AppData\\Roaming\\Code\\User\\globalStorage\\llvm-vs-code-extensions.vscode-clangd\\install\\22.1.0\\clangd_22.1.0\\bin\\clangd.exe",
    "C_Cpp.autocompleteAddParentheses": true,
    "C_Cpp.default.cppStandard": "c++17",
    "C_Cpp.default.cStandard": "c99",
    "C_Cpp.default.includePath": [
        "C:\\msys64\\mingw64\\include\\**"
    ],
    "C_Cpp.default.intelliSenseMode": "windows-gcc-x64",
    "C_Cpp.intelliSenseEngine": "disabled",
    "makefile.configureOnOpen": true
}

.vscode/settings.json:

{
    "terminal.integrated.profiles.windows": {
        "PowerShell": {
            "source": "PowerShell",
            "icon": "terminal-powershell"
        },
        "Command Prompt": {
            "path": [
                "${env:windir}\\Sysnative\\cmd.exe",
                "${env:windir}\\System32\\cmd.exe"
            ],
        "args": [],
        "icon": "terminal-cmd"
        },
        "Git Bash": {
            "source": "Git Bash"
        },
        "MSYS2": {
            "path": "C:\\msys64\\usr\\bin\\bash.exe",
            "args": [
                "--login",
                "-i"
            ],
            "env": {
                "MSYSTEM": "MINGW64",
                "CHERE_INVOKING": "1"
            }
        }
    },
    "clangd.fallbackFlags": [
        "-I${workspaceFolder}/include",
        "-IC:/msys64/mingw64/include",
        "-IC:/msys64/mingw64/include/c++/15.2.0"
    ]
}

.vscode/c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/include/**",
                "C:\\msys64\\mingw64\\include\\**",
                "C:\\msys64\\mingw64\\include\\c++\\15.2.0\\**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "browse": {
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            },
            "cppStandard": "c++17"
        }
    ],
    "version": 4
}

.clangd:

CompileFlags:
    Add:
        # - '--query-driver=C:\msys64\mingw64\g++.exe'
        # - '-std:c++latest'
        - "-IC:/msys64/mingw64/include"
        - "-I${workspaceFolder}/include"
        - "-IC:/msys64/mingw64/include/c++/15.2.0"

Makefile:

CXX = g++
CC = gcc
# -I. (current dir) and -I./include (for glad headers)
CXXFLAGS = -Wall -O2 -I./include
CFLAGS = -Wall -O2 -I./include

# Link against GLFW and Windows system libs
LDFLAGS = -lglfw3 -lopengl32 -lgdi32 -luser32 -lkernel32

# Include both your C++ code and the GLAD C source
SRCS = src/main.cpp src/glad.c
TARGET = glGame

all: $(TARGET)

$(TARGET): $(SRCS)
    $(CXX) $(CXXFLAGS) $(SRCS) -o $(TARGET) $(LDFLAGS)

clean:
    rm -f $(TARGET).exe

评论区有人建议我把我使用的扩展也添加上:

  • C/C++ by Microsoft
  • C/C++ DevTools by Microsoft
  • C/C++ Extension Pack by Microsoft
  • C/C++ Themes by Microsoft
  • clangd by LLVM
  • CMake Tools by Microsoft(包含在C/C++ MS扩展包中)
  • Makefile Tools by Microsoft

解决方案

你漏掉了VSCode C/C++扩展的C++包含路径。

    "C_Cpp.default.includePath": [
        "C:\\msys64\\mingw64\\include\\**"
    ],

应该是

    "C_Cpp.default.includePath": [
        "C:\\msys64\\mingw64\\include\\**",
        "C:\\msys64\\mingw64\\include\\c++\\15.2.0\\**"
    ],
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章