B站连接
https://www.bilibili.com/video/BV13K411M78v/?spm_id_from=333.337.search-card.all.click&vd_source=a302e304b0aa60652c390b422ff81ab8
1、开发环境搭建
下载cmake和mingw64
高级系统设置
进入环境变量
path的环境变量对应可执行文件的搜索目录,由于之前已经配过环境变量,就没有加载现在下载的文件进来了。
下载以下三款插件到vscode
2、代码实践演练
单文件编写
vscode以文件夹为单位的工程文件
交换两个数据
生成带调试信息的可执行文件
g++ -g .\main.cpp -o my_single_swap
调试代码
多文件编写
main.cpp
#include <iostream>
using namespace std;
#include "swap_.h"
int main(int argc, char **argv)
{
int val1 = 10;
int val2 = 20;
cout<<" before swap: "<<endl;
cout<<" val1 = "<<val1<<endl;
cout<<" val2 = "<<val2<<endl;
swap(val1,val2);
cout<<" after swap: "<<endl;
cout<<" val1 = "<<val1<<endl;
cout<<" val2 = "<<val2<<endl;
}
swap_.cpp
#include "swap_.h"
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
swap_.h
void swap(int &a, int &b);
多文件编译
debug出错
launch.json文件
如果需要编译多个文件,那么需要修改json文件,如下配置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" : "g++.exe - 生成和调试活动文件" ,
"type": "cppdbg" ,
"request": "launch",
"program": "${workspaceFolder}/my_multi_swap.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
//"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
cmake
新建CMakeList.txt文件
指定MYSWAP作为工程名字
project(MYSWAP)
生成可执行文件,需要通过main.cpp和swap.cpp文件生成
add_executable(my_cmake_swap main.cpp swap.cpp)
在终端操作前需要进行配置,会自动创建build文件夹,配置操作如下图
ctrl+shift+p–>CMake:Configure
选择gcc编译器
生成build文件
终端操作
windows下的make与Linux下的make不同
配置launch.json文件,路径需要指向my_cmake_swap.exe
如果不需要自动创建build文件,可自己创建build文件
用mingw,不用MSVC
因此第一次需要使用
cmake -G "MinGW Makefiles" ..
配置launch.json和task.json文件
未完待续…20230725