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

一、装置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

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理