Should VSC C/C++ extention take compile_commands for absolute truth? #13771
Unanswered
bhubavenski
asked this question in
Q&A
Replies: 1 comment
-
It sounds like a bug in the way we handle compile_commands.json. Given that the majority of C++ developers use one of the expected file extensions for C++ source files, it may not be a bug that we end up prioritizing. I would recommend that you change your source files' extensions so that you get the best support from the various tools you'll end up using for C++ development (many of which will bake in some of the same assumptions we do about file extensions). |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
🤖 compile_commands.json is not absolute truth (C++ extension behavior)
Hello!
I’d like to bring up a topic for discussion. Below, I’ve structured my thoughts and observations into a few sections: background, description, idea, reproduction, and conclusion.
🧭 A bit of background
I recently started learning C++ and exploring the official VS Code extension for it. When I got to the topic of
compile_commands.json
, I read that the extension uses this file to determine how each file is supposed to be compiled.I wanted to test whether this is an absolute truth, or if there are exceptions.
📌 compile_commands.json is not absolute truth
If you’re using the C++ Standard Library but your file has a
.c
extension, the compiler will, by default, treat it as a C file.That means it will use the C runtime and include only the C Standard Library (e.g.
<stdio.h>
), but not the C++ Standard Library (e.g.<iostream>
).However, if you explicitly tell the compiler to treat the
.c
file as C++ using the-x c++
flag, for example:then the compilation will succeed and link against the C++ Standard Library.
❗ Problem with IntelliSense
Even though the actual compilation works fine, IntelliSense may still report errors and fail to recognize C++ headers. That’s because:
compile_commands.json
;"cStandard": "c17"
setting fromc_cpp_properties.json
when it sees a.c
file.So even if the compiler is using C++, IntelliSense continues to treat the file as plain C, purely based on its extension.
❓ Discussion question
Shouldn’t IntelliSense fully honor
compile_commands.json
and treat files according to the flags specified there?If a file is compiled with
g++ -x c++
, is it really correct for IntelliSense to treat it as C just because it ends in.c
?Beta Was this translation helpful? Give feedback.
All reactions