659441806 发表于 2017-6-29 08:11:26

windows下使用vs code调试简单的C程序

  常使用visual studio code(vs code)打开.c文件,如果让vs code具备调试技能估计会比较有用

准备工作:
  1. vs code安装插件:cpptools
  2. windows安装MinGW,然后配置MinGW,需要的工具有gcc,g++,gdb,最后将MinGW的路径添加到path系统环境变量

写个hello world测试一下(首先需要打开文件夹):

1. 源程序:test.c



#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a = 1;
int b;
printf("Hello vs code!\n");
b = a;
printf("b = %d\n", b);
system("pause");
return 0;
}
2. 配置tasks.json文件
  使用ctrl+shift+p调出命令对话框,输入:tasks,选择:configure task runner,继续选择:others,即可产生tasks.json文件,改为如下内容:



1 {
2   // See https://go.microsoft.com/fwlink/?LinkId=733558
3   // for the documentation about the tasks.json format
4   "version": "0.1.0",
5   "command": "gcc",
6   "isShellCommand": true,
7   "args": ["-g", "${file}", "-o", "${file}.exe"],
8   "showOutput": "always"
9 }
  第5行:命令为gcc
  第7行:命令gcc的输入参数,-g表示输出调试信息,-o后接输出文件
  这个task即是用gcc编译源程序了:gcc -g test.c -o test.c.exe
  到这里就可以使用ctrl+shift+b来build源程序了,切换到test.c页面下,使用快捷键即可编译出test.c.exe文件
  顺便打开下方的“终端”选项卡,输入“.\test.c.exe”来运行

3. 配置launch.json文件
  切换到test.c页面,按下F5,在“选择环境”对话框中输入GDB,即选择“C++ (GDB/LLDB)”,产生launch.json文件
  将“program”的值改为:



"program": "${file}.exe",
  顺便在下面加上一行:



"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
  这里的miDebuggerPath即为gdb的安装路径。
  切换到test.c页面,按F5即可开始调试
  也可使用windows的编译工具,参见MSDN。不过我的output选项卡窗口中文都出现了乱码
页: [1]
查看完整版本: windows下使用vs code调试简单的C程序