当我在VS Code上编写C代码时,当项目正常编译时,IntelliSense无法检测到标准库中声明的函数.
详细信息:
flockfile
函数在stdio.h
中被声明为void flockfile (FILE *__stream)
,但IntelliSense错误地对待该函数.
我发现老的C编译器将information个未声明的函数视为int functionname()
个.
所以我猜IntelliSense认为flockfile
还没有声明,尽管我肯定在使用flockfile
的文件的顶部包含了stdio.h
.
当我在VS Code上打开stdio.h
时,fnlockfile
的定义显示为无法访问的代码.
然而,该项目编译正常,没有与fnlockfile
相关的警告,构建的应用程序也可以正常工作.
所以我认为这个问题的根本原因是IntelliSense使用的编译器接收的参数与实际编译器接收的参数不同.
如何才能让IntelliSense像实际编译器一样解释代码?
我在网上搜索了相关报道,但没有找到.
在包含stdio.h
之前添加__USE_POSIX199506
个定义可能会解决这个问题,但我在几个文件中使用了stdio.h
,我不想在每次包含stdio.h
时都编写该定义.
环境:
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
个文件;.vscode/c_cpp_properties.json
个文件;
{
"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
个答案/usr/bin/gcc
我对编译程序不在行,所以如果这是一个愚蠢的问题,很抱歉…
编辑(添加可重现的小 map 示例)
-
创建新项目文件夹.
-
创建main.c
#include <stdio.h>
int main(void)
{
flockfile(stderr);
fprintf(stderr, "hello world\n");
funlockfile(stderr);
return 0;
}
-
创建
.vscode
个文件夹,并将上面的c_cpp_properties.json
放入. -
运行以下命令以生成和运行可执行文件.
$ 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
- 然而,VS Code中的智能感知无法正确检测
flockfile
.