如何使用vscode运行和调试c/c++程序

众所周知

vscode是个万金油,而且体型轻巧,拓展插件多,非常适合初学者编程

那么如何使用vscode进行c/c++程序的运行?

首先必须确保mingw64正确安装

通过以下链接下载解压到c盘根目录即可,然后添加修改path:“C:\mingw64\bin”

然后win+r 输入cmd 回车 输入gcc -v 出现版本号即代表path设置正确

mingw64下载,C语言学习,gcc-C/C++文档类资源-CSDN下载

然后打开vscode 安装插件code runner,c/c++这俩插件即可

然后打开文件-首选项-设置-搜索Run In Terminal,去勾选

然后搜索Code-runner: Executor Map,第一个选择在settings.json中编辑

"c": "chcp 65001 && cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"cpp": "chcp 65001 && cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",

 如果找不到code-runner.executorMap,请在最后一行手打输入code-runner.executorMap,打一半软件会提示然后莫名其妙自动补全然后出现下面的一堆语言设置,然后在c和cpp行换成这俩行代码,加了chcp 65001确保code runner不会中文乱码错误发生

设置好以后就可以正常使用code runner即实现c/c++在vscode中的运行

接下来实现c/c++在vscode中的调试实现

首先你需要在vscode中打开一个文件夹,然后创建一个test.c文件,然后创建个.vscode文件夹,在里面创建四个文件

c_cpp_properties.json:将这段代码复制进去

{
  "configurations": [
    {
      "name": "windows-gcc-x64",
      "includePath": [
        "${workspaceFolder}/**"
      ],
      "compilerPath": "C:/mingw64/bin/gcc.exe",
      "cStandard": "${default}",
      "cppStandard": "${default}",
      "intelliSenseMode": "windows-gcc-x64",
      "compilerArgs": []
    }
  ],
  "version": 4
}

launch.json:复制粘贴

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "(gdb) Launch",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": true,
      "internalConsoleOptions": "neverOpen",
      "MIMode": "gdb",
      "miDebuggerPath": "gdb.exe",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": false
        }
      ],
      "preLaunchTask": "Compile"
    },
    {
      "name": "C/C++ Runner: Debug Session",
      "type": "cppdbg",
      "request": "launch",
      "args": [
        ""
      ],
      "stopAtEntry": false,
      "cwd": "c:\\Users\\Rise\\Desktop\\c\\book",
      "environment": [],
      "program": "c:/Users/Rise/Desktop/c/book/build/Debug/outDebug",
      "internalConsoleOptions": "openOnSessionStart",
      "MIMode": "gdb",
      "miDebuggerPath": "gdb",
      "externalConsole": false,
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ]
    }
  ]
}

自行更改

"cwd": "c:\\Users\\Rise\\Desktop\\c\\book",

"program": "c:/Users/Rise/Desktop/c/book/build/Debug/outDebug",

改成自己的目录

tasks.json:复制粘贴



// https://code.visualstudio.com/docs/editor/tasks
{
    "version": "2.0.0",
    "tasks": [{
        "label": "Compile", // 任务名称,与launch.json的preLaunchTask相对应
        "command": "gcc",   // 要使用的编译器,C++用g++
        "args": [
            "${file}",
            "-o",    // 指定输出文件名,不加该参数则默认输出a.exe,Linux下默认a.out
            "${fileDirname}/${fileBasenameNoExtension}.exe",
            "-g",    // 生成和调试有关的信息
            "-m64",  // 不知为何有时会生成16位程序而无法运行,此条可强制生成64位的
            "-Wall", // 开启额外警告
            "-static-libgcc",     // 静态链接libgcc,一般都会加上
            "-fexec-charset=GBK", // 生成的程序使用GBK编码,不加这条会导致Win下输出中文乱码;繁体系统改成BIG5
            "-D__USE_MINGW_ANSI_STDIO", // 用MinGW写C时留着,否则不需要,用于支持printf的%zd和%Lf等
        ], // 编译的命令,其实相当于VSC帮你在终端中输了这些东西
        "type": "process", // process是把预定义变量和转义解析后直接全部传给command;shell相当于先打开shell再输入命令,所以args还会经过shell再解析一遍
        "group": {
            "kind": "build",
            "isDefault": true // 不为true时ctrl shift B就要手动选择了
        },
        "presentation": {
            "echo": true,
            "reveal": "always", // 执行任务时是否跳转到终端面板,可以为always,silent,never。具体参见VSC的文档,即使设为never,手动点进去还是可以看到
            "focus": false,     // 设为true后可以使执行task时焦点聚集在终端,但对编译C/C++来说,设为true没有意义
            "panel": "shared"   // 不同的文件的编译信息共享一个终端面板
        },
        "problemMatcher":"$gcc" // 捕捉编译时终端里的报错信息到问题面板中,修改代码后需要重新编译才会再次触发
        // 本来有Lint,再开problemMatcher就有双重报错,但MinGW的Lint效果实在太差了;用Clangd可以注释掉
    }]
}

setting.json复制粘贴

{
  "files.defaultLanguage": "c",
  "editor.formatOnType": true,
  "editor.suggest.snippetsPreventQuickSuggestions": false,
  "editor.acceptSuggestionOnEnter": "off",
  "code-runner.runInTerminal": true,
  "code-runner.executorMap": {
    "c": "gcc '$fileName' -o '$fileNameWithoutExt.exe' -Wall -O2 -m64 -lm -static-libgcc -fexec-charset=GBK -D__USE_MINGW_ANSI_STDIO && &'./$fileNameWithoutExt.exe'",
    "cpp": "g++ '$fileName' -o '$fileNameWithoutExt.exe' -Wall -O2 -m64 -static-libgcc -fexec-charset=GBK && &'./$fileNameWithoutExt.exe'"
  },
  "code-runner.saveFileBeforeRun": true,
  "code-runner.preserveFocus": true,
  "code-runner.clearPreviousOutput": false,
  "code-runner.ignoreSelection": true,
  "code-runner.fileDirectoryAsCwd": true,
  "C_Cpp.clang_format_sortIncludes": true,
  "C_Cpp_Runner.cCompilerPath": "C:/mingw64/bin/gcc.exe",
  "C_Cpp_Runner.cppCompilerPath": "C:/mingw64/bin/g++.exe",
  "C_Cpp_Runner.debuggerPath": "gdb",
  "C_Cpp_Runner.cStandard": "",
  "C_Cpp_Runner.cppStandard": "",
  "C_Cpp_Runner.msvcBatchPath": "",
  "C_Cpp_Runner.warnings": [
    "-Wall",
    "-Wextra",
    "-Wpedantic"
  ],
  "C_Cpp_Runner.enableWarnings": true,
  "C_Cpp_Runner.warningsAsError": false,
  "C_Cpp_Runner.compilerArgs": [],
  "C_Cpp_Runner.linkerArgs": [],
  "C_Cpp_Runner.includePaths": [],
  "C_Cpp_Runner.includeSearch": [
    "*",
    "**/*"
  ],
  "C_Cpp_Runner.excludeSearch": [
    "**/build",
    "**/build/**",
    "**/.*",
    "**/.*/**",
    "**/.vscode",
    "**/.vscode/**"
  ]
}

然后就可以在test.c中编辑代码按f5进行调试

上述gcc路径和工作区路径自行更改即可

  • 10
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值