microsoft / vscode-cpptools

Official repository for the Microsoft C/C++ extension for VS Code.
Other
5.5k stars 1.55k forks source link

Cannot find c++ std headers even manually set include path. #12259

Closed LoveDaisy closed 4 months ago

LoveDaisy commented 5 months ago

Environment

Bug Summary and Steps to Reproduce

Bug Summary:

  1. VSCode cannot find std headers (such as <algorithm>, <string>), even if I manually specify includePath and systemIncludePath.
  2. The project is built successfully in terminal alone. It is also built smoothly with cmake-tools extension (click "configure" and "build" icon in cmake panel). The only problem is the intelligence tooltips.)
  3. I've installed c/c++, c/c++ extension pack, and clangd extensions.

Steps to reproduce:

  1. SSH remote to my developing computer, which is an arm linux machine with OS ubuntu 22.04.
  2. Enter the project folder, and build with building script ./script/build.sh and it works well.
  3. Remove the build folder and enter the "CMake" panel. Click "Delete cache and reconfigure" icon (or cmd+shift+p and choose "CMake: Delete cache and reconfigure") -- it configure successfully. Then click "Build" icon (or cmd+shift+p and choose "CMake: Build target") -- it build successfully.
  4. Open any source file, the error "file not found" is shown on the first #include <...> line. The error is only on the std headers, while the project headers are all OK.
  5. I checked the compile_commands.json. It is generated successfully and I've copied it to build/compile_commands.json where it should be.
  6. I manually set "C_Cpp.default.includePath" and "C_Cpp.default.systemIncludePath" but it won't help.

Expected behavior: C++ std header files can be find correctly.

Configuration and Logs

The configuration is:

{
    "cmake.configureEnvironment": {
        "PKG_CONFIG_PATH": "${workspaceFolder}/lib/opencv/lib/pkgconfig:${workspaceFolder}/lib/rknpu2/lib/pkgconfig"
    },
    "cmake.buildDirectory": "${workspaceFolder}/build/build_linux_aarch64",
    "cmake.copyCompileCommands": "${workspaceFolder}/build/compile_commands.json",
    "C_Cpp.default.cppStandard": "c++14",
    "C_Cpp.default.includePath": [
        "${workspaceFolder}",
        "/usr/include/c++/11"
    ],
    "C_Cpp.default.systemIncludePath": [
        "/usr/include/c++/11"
    ]
}

There is no `C/C++: Log Diagnostics` in my command palette.

