-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Should VSC C/C++ extention take compile_commands for absolute truth? #13771
-
🤖 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.
Note: this is a personal reflection, structured with the help of ChatGPT.
🧭 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.
(Please don’t judge too harshly if this seems naive — I’m still learning! 😊)
📌 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:
g++ -x c++ -std=c++17 -o hello.exe src/hello.c
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:
- IntelliSense does not fully trust
compile_commands.json
; - and it applies the
"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
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.