vscode 写个 c 程序

vscode 写个 c 程序

安装编译器

  1. 解压 mingw64 到想要的目录,如 D:\c\win\mingw64\bin
  2. 添加 D:\c\win\mingw64\bin 到环境变量

写个 c 程序

程序源码

#include <stdio.h>
#include <stdlib.h>

int main(void){
    int i = 0;
    for(i = 0; i < 10; i++){
        printf("hello world == %d\n",i);
    }
}

用到插件c/c++

  1. 安装, ctrl+shift+x 搜索 c/c++ 并安装,支持智能提示\调试\代码浏览
  2. 配置, ctrl+shif+p 搜索 c/c++ 选择 Edit Configurations(JSON) 并编辑,如下

c_pp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "D:/C/win/mingw64/x86_64-w64-mingw32/include/**" //头文件搜索路径,**表示递归
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "8.1",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64",                        //配合编译器
            "compilerPath": "D:/C/win/mingw64/bin/gcc"            //编译器路径名
        }
    ],
    "version": 4
}

编译

  • 直接用命令编译, gcc -g -o helloworld.exe helloworld.c
  • 配置task来编译, ctrl+shif+p 搜索 tasks 选择 Configure Default Build Task 并编辑,如下

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "gcc",       //gcc
            "args": [               //参数
                "-g",
                "-o",
                "helloworld.exe",
                "helloworld.c"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": [
                "$msCompile"
            ]
        }
    ]
}

ctrl + shift + b 编译

调试

  1. 配置,按 f5, 选c++(gdb/lldb), 选Default Configure,生成默认配置文件
  2. 执行调试,设置断点,按f5

launch.json

{
    "configurations": [
        {
            "name": "(gdb) Launch",                             //cmd,powershell lanch
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/helloworld.exe",     //要调试的程序的路径
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",                        //要调试的程序的目录
            "environment": [],
            "externalConsole": false,                           //使集成终端,容易观察调试输出
            "MIMode": "gdb",
            "miDebuggerPath": "C:/win/gnu/mingw64/bin/gdb.exe",             //编译器路径
            // "miDebuggerPath": "C:\\win\\gnu\\mingw64\\bin\\gdb.exe",     //ok
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

因为 vscode 会将上面的调试配置转成 cmd 格式的命令(只能在 powershell/cmd 中运行),可以通过菜单help->open process explorer 查看,如下

"C:\Users\sfuwe\.vscode\extensions\ms-vscode.cpptools-0.23.0-insiders\debugAdapters\bin\WindowsDebugLauncher.exe"
--stdin=Microsoft-MIEngine-In-oj45wsvs.dcm --stdout=Microsoft-MIEngine-Out-axvtgnnf.053 --stderr=Microsoft-MIEngine-Error-whwum2ps.z5j
--pid=Microsoft-MIEngine-Pid-naoc4b3s.mka --dbgExe=D:/C/win/mingw64/bin/gdb.exe --interpreter=mi

而且 "externalConsole": false 表示要使用 vscode 集成的终端,所以这里集成终端必须选 window 系的 powershell 或者 cmd 不能是 bash

ctrl + ,编辑设置文件,如下
settings.json

    "terminal.integrated.shell.windows":
        "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe", //ok
        //"C:\\Windows\\System32\\cmd.exe"          //ok
        //"C:\\Windows\\System32\\bash.exe"             //no!!!

参考

https://code.visualstudio.com/docs

猜你喜欢

转载自blog.csdn.net/u011714033/article/details/89575051