How C/C++ headerfiles are processed in vscode

Viewed 196

I'm learning C now using vscode. I got curious and tried modifying the headerfiles. stdio.h to be specific and I found some really peculiar behavior. I studied the behavior using the simple Helloworld program.

infos: Windows 10 OS, VScode v1.56.0, Compiled using CL not gcc, ucrt file path:

C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\ucrt

The code:

#include<stdio.h>
int main()
{
    printf("Hello world!");
}
  1. So first I tried adding some random lines in the header file and save it. The code ran fine without any errors. I tried it after restarting. It still worked fine.

  2. Then I commented the whole file with 2k+ and saved it and was expecting for the compiler to tell me that printf is not declared anywhere. But still again the code worked fine.

  3. Then I reverted all changes, copied the header file to another folder and deleted the file in the ucrt folder So. Now for the first time compiler said printf isn't declared anywhere.

  4. I modified the headerfile outside and copied it to the urct folder and the code worked fine again. So, as I checked into the file (in ucrt) the changes I made isn't showing here.

  5. So I again deleted the headerfile in ucrt folder, cleared everyline added 2 3 random lines there saved it. And then pasted it in the ucrt to see that code still works really fine. And as I checked the header file.......It is the same old stdio.h that was when I installed it.

  6. And Finally....when I copied the header file in ucrt and pasted it outside. And to my surprise It had the lines I added in No.5

There is only one explanation that I can think of...That VScode overrides any changes in the file.

So here are my questions:

  1. How headerfiles are processed in vscode.
  2. Can we actually modify it(The one in ucrt folder)
  3. What is actually happening in the steps I mentioned above. Why these weird outcomes.
  4. What is the difference between compiling with CL and with GCC.

NOTE: I know it's too lengthy I just wanted to give as much information I have.

1 Answers

VSCode itself doesn't do much with header files, the code explorer, intellisense and linter might have include paths but it is not responsible in any way for code-gen.

You can modify your system stdlib but I really wouldn't recommend it - my recommendation is to compile with -ffreestanding (on GCC and Clang) and then have a copy of a stdlib in your project and include it from there manually (Using -I or by using relative includes).

The most likely culprit is that your compiler & vscode's code explorer are being passed a flag which is modifying their include path, so the stdlib they are accessing is the system one, and the strange file modification behaviour can be attributed to how you are accessing the file (i.e. if you are using Go To Declaration).

Each may have a different default include setup, GCC uses the GNU Toolchain where it can - but ultimately they both follow the same preprocessing rules and its just a question of looking at what is included by default.

One last thing is that sometimes compilers will be nice and if stdio.h or the like isn't included and its a common or stdlib function, they will provide the correct prototype automatically and link as normal without the header being included - its not the case for my GCC config at least, and I'm not sure about CL

Related