背景: 公司要用vs code 在win10下跑通代码单步调试。下面是具体的操作。
安装环境:win10,MinGW
下载地址
https://code.visualstudio.com/
安装之后需要install C/C++插件
打开vscode,按ctrl+p打开快速命令框,输入以下命令后等待
ext install cpptools
点击需要的插件 install
完成后,需要点击reload,使安装生效。Reload之后的效果:
2 安装编译/调试环境
这里使用的是codeblocks下的MinGW.
下载地址(不建议用win10自带的IE浏览器,这里用的qq浏览器):
https://sourceforge.net/projects/codeblocks/files/Binaries/17.12/Windows/
双击安装,一路next即可。
安装完成后会提示是否打开codeblocks,点击是,跳出编译器选择界面,可以看到已经安装好了gnu gcc compiler.
配置环境变量
在codeblocks的安装目录下,有g++编译器,C:\Program Files (x86)\CodeBlocks\MinGW\bin。把这个目录添加到环境变量里。
Hello world~
在D盘新建一个文件夹,命名为code.然后建一个文件,hello_world.c 打开VS Code,
File --> open folder --> 选中D:/code文件夹。出现如下界面。
选中code下方的区域,右键,新建文件hello.cpp。
Hello.cpp与hello.c都可以试着跑一下,代码如下:
Hello.cpp
#include <iostream>
#include <stdlib.h>
int main()
{
std::cout<< "hello world !\n";
system("pause");
return 0;
}
Hello.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("hello world !\n");
system("pause");
return 0;
}
配置.json文件
点击左侧第4个按钮(调试按钮),再点击工具栏带红点的齿轮按钮,选择c++(GDB/LLDB)
会在工作目录下生成一个launch.json的启动配置文件,将内容替换为以下内容:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:\\Program Files (x86)\\CodeBlocks\\MinGW/bin\\gdb32.exe",// miDebugger的路径,注意这里要与MinGw的路径对应
"preLaunchTask": "g++", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
注意:"miDebuggerPath"中的斜杠最好用\\,否则会出现链接问题。
D:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../mingw32/bin/ld.exe:d:\code\.vscode\launch.json: file format not recognized; treating as linker script
D:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../mingw32/bin/ld.exe:d:\code\.vscode\launch.json:1: syntax error
collect2.exe: error: ld returned 1 exit status
The terminal process terminated with exit code: 1
在.vscode文件夹中再创建一个文件:tasks.json,将以下内容copy到文件里。
{
"version": "2.0.0",
"command": "g++",
"args": ["-g","${file}","-o","${fileBasenameNoExtension}.exe"], // 编译命令参数
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceRoot}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
确保2个.json文件在.vscode文件夹下面。否则,会出现could not find the task 'g++' 的编译问题。
左侧第一个按钮是查看打开的文件,第4个按钮是调试代码用的。
调试时,先点第4个按钮,再点击绿色的箭头按钮,就可以看到有打印数据出现。
多文件编译方法:
需要修改tasks.json的args
"args": ["-g","basefunction.c","${file}","-o","${fileBasenameNoExtension}.exe"],
参考文档:
https://blog.csdn.net/bat67/article/details/81268581