欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Windows10下使用Visual Studio Code编译C代码

程序员文章站 2022-06-17 08:45:43
...

第一步。
安装MinGW,并且配置MinGW变量使得在cmd下输入gcc -v能够打印出版本信息出来
第二步安装Visual Studio Code,并且安装相应的插件
编辑一下几个文件

hello.c
#include<stdio.h>
int main(int argc, char const *argv[])
{
   printf("hello.word!");
   system("pause");
    return 0;
}

.vscode\launch.json
{
    "version": "0.2.0",
    "configurations": [

        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/hello.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "echo"
        }
    ]
}

.vscode\tasks.json
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "gcc",
            "args": [
                "-g",
                "hello.c",
                "-o",
                "hello.exe"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
        }
    ]
}

其中tasks中写的就是编译命令上述表示在cmd执行gcc -g hello.c -o hello.exe,而launch则表示用于调试,并且调试工具是gdb,通过preLaunchTask指明了是哪个编译的tasks(通过tasks中的label指明)