栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Mac vscode配置c++11

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Mac vscode配置c++11

Mac vscode配置c++11

在Mac vscode默认配置中,c++的标准为c++98,有很多语法支持不了,查网上的资料,发现有些资料里写的改上去没效果,特此记录一下,避免之后再踩坑。本文是使用mac上的clang编译器

首先是新建好文件夹,在里面可以添加上如下的测试程序:

#include 
#include 
#include 

using namespace std;

int main()
{
    vector msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};

    for (const string& word : msg)
    {
        cout << word << " ";
    }
    cout << endl;
}

然后从主菜单中,选取“终端”>“配置默认生成任务”。将出现一个下拉菜单,列出VS Code在您的机器上找到的编译器的各种预定义构建任务。选择C/C++ clang++构建活动文件以构建编辑器中当前显示(活动)的文件。

然后将.vscode 文件夹下tasks.json改为如下代码:

{
  // See https://go.microsoft.com/fwlink/?linkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "clang++ build active file",
      "command": "/usr/bin/clang++",
      "args": [
        "-std=c++17",
        "-stdlib=libc++",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}/${filebasenameNoExtension}"
      ],
      "options": {
        "cwd": "${workspaceFolder}"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

launch.json改为

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "clang++ - Build and debug active file",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}/${filebasenameNoExtension}",
      "args": [],
      "stopAtEntry": true,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "lldb",
      "preLaunchTask": "clang++ build active file"
    }
  ]
}

c_cpp_properties.json文件

{
  "configurations": [
    {
      "name": "Mac",
      "includePath": ["${workspaceFolder}/**"],
      "defines": [],
      "macframeworkPath": [
        "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/frameworks"
      ],
      "compilerPath": "/usr/bin/clang",
      "cStandard": "c11",
      "cppStandard": "c++17",
      "intelliSenseMode": "clang-x64"
    }
  ],
  "version": 4
}

理论上以上操作就OK了。但是如果想要使用code runner使其按照c++11标准编译,那么还需要设置code runner

在setting.json加上以下代码即可

    "code-runner.runInTerminal": true,
    "C_Cpp.default.cppStandard": "c++11",
    "code-runner.executorMap": {
        
        "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt -std=c++14 && $dir$fileNameWithoutExt"
    },
    "files.associations": {
        "typeinfo": "cpp"
    },
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/744344.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号