I have two C files, main.c
and library.c
, which contain the following:
// main.c
#include <stdio.h>
int MY_NUMBER = 6;
#include "library.c"
int main() {
change_the_number();
printf("The number: %dn", MY_NUMBER);
}
// library.c
void change_the_number() {
MY_NUMBER = 5;
}
After running gcc main.c -o main
, and then running main
, I get the output The number: 5
, which is exactly what I would expect.
VSCode underlines MY_NUMBER
in library.c
, complaining that “identifier MY_NUMBER
is undefined”. This is also expected behaviour, as far as I’m concerned – looking solely at library.c
, there’s nothing to indicate what MY_NUMBER
is or where it comes from.
How can I configure Microsoft’s C/C++ extension to recognise MY_NUMBER
?
What I’ve tried so far:
- Adding
${workspaceFolder}
and${workspaceFolder}/**
toC-Cpp > Default: Include Path
. - Changing
library.c
tolibrary.h
(I didn’t really think this would do anything and I was right.)
One constraint is that I am unable to change the code – I must setup the IDE to allow for this declaration/usage arrangement, because I’m an intern and this pattern is repeated hundreds, if not thousands of times across the codebase I’m working on. I would ask my coworkers for help with this, but none of them use VSCode with C/C++ extension – every single one uses something different.
Ultimately, if the MS C/C++ extension doesn’t support this kind of thing, that’s fine, I’ll just use something else, but I thought it was interesting that this completely valid C program would flag up as problematic.