系统:ubuntu 20.04
工具:visual studio code ver 1.67.2
错误发生原因:结构体成员指定初始化(Designated initializers)出错
错误场景还原:
#include "stdio.h"
struct test{
int ID;
int name;
};
int main(void)
{
struct test t1 = {1,2};
struct test t2 = {3,4};
/* t3 在vscode中会报错duplicate designator is not allowedC/C++(2906)*/
struct test t3 ={
.ID = 2019,
.name = 1111};
}
错误分析:
应该是vscode C++ Intellisense 没有完全支持 gcc编译器的问题,虽然并不影响编译,但是实在是闹心,查到github上的回复是已经在1.4.0-insiders版本应该已经修复了,官方已经到v1.9.8版本,但是依然有这个问题,即使手动安装1.4.0版本错误提示依然存在。
解决方法:将编译器设置改为clang,我的c_cpp_properties.json配置如下:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/src/**",
"/usr/lib/clang/10.0.0/include/**"
],
"defines": [],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++20",
"intelliSenseMode": "clang-x64",
"configurationProvider": "ms-vscode.makefile-tools"
}
],
"version": 4
}
在"includePath"
中加入clang的头文件库,并且在compilePath
中设置clang的位置就可以了,具体位置查询可以使用which clang
获得。
切换clang后vsc可能出现的问题:
1、使用#define来定义标识符来表示一个字符串常量,如果在使用过程中出现拼接,字符串和标识符之间需要增加空格,否则会报错:user-defined literal operator not found C/C++(2486)
输出错误:
正确写法:
Ref:
https://github.com/microsoft/vscode-cpptools/issues/6316
https://github.com/microsoft/vscode-cpptools/issues/3491
https://github.com/microsoft/vscode-cpptools/issues/5550