关于c++17:Win10用VS-Code编写C程序

10次阅读

共计 2021 个字符,预计需要花费 6 分钟才能阅读完成。

一、装置 C /C++ 编译器

macOS 和支流 Linux 零碎都自带 C /C++ 编译器,Windows 零碎须要通过装置 mingw 来取得 C /C++ 编译器
(1)mingw 下载地址:https://sourceforge.net/proje…

(2)装置完 mingw 后,将装置目录增加到用户变量和零碎变量中的 Path 后。bin 目录下的文件列表如图:

二、验证 C /C++ 开发环境

关上命令行,输出 gcc –verion 和 g ++ –version。如果呈现以下输入证实开发环境装置实现:

三、在 VS Code 中装置 C ++ 相干插件

关上 VS Code,装置以下插件:
C/C++ 插件,由微软官网开发保护,提供了丰盛的 C 和 C ++ 的开发反对
Code Runner 插件:一键运行代码,反对 40 多种编程语言
CodeLLDB 插件:一款用于调试 C ++ 的原生插件
因为网络问题,CodeLLDB 可能无奈在线装置,能够下载 CodeLLDB 对应的 vsix 文件进行离线装置

四、进入调试界面增加配置环境

(1)写代码的文件夹下创立了一个.vscode 文件夹,.vscode 有三个文件,内容如下:

(2)首次调试时,需增加配置环境,抉择 C ++(GDB/LLDB),会主动生成 launch.json 配置文件

(3)编辑 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": "g++.exe 生成和调试流动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\myMinGW\\minGW\\bin\\gdb.exe", // minGW 的装置地址
            "setupCommands": [
                {
                    "description": "为 gdb 启用参差打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "task g++" // 批改项
        }
    ]
}

(4)返回 cpp 文件,按 F5 进行调试,会弹出:找不到工作“task g++”;这是抉择配置工作,会主动生成 tasks.json。编辑 tasks.json 文件

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "task g++",
            "type": "shell",
            "command": "D:\\myMinGW\\minGW\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {"cwd": "D:\\myMinGW\\minGW\\bin"},
            "problemMatcher": ["$gcc"],
            "group": "build"
        }
    ]
}

留神,launch.json 中 preLaunchTask 的值必须和 tasks.json 文件中 label 的值一样

(5)c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": ["${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "D:\\myMinGW\\minGW\\bin\\g++.exe", // 替换为理论装置目录
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}

五、调试和运行胜利

留神

如果调试运行时零碎报错:g++ : 无奈将“g++”项辨认为 cmdlet;则 可抉择以管理员身份启动 VS Code

正文完
 0