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

如图所示:

至此配置结束。

相关推荐
芯橦6 小时前
【瑞昱RTL8763E】音频
单片机·嵌入式硬件·mcu·物联网·音视频·visual studio code·智能手表
行十万里人生4 天前
信号处理: Block Pending Handler 与 SIGKILL/SIGSTOP 实验
c++·后端·深度学习·ubuntu·serverless·信号处理·visual studio code
奇点 ♡7 天前
【线程】线程安全的单例模式
linux·c语言·c++·安全·单例模式·visual studio code
羊小猪~~8 天前
C/C++语言基础--C++运算符重载以及其重载限制
java·c语言·开发语言·c++·visualstudio·visual studio code
豆包MarsCode10 天前
豆包MarsCode IDE 搭建 VitePress 博客并使用 GitHub 部署
ide·人工智能·python·数据分析·github·visual studio code
奇点 ♡11 天前
【线程】POSIX信号量---基于环形队列的生产消费者模型
linux·运维·服务器·开发语言·c++·算法·visual studio code
自白11 天前
关于我是如何二次开发了 antd-vue 的a-range-picker组件,同时还添加了 vscod智能提示这件事
vue.js·visual studio code·ant design
奇点 ♡15 天前
【线程】线程的控制
linux·运维·c语言·开发语言·c++·visual studio code
羊小猪~~20 天前
C/C++语言基础--从C到C++的不同(上)
linux·c语言·c++·后端·qt·visualstudio·visual studio code
就是蠢啊24 天前
VScode 的简单使用
visual studio code