VisualStudio Code 支持C++11插件配置

问题

Visual Studio Code中的插件: Code Runner 支持运行C、C++、Java、JS、PHP、Python等多种语言。

但是它不支持C++11特性的一些使用,比如类似错误:

binarySearch.cpp:26:17: error: non-aggregate type 'vector' cannot be initialized with an initializer list

需要安装插件:

  • C/C++
  • CodeRunner

C/C++配置

打开插件拓展 , 找到C/C++ , 选择扩展设置

可通过查找,找到配置Custom Configuration Variables , 并打开setttings.json文件

添加如下内容,保存关闭

json 复制代码
"C_Cpp.default.compilerArgs": [
  "-g",
  "${file}",
  "-std=c++11",
  "-o",
  "${fileDirname}/${fileBasenameNoExtension}"
],

然后打开本目录内的**.vscode**文件夹,查看是否存在c_cpp_properties.json文件,如果没有,则新建, 它是用于配置C++语言环境的IntelliSense配置相关,将如下内容复制进去:

json 复制代码
{
    "configurations": [
      {
        "name": "Mac",
        "defines": [],
        "macFrameworkPath": [
          "/System/Library/Frameworks",
          "/Library/Frameworks",
          "${workspaceFolder}/**"
        ],
        "compilerPath": "/usr/bin/g++",
        "cStandard": "c11",					// 如果存在,则重要修改此处
        "cppStandard": "c++11",				// 如果存在,则重要修改此处
        "intelliSenseMode": "clang-x64",
        "browse": {
          "path": [
            "${workspaceFolder}"
          ],
          "limitSymbolsToIncludedHeaders": true,
          "databaseFilename": ""
        }
      }
    ],
    "version": 4
}

然后打开 .vscode 文件夹下的tasks.json文件,在args内增加属性:

json 复制代码
"args": [
  "-std=c++11",                       // add
  "-stdlib=libc++",                   // add
  "-fdiagnostics-color=always",       // add

  "-fcolor-diagnostics",
  "-fansi-escape-codes",
  "-g",
  "${file}",
  "-o",
  "${fileDirname}/${fileBasenameNoExtension}"
],

到这里C++11的配置算是结束,但是CodeRunner插件需要配置下,保证C++11的代码运行正常。

Code Runner

打开拓展 ,找到CodeRunner ,选择扩展设置

找到Executor Map By File Extension , 打开settings.json

查找下字符串"cpp", 替换内容为:

json 复制代码
"cpp": "cd $dir && g++ -std=c++11 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",

如图所示:

至此配置结束。

相关推荐
晚秋大魔王13 小时前
C# 添加图标
c#·visual studio code
长安第一美人4 天前
Qt中2个.app源文件之间函数与变量的互相调用
开发语言·c++·嵌入式硬件·qt·visual studio code
一梦南柯8 天前
开发新体验:基于Ollama+deepseek打造私有化代码助手
人工智能·visual studio code·deepseek
敲敲敲敲暴你脑袋8 天前
vue3中ref响应式变量为什么script中要用.value,而template模板中不需?
javascript·vue.js·visual studio code
红虾程序员11 天前
CSS盒子模型详解
前端·pycharm·intellij-idea·css3·html5·visual studio code
天机️灵韵12 天前
Continue 与 CodeGPT 插件 的对比分析
ide·visualstudio·intellij-idea·visual studio code
Lorcian15 天前
web前端12--表单和表格
前端·css·笔记·html5·visual studio code
Lorcian21 天前
web前端11--伪类与过渡
前端·css·笔记·html5·visual studio code
惜.己23 天前
鸿蒙仓颉环境配置(仓颉SDK下载,仓颉VsCode开发环境配置,仓颉DevEco开发环境配置)
vscode·华为·harmonyos·visual studio code·仓颉
小兵张健24 天前
cursor 使用教程(07) —— 内嵌对话框
程序员·visual studio code·visual studio