目录
一、开发环境搭建:
cmake百度网盘链接:
链接:
https://pan.baidu.com/s/1CFQnlU4AwrqAYiMYyGHdmw
提取码:ab7a
windows 下的GCC编译器mingw64.zip下载:
链接:
https://pan.baidu.com/s/1I47MAENXtD-cqjZcqKI_Jw
提取码:qbv9
1、下载以上2个工具,解压后,放到C:\Program Files文件夹下,然后将2个工具bin文件夹的完整路径设置到Path环境变量,然后点住shift键,鼠标右键,选择打开powershell窗口,分别输入 cmake 和 g++ 进行测试,正常的界面如下图所示:
2、安装 visual studio code 的插件
cmake 和 cmake tools 和 c/c++
二、代码项目实践
1、配置添加launch.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}\\build\\my_wap.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "Build"
}
]
}
2、配置添加tasks.json
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.exe",
"args": [
],
},
{
"label": "Build",
"dependsOn":[
"cmake",
"make"
]
}
],
}
3、在工程文件夹下,新建include和src文件夹,并在include文件夹下,建立以下2个头文件
Gun.h
#pragma once//防止多次包含头文件,建议每个头文件加此行
#include <string>
class Gun
{
public:
Gun(std::string tempType)
{
this->_gBulletCount=0;
this->_gType=tempType;
}
void AddBullet(int tempNumber);
bool Shoot();
private:
int _gBulletCount;
std::string _gType;
};
Soldier.h
#pragma once
#include <string>
#include "Gun.h"
class Soldier
{
public:
Soldier(std::string tempName);
~Soldier();
void AddGun(Gun* tempGun);
void AddBulletToGun(int tempNum);
bool Fire();
private:
std::string _gName;
Gun* _gPtrGun;
/* data */
};
4、在工程文件夹下,在src文件夹下,建立以下2个源文件
Gun.cpp
#include "Gun.h"
#include <iostream>
using namespace std;
void Gun::AddBullet(int tempNumber)
{
this->_gBulletCount+=tempNumber;
}
bool Gun::Shoot()
{
if (this->_gBulletCount<=0)
{
cout<<"there is no bullet!"<<endl;
return false;
/* code */
}
this->_gBulletCount -= 1;
cout<<"already shoot:"<<this->_gBulletCount<<endl;
return true;
}
Soldier.cpp
#include "Soldier.h"
Soldier::Soldier(std::string tempName)
{
this->_gName=tempName;
this->_gPtrGun=nullptr;
}
Soldier::~Soldier()
{
if(this->_gPtrGun==nullptr)
{
return;
}
delete this->_gPtrGun;
this->_gPtrGun=nullptr;
}
void Soldier::AddGun(Gun* tempGun)
{
this->_gPtrGun=tempGun;
}
void Soldier::AddBulletToGun(int tempNum)
{
this->_gPtrGun->AddBullet(tempNum);
}
bool Soldier::Fire()
{
return this->_gPtrGun->Shoot();
}
5、在工程文件夹的目录建立main.cpp
main.cpp
#include "Gun.h"
#include "Soldier.h"
void test()
{
Soldier sanduo("sanduo");
sanduo.AddGun(new Gun("AK47"));
sanduo.AddBulletToGun(20);
sanduo.Fire();
}
int main()
{
test();
return 0;
}
6、新建CMakeLists.txt文件
CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
PROJECT(my_multiple)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set( CMAKE_BUILD_TYPE "Debug")
include_directories(${CMAKE_SOURCE_DIR}/include)
set(SRC
src/Gun.cpp
src/Soldier.cpp)
add_executable(my_wap main.cpp ${SRC})
7、点击快捷键ctrl+shift+p 添加CMake:Configure后,继续选择GCC8.1编译器,工程文件夹下会自动生成build文件夹,可以执行以下命令进行测试:
8、确认launch.json tasks.json CMakeLists.txt这3个文件的各种包含路径没有错误后,ctrl+shift+b 自动执行tasks.json进行编译,点击任意键退出终端
9、F5按键运行,将先执行launch.json,再执行launch.json中的”preLaunchTask”: “Build”,调用执行tasks.json编译,最后执行调试
三、可能出现以下问题及解决方案:
1、工程的所有目录路径必须使用英文路径
2、如果更换工程目录,需要删除build文件夹,然后点击ctrl+shift+p 添加CMake:Configure后,继续选择GCC8.1编译器,再重新cmake .. 和make进行测试
3、不要将工程放在具有同步功能的网盘下,会出现不同进程调用同1个文件,而发生错误