When I’m writing C codes on VS Code, IntelliSense can’t detect functions declared in standard libraries, while the project compile properly.
Details:
The flockfile
function is declared as void flockfile (FILE *__stream)
in stdio.h
, however IntelliSense treats the function wrongly.
I found the information that old C compilers treat undeclared functions as int functionname()
.
So I guess that intellisense thinks that flockfile
hasn’t been declared yet, even though I certainly include stdio.h
at the top of the file which uses flockfile
.
When I opened stdio.h
on VS Code, the definition of fnlockfile
is displayed as the unreachable code.
However, the project compile properly without warnings related to fnlockfile
and the built app also works properly.
So I think the root cause of this problem is that the compiler used by IntelliSense receives different arguments from ones the actual compiler receives.
How can I make IntelliSense interpret codes as well as the actual compiler does?
I searched around the related reports on the internet, but I wasn’t able to find.
Adding __USE_POSIX199506
definition before including stdio.h
might address the issue, but I’m using stdio.h
in a couple of files and I don’t want to write that definition every time I include stdio.h
.
Environment:
Host OS: Windows10
Guest OS: Ubuntu 22.04.4 LTS (WSL2)
Compiler: gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04)
Editor: VS Code(1.87.2) with C/C++ extension from Microsoft (v1.19.9)
.vscode/c_cpp_properties.json
file;
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c17",
"cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-x64",
"configurationProvider": "ms-vscode.cpptools"
}
],
"version": 4
}
which gcc
answers /usr/bin/gcc
I’m not savvy in compiler around things, so sorry if it was a silly question…
Edit (add minimap reproducible example)
-
Create a new project folder.
-
Create main.c
#include <stdio.h>
int main(void)
{
flockfile(stderr);
fprintf(stderr, "hello worldn");
funlockfile(stderr);
return 0;
}
-
Create
.vscode
folder and put the abovec_cpp_properties.json
. -
Run below commands to build and run an executable.
$ cc -g -W -Wall -Wno-unused-parameter -iquote . -pthread -iquote platform/linux -c main.c -o main.o
$ cc -g -W -Wall -Wno-unused-parameter -o out.a main.o
$ ./out.a
hello world
- However, IntelliSense in VS Code can’t detect
flockfile
properly.
2
Answers
OK, the problem was solved.
I realized that the compiler args which I passed to the actual compiler wasn't told to intellisense.
So I added
compilerArgs
parameter toc_cpp_properties.json
Then, intellisense can detenct
flockfile
properly.Thank you for everyone who gave me comments.
With the C/C++ extension (cpptools) (and many other C/C++ extensions for VS Code), intellisense is separate from build. See the related official FAQ entry. You either need to manually configure the VS Code extension providing intellisense to know how exactly you are doing build, or find an extension that does both, or one that integrates build info to the C/C++ extension, such as the CMake extension.
In this specific case, you were passing
-pthread
during build, but not to the C/C++ extension for intellisense.