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

如图所示:

至此配置结束。

相关推荐
ServBay16 天前
Local harness 将退役,VS Code 本地开发要不要搬到云端?
后端·ai编程·visual studio code
DigitalOcean1 个月前
不受限于 Copilot,教你在 VS Code 中接入 GPT、Claude
agent·visual studio code
formulahendry1 个月前
为什么全世界都在 Fork 它?VS Code 官方纪录片(中字完整版)正式上线!
微软·visual studio code·vs code
三十而立洋1 个月前
知枝 · MindSprout:可以直接将 markdown 转换为思维导图的 vscode 插件
前端·visual studio code
formulahendry2 个月前
在 VS Code 里自由切换 Copilot、Claude、Codex、OpenCode、Pi、Kimi、Qwen:任何 Agent 都能接
visual studio code·vs code·acp
开开心心就好2 个月前
打印机卡住打不了?一键清理卡死打印队列
服务器·开发语言·算法·pdf·ocr·idea·visual studio code
free-elcmacom2 个月前
手撕 memcpy 和 memmove:从字节级拷贝到内存重叠的终极较量
c语言·开发语言·青少年编程·visual studio code·visual studio
free-elcmacom2 个月前
发际线警告!手撕《C陷阱与缺陷》终极 BOSS:signal 函数与 typedef 魔法
c语言·开发语言·visual studio code·visual studio
前端小白乘风2 个月前
VS Code 终于能连微信了!发现一个硬核开源神仙插件:WeChat AHP
微信·visual studio code·deepseek
free-elcmacom3 个月前
C语言指针进阶——指针数组模拟二维数组,以及二级指针到底怎么理解
c语言·visual studio code