本文介绍使用vscode配置C/C++开发环境
安装软件
- 安装cmake工具和mingw64
- 添加环境变量
- 在vscode中下载插件C/C++ v1.8.4,CMake, CMake Tools
编写代码
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
cout<<"hello world"<<endl;
return 0;
}
编译代码,在终端中输入
g++ .\main.cpp
vscode帮我们自动生成了a.exe 文件,终端中输入.\a.exe ,则执行了a.exe 文件,终端窗口中显示"hello world"
接下来我们生成可调试的文件,在终端中输入
g++ -g .\main.cpp -o myfistproj
-g 代表配置生成可调试的可执行文件,-o 设置可执行文件的名称
多文件编译代码
#include <iostream>
using namespace std;
void swap(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}
int main(int argc, char** argv)
{
int val_a = 1;
int val_b = 2;
swap(val_a,val_b);
cout<<"val_a= "<<val_a<<endl;
cout<<"val_b= "<<val_b<<endl;
return 0;
}
将代码分成两个文件
编译指令
g++ -g .\main.cpp .\swap.cpp -o multifiletest
修改json文件
{
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/multifiletest.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
}
]
}
这里注释掉了preLaunchTask ,手动修改了"program": "${fileDirname}/multifiletest.exe", 可执行文件的位置和名称,按下F5即可开启调试
使用CMake编译代码
新建一个CMakeLists.txt文件
project(MYSWAP)
add_executable(my_cmake_swap main.cpp swap.cpp)//第一个参数代表生成的可执行文件的名字,后面的参数是要编译的cpp文件
然后ctrl+shift+p打开Cmake的configure,选择编译器gcc 8.1.0,在终端中输入编译指令
cd .\build\
cmake ..
mingw32-make.exe
此时终端中会显示
S D:\professionalSkillsLearning\CodeTraining\MyVsCodeProj> cd .\build\ PS D:\professionalSkillsLearning\CodeTraining\MyVsCodeProj\build> cmake ..
-- Configuring done
-- Generating done
-- Build files have been written to: D:/professionalSkillsLearning/CodeTraining/MyVsCodeProj/build
PS D:\professionalSkillsLearning\CodeTraining\MyVsCodeProj\build> mingw32-make.exe
[100%] Built target my_cmake_swap
PS D:\professionalSkillsLearning\CodeTraining\MyVsCodeProj\build>
此时已经生成了my_cmake_swap 可执行文件,接下来需要通过json文件配置调试信息,同上修改program中可执行文件名的位置和名称
配置json文件
我们使用cmake编译时,主要使用的指令是cmake .. 和mingw32-make.exe 。考虑在debug时如果代码有修改,总是需要使用这两个命令去编译生成exe文件,比较麻烦。可以通过配置json文件,将这两个指令配置在json的preLaunchTask 任务中,那么代码重新修改之后,直接F5即可生成重新编译生成exe文件开始debug。
launch.json文件配置如下:
{
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/build/mytest.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "Build"
}
]
}
tasks.json文件配置如下
{
"version": "2.0.0",
"options": {
"cwd": "${workspaceFolder}/build"
},
"tasks": [
{
"type": "shell",
"label": "cmake",
"command": "cmake",
"args": [
".."
],
},
{
"label": "make",
"group": {
"kind": "build",
"isDefault": true
},
"command": "mingw32-make",
"args": [
],
},
{
"label": "Build",
"dependsOn":[
"cmake",
"make"
]
}
],
}
|