3

Small C++ project using CMake, set to c++20, compiles fine using <string_view>.

  • host OS: Windows 10, remote OS: Ubuntu 20.04
  • VSCode versions: VSCode 1.71.2, C/C++ extension 1.12.4, CMake Tools 1.12.27
  • compiler: clang 15 as installed by apt.llvm.org/llvm.sh

But VSCode IntelliSense insists that std::string_view is undefined:

enter image description here

If I F12 on #include <string_view> I go to /usr/include/c++/9/string_view where I see that it thinks the built-in preprocessor macro __cplusplus is set to c++11:

enter image description here

It is getting its configuration from CMake Tools as seen by the following message in the "C/C++ Configuration Warnings" output window:

[10/3/2022, 9:57:47 PM] For C++ source files, the cppStandard was changed from "c++23" to "c++20" based on compiler args and querying compilerPath: "/usr/bin/clang"

(I had set the VSCode IntelliSense Configuration "C++ standard" to "c++23" while my CMake project was still saying "c++20".)

My c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "Linux",
            "intelliSenseMode": "linux-clang-x64",
            "includePath": [],
            "defines": [],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c17",
            "cppStandard": "c++20",
            "configurationProvider": "ms-vscode.cmake-tools"
        }
    ],
    "version": 4
}

Why is the built-in __cplusplus macro wrong for whatever it is that IntelliSense is doing?

The IntelliSense engine is "default":

enter image description here

I installed the extension clangd--Clang's own language server--and disabled the built-in IntellSense (clangd warned of a conflict there, which would be expected). This doesn't work either, but now __cplusplus is set to c++14--no other change to any settings were made.

enter image description here

I did try the solution suggested in this answer to How to change C++ version being used by VS Code? which is to set the language in the C/C++ extension settings. As is demonstrated by the c_cpp_properties.json file included--they are the "cStandard" and "cppStandard" properties. And the question also shows that those properties aren't used in this case because I'm using the setting where IntelliSense asks the build system--CMake Tools in this case--for the proper settings: that is shown by the actual message given by CMake Tools.

philipxy
  • 14,867
  • 6
  • 39
  • 83
davidbak
  • 5,775
  • 3
  • 34
  • 50
  • @273K the closure of this question has been disputed in [a revision](/revisions/73941460/3) and [on meta](https://meta.stackoverflow.com/q/420668) if you'd like to follow up. – Henry Ecker Oct 04 '22 at 02:10
  • That question is about setting C++ version for the extension ms-vscode.cpptools that is vscode Intellisense. There is nothing about compilation. – 273K Oct 04 '22 at 02:13
  • @HenryEcker This question is a duplicate of the well answered question. – 273K Oct 04 '22 at 02:16
  • @273K I appreciate your taking a second look. To be clear, I wasn't disagreeing with your action (I know almost nothing about this specific topic). I was just bringing it to your attention in case you were interested in following up with the OP here. – Henry Ecker Oct 04 '22 at 02:18
  • @273K - Oh, and _one more thing. Here is the _actual other question_: "I printed ..cplusplus and found out that my files are executed with C++98 in VSCode. " The question asks about _executing_ (that is, _compiling_ and executing) C++. And that _execution_ gave the wrong version. The _question_ is _not_ about Intellisense. And also the answer, with 16 votes, wasn't checked as "the answer" by the questioner. In _this_ question compilation _works_. – davidbak Oct 04 '22 at 02:36
  • I recommend you to uninstall the Clangd extension, it can give you more problems than that, like cryptic errors like in [this](https://stackoverflow.com/questions/73115800/unknown-signal-exception-when-debugging-on-visual-studio-code) question I asked while ago. Use the Microsoft one, it's more stable. – Otávio Augusto Silva Oct 04 '22 at 16:02
  • @OtávioAugustoSilva - well, what you say doesn't surprise me in general anyway, but I will do that as that extension didn't fix this particular problem anyway! – davidbak Oct 04 '22 at 16:04
  • I see `/usr/include/c++/9`, are there any other version? – Bob__ Oct 04 '22 at 16:57
  • I see you've set `"configurationProvider"` in your cpp-tools config. Have you done anything in your CMake configuration to indicate the language standard to use? Ex. configuring [the `CMAKE_CXX_STANDARD` cache variable](https://cmake.org/cmake/help/latest/variable/CMAKE_CXX_STANDARD.html) or using [`target_compile_features`](https://cmake.org/cmake/help/latest/command/target_compile_features.html)? – starball Oct 30 '22 at 22:07
  • [Why should I not upload images of code/data/errors when asking a question?](https://meta.stackoverflow.com/q/285551/3404097) – philipxy Oct 30 '22 at 22:13
  • @philipxy - thank you I get that of course, problem is Intellisense is entirely a matter of _the IDE_. It's important to show, for purposes of this question, how screenshots of the IDE because that's really the only way to show some of what it is thinking. (The error about `string_view` shows up in a message window - though _not_ a terminal window - but the information about what value it thinks the macro has is only shown in a pop-up window. AFAIK. Correct me if I'm wrong.) – davidbak Oct 30 '22 at 22:39
  • Content that isn't about how something looks should be given as text. Like error messages & code. Thanks, I'm done. – philipxy Oct 30 '22 at 22:45
  • @david-fong - true, I did not give you my `CMakeLists.txt` but I _did_ give the informational message from the VSCode "CMake provider" which works off of it, using CMake itself to interpret it, and _that_ informed us that the compiler told it it was set to use `C++20`. Thanks! – davidbak Oct 30 '22 at 22:48
  • 1
    for me, it was the `"configurationProvider"` key-value pair in `settings.json`. i got rid of the problem by deleting that line entirely. – Mampac Jan 30 '23 at 17:37
  • 1
    If vscode intellisense still cannot detect the standard higher than C++14. Try to search in command palette (ctrl+p or ctrl+shift+p) with `C/C++: Edit Configurations (UI)`. Then check the `C++ standard` in this UI. I found it was set to `C++14` while `ms-vscode.cpptools` was set to `C++20`. – Louis Go Feb 02 '23 at 07:04
  • I was able to fix this on Windows with MSVC/cl.exe by adding `/Zc:__cplusplus` to the compiler arguments. – Mark A. Ropper May 07 '23 at 17:31
  • you need to show a [mre], which I've already asked about several months ago. For what it's worth, I just wrote [what I think is a better answer to the question you linked](https://stackoverflow.com/a/76610269/11107541). – starball Jul 04 '23 at 07:34

0 Answers0