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",

如图所示:

至此配置结束。

相关推荐
程序员Supers2 天前
VSCode 好用插件推荐
visual studio code
慢功夫3 天前
💡第五篇:VSCode插件是如何与主进程通信的?
前端·visual studio code
凉凉的知识库3 天前
用 GPT-5.6 SOL 写了个 VS Code 插件,效果出乎意料
api·测试·visual studio code
土土哥tutuge5 天前
开源项目 Claude Code Notifier Plus:让 Claude Code 真正需要你时再提醒你
ai编程·visual studio code
慢功夫6 天前
💡第四篇:编辑器内核, Monaco model、URI、dirty 状态和 tabs 是怎么协作的?
前端·visual studio code
annekqiu7 天前
VS code 部署agent
github·visual studio code
RequieMa7 天前
再见了,所有的 VS Code 预览窗
visual studio code
mCell9 天前
从 VS Code 学系统架构:回到当时,理解它为什么这样演进
架构·源码阅读·visual studio code
你脑门上的脚印10 天前
VSCode 更新后 Cmd(Ctrl)+点击终端地址在编辑器内部打开?一个设置帮你搞定
visual studio code
慢功夫12 天前
开篇:VS Code 为什么不是一个普通 React App
前端·visual studio code