在mac上使用vscode创建第一个C++项目

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

在mac上使用vscode创建第一个C++项目

sundaysc++   2020-12-30 我要评论

https://blog.csdn.net/bujidexinq/article/details/106539523

 

 

准备工作:
安装好vscode
安装插件『C/C++』
正式开始:
首先是创建一个空的文件夹(比如文件夹为test),然后在其中新建一个.cpp文件(比如文件为hello.cpp)打开vscode打开test文件夹作为工作目录,接下来用三步配置好C++开发环境

第一步:
[⇧⌘P]打开命令模式,选择[C/Cpp: Edit Configurations(JSON)]命令,回车后会自动生成一个.vscode目录,目录下有一个c_cpp_properties.json文件,下面给出我的文件示例:

  1.  
    {
  2.  
        "configurations": [
  3.  
            {
  4.  
                "name": "Mac",
  5.  
                "includePath": [
  6.  
                    "${workspaceFolder}/**",
  7.  
                    "/Library/Developer/CommandLineTools/usr/include/c++/v1",
  8.  
                    "/usr/local/include",
  9.  
                    "/Library/Developer/CommandLineTools/usr/lib/clang/11.0.0/include",
  10.  
                    "/Library/Developer/CommandLineTools/usr/include"
  11.  
                ],
  12.  
                "defines": [],
  13.  
                "macFrameworkPath": [
  14.  
                    "/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks",
  15.  
                    "/System/Library/Frameworks",
  16.  
                    "/Library/Frameworks"
  17.  
                ],
  18.  
                "compilerPath": "/usr/bin/clang",
  19.  
                "cStandard": "c11",
  20.  
                "cppStandard": "c++17",
  21.  
                "intelliSenseMode": "clang-x64"
  22.  
            }
  23.  
        ],
  24.  
        "version": 4
  25.  
    }


第二步:
[⇧⌘P]打开命令模式,选择[Tasks: Configure Task]命令,选择的模板为MSBuild,回车后会自动在.vscode目录下生成一个tasks.json文件,下面给出我的文件示例:

  1.  
    {
  2.  
        // See https://go.microsoft.com/fwlink/?LinkId=733558
  3.  
        // for the documentation about the tasks.json format
  4.  
        "version": "2.0.0",
  5.  
        "tasks": [
  6.  
            {
  7.  
                "label": "build c++",
  8.  
                "type": "shell",
  9.  
                "command": "g++",
  10.  
                "args": [
  11.  
                    "${file}",
  12.  
                    "-std=c++17",
  13.  
                    "-g",
  14.  
                    "-Wall",
  15.  
                    "-lm",
  16.  
                    "-o",
  17.  
                    "${fileDirname}/${fileBasenameNoExtension}.out"
  18.  
                ],
  19.  
                "group": "build",
  20.  
                "presentation": {
  21.  
                    "reveal": "silent",
  22.  
                    "panel": "shared",
  23.  
                    "echo": true,
  24.  
                    "focus": false,
  25.  
                    "showReuseMessage": true,
  26.  
                    "clear": false
  27.  
                },
  28.  
                "problemMatcher": "$gcc"
  29.  
            },
  30.  
            {
  31.  
                "label": "run c++",
  32.  
                "type": "shell",
  33.  
                "dependsOn": "build c++",
  34.  
                "command": "${fileDirname}/${fileBasenameNoExtension}.out",
  35.  
                "presentation": {
  36.  
                    "focus": true
  37.  
                },
  38.  
                "group": "test"
  39.  
            }
  40.  
        ]
  41.  
    }


第三步:
[⇧⌘P]打开命令模式,选择[Debug: Open launch.json]命令,选择的模板为C/C++,回车后会自动在.vscode目录下生成一个launch.json文件,下面给出我的文件示例:

  1.  
    {
  2.  
        // Use IntelliSense to learn about possible attributes.
  3.  
        // Hover to view descriptions of existing attributes.
  4.  
        // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  5.  
        "version": "0.2.0",
  6.  
        "configurations": [
  7.  
            {
  8.  
                "name": "c/c++ Launch",
  9.  
                "type": "cppdbg",
  10.  
                "request": "launch",
  11.  
                "program": "${fileDirname}/${fileBasenameNoExtension}.out",
  12.  
                "args": [],
  13.  
                "stopAtEntry": false,
  14.  
                "cwd": "${workspaceFolder}",
  15.  
                "environment": [],
  16.  
                "externalConsole": true,
  17.  
                "MIMode": "lldb",
  18.  
                "preLaunchTask": "build c++",
  19.  
                "logging": {
  20.  
                    "trace": true,
  21.  
                    "traceResponse": true,
  22.  
                    "engineLogging": true
  23.  
                }
  24.  
            }
  25.  
        ]
  26.  
    }


完成这三步C++开发环境就配置好了,接下来就可以编译,运行,调试C++程序了

[⇧⌘B]是编译程序,[⇧⌘R]是运行程序,如果安装了插件『Code Runner』可以直接运行程序

如果需要调试,那就按F5,进入调试模式即可

好了,这样就可以顺利地在vscode上进行C++开发啦,赶快行动起来吧~

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们