The output of `clangd` is:
```log
I[11:29:26.388] Ubuntu clangd version 12.0.1-19ubuntu3
I[11:29:26.388] PID: 1287776
I[11:29:26.388] Working directory: /home/orangepi/zhangjiajie/surveillance_ai
I[11:29:26.388] argv[0]: /usr/bin/clangd-12
I[11:29:26.388] Starting LSP over stdin/stdout
I[11:29:26.389] <-- initialize(0)
I[11:29:26.390] --> reply:initialize(0) 0 ms
I[11:29:26.613] <-- initialized
I[11:29:26.668] <-- textDocument/didOpen
I[11:29:26.672] Loaded compilation database from /home/orangepi/zhangjiajie/surveillance_ai/build/compile_commands.json
I[11:29:26.674] --> window/workDoneProgress/create(0)
I[11:29:26.674] Enqueueing 43 commands for indexing
I[11:29:26.676] ASTWorker building file /home/orangepi/zhangjiajie/surveillance_ai/src/roi/roi_pipeline.h version 1 with command inferred from /home/orangepi/zhangjiajie/surveillance_ai/src/roi/roi_pipeline.cc
[/home/orangepi/zhangjiajie/surveillance_ai/build/build_linux_aarch64]
/usr/bin/c++ --driver-mode=g++ -DWZAI_USE_OPENCV_ALL -Dwzai_EXPORTS -I/home/orangepi/zhangjiajie/surveillance_ai -I/home/orangepi/zhangjiajie/surveillance_ai/third_party/miniz -I/home/orangepi/zhangjiajie/surveillance_ai/third_party/crypt -I/home/orangepi/zhangjiajie/surveillance_ai/lib/rknpu2/lib/pkgconfig/../../include -I/home/orangepi/zhangjiajie/surveillance_ai/lib/opencv/lib/pkgconfig/../../include/opencv -I/home/orangepi/zhangjiajie/surveillance_ai/lib/opencv/lib/pkgconfig/../../include -g -fPIC -c -x c++-header -std=c++14 /home/orangepi/zhangjiajie/surveillance_ai/src/roi/roi_pipeline.h -fsyntax-only -resource-dir=/usr/lib/llvm-12/lib/clang/12.0.1
I[11:29:26.680] --> textDocument/clangd.fileStatus
I[11:29:26.812] Loaded compilation database from /home/orangepi/zhangjiajie/surveillance_ai/build/build_linux_aarch64/compile_commands.json
I[11:29:26.822] <-- textDocument/documentSymbol(1)
I[11:29:26.822] <-- textDocument/documentLink(2)
I[11:29:26.823] <-- textDocument/codeAction(3)
I[11:29:26.922] <-- reply(0)
I[11:29:26.922] --> $/progress
I[11:29:26.922] --> $/progress
I[11:29:27.028] <-- textDocument/documentSymbol(4)
I[11:29:27.083] --> $/progress
I[11:29:27.083] --> $/progress
I[11:29:27.167] <-- textDocument/semanticTokens/full(5)
I[11:29:27.716] --> textDocument/publishDiagnostics
I[11:29:27.720] --> reply:textDocument/documentSymbol(1) 897 ms
I[11:29:27.721] --> reply:textDocument/documentLink(2) 898 ms
I[11:29:27.722] --> reply:textDocument/codeAction(3) 898 ms
I[11:29:27.723] --> reply:textDocument/documentSymbol(4) 694 ms
I[11:29:27.724] --> reply:textDocument/semanticTokens/full(5) 556 ms
I[11:29:27.724] --> textDocument/clangd.fileStatus


### Other Extensions

I have installed C/C++, C/C++ extension pack, CMake, CMake Tools, clangd, C/C++ themes.

### Additional context

_No response_
sean-mcmanus commented 5 months ago

@LoveDaisy C/C++: Log Diagnostics is only in the command palette when a C/C++ file is active.

Our extension is the C/C++ extension and not the clangd extension -- which do you intend to use? The C_Cpp settings are for the C/C++ extension. You should set the compilerPath setting and avoid setting system includes explicitly, since in your case you only provided 1 of them. You could also set the configurationProvider or compileCommands properties. Maybe look at https://code.visualstudio.com/docs/cpp/cmake-linux .

LoveDaisy commented 5 months ago

Sorry for my confusion. I'm using both C/C++ extension and clangd extension. How to tell whether this problem is caused by clangd or c/c++?

I've tried: 1. to provide only compiler path; 2. to provide both compiler path and system include paths. But both have nothing to do with the problem.

I also tried to set compileCommands to the "compile_commands.json" file, but also nothing helps.

And additional information. I tried exactly the same settings on another machine (ssh to another host, whose OS is also ubuntu), the same extensions and the same configurations, and everything works smoothly.

sean-mcmanus commented 5 months ago

@LoveDaisy You could disable one. Or instead of disabling the C/C++ extension you can set C_Cpp.intelliSenseEngine to "disabled".

If you have the CMake Tools extension installed then our extension maybe trying to use that to configure. You can use the C/C++: Log Diagnostics command after opening a C/C++ file to see how the file is getting configured for IntelliSense.

LoveDaisy commented 4 months ago

Thanks for you advices @sean-mcmanus !

I think there may be something wrong at the specific machine. Now the instance is closed and there's no problem with new instances. Your advices are valuable in case I come into the same problem next time. Thanks for your time!