WIN下vscode调试C/C++,从零开始生成helloworld项目【2018年6月29日】

最近迷上了vscode这款编译器,小巧美观,用起来也很顺手,最主要的是全平台,正好最近要上手作Linux C客户端,之前没接触过linux,先拿它先在WIN上练练手。html

这几天在网上找了很是多的教程,win老是配很差环境。(linux和win一块儿开始配的,感受linux环境比win好配多了,下一篇博文再把linux端的配置过程分享出来,这里先写win的)。linux

 

环境:c++

WIN10 64 专业版shell

vscode版本:1.24.1json

launch.json版本:0.2.0windows

tasks.json版本:2.0.0ui

mingw-w64版本:8.1.0spa

 

 

过程:.net

1、 安装vscode插件

 vscode官网下载安装包直接安装便可

 

2、 vscode内安装C/C++ 插件

vscode内按快捷组合键Ctrl+Shift+X(或如图第①步点击[拓展]按钮)打开拓展分页,在搜索栏输入" C ",查找到如图的第一个插件,安装并从新加载之。

3、 安装mingw-w64(具体安装与环境变量配置能够查看这里

mingw-w64官网下载64位的mingw-w64在线安装包(以在线包为例)或离线包(离线包直接解压出来就能用)

在线包:根据系统选择合适的安装包进行下载,选择在线安装器

下载完成后出现以下安装包

安装该包,在Setting 界面将Architecture选项改成x86_64,其余不变,选择合适的安装路径(默认或从新指定均可以,路径中不要有中文)

 

配置计算机环境变量如图(个人安装路径是D:\mingw64,所以环境变量这么加)

 

安装完成后打开控制台,分别输入   g++ --version   和 gcc --version  查看环境是否安装成功(是否有当前版本号)

 

4、重启电脑(这里看了其余不少博主的没有提到,我没有重启,后来vscode代码写出来跑了不少次提示没有找到g++命令,最后重启解决)

5、运行C++代码

打开vscode,选择或新建一个空文件夹目录打开做为项目目录,新建一个test.cpp文件,键入以下helloworld代码

#include <stdio.h>
int main(int argc, char *args[])
{
    int i, j;
    printf("hello world!\n");
    printf("argc:%d\nargv:\n", argc);
    for (i = 0; i < argc; i++)
    {
        printf("%d:%s\n", i, args[i]);
    }
    getchar();
    return 0;
}

按下F5,顶部或出现以下菜单,选择C++(GDB/LLDB)

系统自动在当前目录下建立.vscode文件夹,并在其中新建一个launch.json的模版文件以下:

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "enter program name, for example ${workspaceFolder}/a.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "/path/to/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

将该模版修改成以下(能够直接复制,并修改有注释的一段)

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "preLaunchTask": "build",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "D:/mingw64/bin/gdb.exe", // 这里修改GDB路径为安装的mingw64的bin下的gdb.exe路径
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }]
}

 

vscode中按下组合键Shift+Ctrl+P,在唤出的任务栏中键入>task,下拉找到并点击 Tasks:Configure Task(任务:配置任务)项,并在接下来的返回项中选择使用模版建立tasks.json文件

 

系统会自动在.vscode文件夹下建立一个tasks.json文件,自动生成的代码以下

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "msbuild",
            "args": [
                // Ask msbuild to generate full paths for file names.
                "/property:GenerateFullPaths=true",
                "/t:build"
            ],
            "group": "build",
            "presentation": {
                // Reveal the output only if unrecognized errors occur.
                "reveal": "silent"
            },
            // Use the standard MS compiler pattern to detect errors, warnings and infos
            "problemMatcher": "$msCompile"
        }
    ]
}

同理,将之修改成以下代码(可直接覆盖)

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared"
            },
            "windows": {
                "command": "g++",
                "args": [
                    "-ggdb",
                    "\"${file}\"",
                    "--std=c++11",
                    "-o",
                    "\"${fileDirname}\\${fileBasenameNoExtension}.exe\""
                ]
            }
        }
    ]
}

至此,环境配置完成,转到C++代码页,按下F5,根目录下出现.cpp文件同名的.exe文件,代码自动执行,完成。

        

 

6、运行C代码

仿照第五步,新建helloworld.c文件,键入或粘贴C语言的helloworld代码

#include <stdio.h>
#include <windows.h>
int main() {
    printf("hello world!\n");
    system("pause");
    return 0;
}

在.c页面内单击F5,稍候片刻出现同名.exe并自动执行,完成。

 

 

最后,感谢如下博主的博客进行参考

【VSCode】Windows下VSCode编译调试c/c++【更新 2018.03.27】

在Windows中安装MinGW-w64

相关文章
相关标签/搜索