CMake tasks.json launch.json

 

heheda@linux:~/Linux/cmake/cmakeClass$ tree
.
├── CMakeLists.txt
├── include
│   ├── Gun.h
│   └── Soldier.h
├── main.cpp
└── src
    ├── Gun.cpp
    └── Soldier.cpp

2 directories, 6 files
heheda@linux:~/Linux/cmake/cmakeClass$ 

  • launch.json(在.vscode文件夹中)
{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) 启动",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/bin/app",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
            ],
            "preLaunchTask": "Build",
            // "miDebuggerPath": "/usr/bin/gdb"
        },
    ]
}
  • tasks.json(在.vscode文件夹中)
{
    "version": "2.0.0",
    "options": {
        "cwd": "${workspaceFolder}/build"
    },
    "tasks": [
        {
            "type": "shell",
            "label": "cmake",
            "command": "cmake",
            "args": [
                ".."
            ]
        },
        {
            "label": "make",
            "group": "build",
            "command": "make",
            "args": [],
            "problemMatcher": []
        },
        {
            "label": "Build",
            "dependsOrder": "sequence",
            "dependsOn": [
                "cmake",
                "make"
            ]
        },
        {
            "type": "cppbuild",
            "label": "C/C++: g++ 生成活动文件",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${workspaceFolder}/bin/app"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "编译器: /usr/bin/g++"
        }
    ]
}
  • settings.json(在.vscode文件夹中)
{
    "files.associations": {
        "ostream": "cpp",
        "array": "cpp",
        "atomic": "cpp",
        "*.tcc": "cpp",
        "cctype": "cpp",
        "clocale": "cpp",
        "cmath": "cpp",
        "cstdarg": "cpp",
        "cstddef": "cpp",
        "cstdint": "cpp",
        "cstdio": "cpp",
        "cstdlib": "cpp",
        "cwchar": "cpp",
        "cwctype": "cpp",
        "deque": "cpp",
        "unordered_map": "cpp",
        "vector": "cpp",
        "exception": "cpp",
        "algorithm": "cpp",
        "memory": "cpp",
        "memory_resource": "cpp",
        "optional": "cpp",
        "string": "cpp",
        "string_view": "cpp",
        "system_error": "cpp",
        "tuple": "cpp",
        "type_traits": "cpp",
        "utility": "cpp",
        "fstream": "cpp",
        "initializer_list": "cpp",
        "iosfwd": "cpp",
        "iostream": "cpp",
        "istream": "cpp",
        "limits": "cpp",
        "new": "cpp",
        "sstream": "cpp",
        "stdexcept": "cpp",
        "streambuf": "cpp",
        "typeinfo": "cpp",
        "head.h": "c"
    }
}
  • Gun.h 
#pragma once  //防止头文件重复包含
#include<string>
class Gun
{
public:
       //构造函数初始化,构造函数直接在头文件中实现
       Gun(std:: string type){
           this->_bullet_count=0;
           this->_type=type;
       }
       //装填子弹
       void addBullet(int bullet_num); 
       //发射子弹的接口
      bool shoot();
 
private:
        int   _bullet_count;
        std:: string _type;   
};
  • Gun.cpp
//.cpp文件写接口函数的实现
#include "Gun.h"
#include<iostream>
using namespace std; 
void Gun::addBullet(int bullet_num)
{
        this-> _bullet_count+=bullet_num;
 
}
bool  Gun::shoot()
{
        if(_bullet_count<=0)
        {
            cout<<" here is no bullet!"<<endl;
            return  false;  //直接返回,程序结束
        }
        cout<<" fire work"<<endl;
        this->_bullet_count-=1;
 
        return true;
 
 
}
  • Soldier.h
#pragma once
#include <string>
#include "Gun.h"
class Soldier
{
public:
    Soldier(std::string name); //构造函数不在头文件中实现
    //因为定义了指针,故构造析构函数
    ~Soldier();
    //为士兵添加一把枪
    void addGun(Gun* ptr_gun );
    //为枪增加子弹的接口
    void addBulletToGun(int num);
    bool fire();
 
private:
    std::string _name; //头文件中必须写std
    Gun *_ptr_gun;
};
  • Soldier.cpp
#include "Soldier.h"

Soldier::Soldier(std::string name) {
    this->_name = name;
    this->_ptr_gun = nullptr;
}

void Soldier::addGun(Gun* ptr_gun) {
    this->_ptr_gun = ptr_gun;
}

void Soldier::addBulletToGun(int num) {
    this->_ptr_gun->addBullet(num);
} 

bool Soldier::fire() {
    return this->_ptr_gun->shoot();
}

Soldier::~Soldier() {
    if(this->_ptr_gun == nullptr) {
       return; 
    }
    delete this->_ptr_gun;
    this->_ptr_gun = nullptr; // 防止野指针
}
  • CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(SOLDIERFIRE)
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O2 -Wall")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
set(CMAKE_CXX_STANDARD 11) # 设置C++标准为C++11(-std=c++11)
set(CMAKE_BUILD_TYPE "Debug")
include_directories(${CMAKE_SOURCE_DIR}/include)
aux_source_directory(${PROJECT_SOURCE_DIR}/src SRC_LIST) # 选择src文件夹下面的所有文件
add_executable(app main.cpp ${SRC_LIST})

# 指定输出的路径
set(HOME ${PROJECT_SOURCE_DIR}) # 定义一个变量用于存储一个绝对路径
set(EXECUTABLE_OUTPUT_PATH ${HOME}/bin) # 将拼接好的路径值设置给 EXECUTABLE_OUTPUT_PATH 变量
  • main.cpp
#include "Gun.h"
#include "Soldier.h"
#include <iostream>
//测试函数, 实现一些测试功能
void test()
{
    Soldier sandu("xusanduo");  //实例化Soldier类
    sandu.addGun(new Gun("AK47")); //要传入一个gun,需要先创建一个出来,也就是new一个
    sandu.addBulletToGun(20);
    sandu.fire( );
}
int main( )
{
    test( );
    auto a = 8;
    std::cout << a + 10 << std::endl;
    return 0;
}

执行结果:

heheda@linux:~/Linux/cmake/cmakeClass$ mkdir build
heheda@linux:~/Linux/cmake/cmakeClass$ cd build/
heheda@linux:~/Linux/cmake/cmakeClass/build$ cmake ..
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/heheda/Linux/cmake/cmakeClass/build
heheda@linux:~/Linux/cmake/cmakeClass/build$ make
Scanning dependencies of target app
[ 25%] Building CXX object CMakeFiles/app.dir/main.cpp.o
[ 50%] Building CXX object CMakeFiles/app.dir/src/Gun.cpp.o
[ 75%] Building CXX object CMakeFiles/app.dir/src/Soldier.cpp.o
[100%] Linking CXX executable ../bin/app
[100%] Built target app
heheda@linux:~/Linux/cmake/cmakeClass/build$

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/327590.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

linux主机的免密登录

实现linux主机之间的相互免密登录 在进行远程登录的时&#xff0c;服务器和主机间进行认证阶段分为&#xff1a; 基于口令认证&#xff08;不安全&#xff0c;易被抓包拦截获取&#xff09; 客户机连接服务器时&#xff0c;服务器将自己的公钥返回给客户机 客户机会将服务器的…

【报错】NVIDIA 驱动版本不兼容 — NVIDIA driver on your system is too old

【报错】NVIDIA 驱动版本不兼容 — NVIDIA driver on your system is too old 报错信息查看torch版本查看nvidia驱动版本 报错信息 CUDA initialization: The NVIDIA driver on your system is too old (found version 11040). Please update your GPU driver by downloading …

29 旋转工具箱

效果演示 实现了一个菜单按钮的动画效果&#xff0c;当鼠标悬停在菜单按钮上时&#xff0c;菜单按钮会旋转315度&#xff0c;菜单按钮旋转的同时&#xff0c;菜单按钮旋转的8个小圆圈也会依次旋转360度&#xff0c;并且每个小圆圈的旋转方向和菜单按钮的旋转方向相反&#xff0…

【SpringMVC】常用注解(续)

在SpringMVC常用注解一文中&#xff0c;已经对一些基本注解&#xff08;有Controller、RequestMapping、ResponseBody、RequestParam&#xff09;进行了简单介绍&#xff1b;在此篇文章中&#xff0c;将继续对剩余的几个常用注解进行简单介绍&#xff0c;有RequestBody、PathVa…

ElasticSearch入门篇

目录 一、 ElasticSearch的定位 二、 什么是倒排索引 三、 什么是全文检索 四、 ElasticSearch的数据存储原理 4.1 ElasticSearch与关系型数据库的数据结构对比 4.2 ElasticSearch的倒排索引原理 一、 ElasticSearch的定位 ElasticSearch是一款开源的分布式 搜索和…

力扣算法之滑动窗口题目--水果成篮

文章目录 题目解析不同之处解决办法解决图示 代码 题目解析 首先我们先看一下题目如下图所示 题目意思也比较容易理解其实就是你有一个篮子这个篮子只能装两个不同种类的水果&#xff0c;问你最多能装多少个水果&#xff0c;这里还贴心的弄了一个样列&#xff0c;121 可以看出…

计算机组成原理 运输层

文章目录 运输层运输层协议概述进程之间的通信运输层的两个主要协议运输层的端口 用户数据报协议 UDPUDP 概述UDP 的首部格式 传输控制协议 TCP 概述TCP 最主要的特点TCP 的连接 可靠传输的工作原理停止等待协议连续 ARQ协议 TCP 报文段的首部格式TCP 可靠传输的实现以字节为单…

tcpdump常用命令

tcp首部解析&#xff1a; tcp-首部_tcp首部-CSDN博客 ref&#xff1a; Home | TCPDUMP & LIBPCAP https://www.cnblogs.com/onlyforcloud/p/4396126.html tcpdump 详细使用指南&#xff08;请尽情食用&#xff09;_tcpdump指定ip和端口-CSDN博客 【博客192】抓取报文查…

【深度学习目标检测】十五、基于深度学习的口罩检测系统-含GUI和源码(python,yolov8)

YOLOv8是一种物体检测算法&#xff0c;是YOLO系列算法的最新版本。 YOLO&#xff08;You Only Look Once&#xff09;是一种实时物体检测算法&#xff0c;其优势在于快速且准确的检测结果。YOLOv8在之前的版本基础上进行了一系列改进和优化&#xff0c;提高了检测速度和准确性。…

八、K8S metrics-server

下载yaml文件 wget https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/high-availability.yaml 改名&#xff1a;mv high-availability.yaml metrics-server.yaml 查看镜像地址 查看镜像地址 grep -rn image high-availability.yaml 150: …

【人工智能平台】ubuntu22.04.3部署cube-studio

简介&#xff1a;本次安装是在虚拟机上进行&#xff0c;需要给虚拟机至少分配16GB&#xff0c;分配8GB时系统会卡死。 一、环境&#xff1a; 主机环境&#xff1a;win11&#xff08;全程科学&#xff09;vm虚拟机 虚拟机&#xff1a;ubuntu22.04.3桌面版&#xff08;新装&…

Ventoy:打造你的万能启动 U 盘 | 开源日报 No.146

ventoy/Ventoy Stars: 54.3k License: GPL-3.0 Ventoy 是一个开源工具&#xff0c;用于创建支持 ISO/WIM/IMG/VHD(x)/EFI 文件的可启动 USB 驱动器。其主要功能包括将镜像文件复制到 USB 驱动器并进行引导、一次性复制多个镜像文件并提供引导菜单选择以及在本地磁盘中浏览和引…

基于SSM的高校班级同学录网站的设计与实现

末尾获取源码 开发语言&#xff1a;Java Java开发工具&#xff1a;JDK1.8 后端框架&#xff1a;SSM 前端&#xff1a;采用JSP技术开发 数据库&#xff1a;MySQL5.7和Navicat管理工具结合 服务器&#xff1a;Tomcat8.5 开发软件&#xff1a;IDEA / Eclipse 是否Maven项目&#x…

Vue组件之间的通信方式都有哪些?

面试官&#xff1a;Vue组件之间的通信方式都有哪些&#xff1f; 一、组件间通信的概念 开始之前&#xff0c;我们把组件间通信这个词进行拆分 组件通信 都知道组件是vue最强大的功能之一&#xff0c;vue中每一个.vue我们都可以视之为一个组件通信指的是发送者通过某种媒体以…

C#灵活的任务调度组件FluentScheduler

FluentScheduler是一个C#的灵活的任务调度组件&#xff0c;支持各类任务调度。网上有很多演示代码&#xff0c;此处记录下来&#xff0c;方便自己查找。 // See https://aka.ms/new-console-template for more information //Console.WriteLine("Hello, World!");us…

Tortoise-orm 使用(一)

创建表 项目基于Vue3.0, FastAPI的模板管理系统&#xff0c;从网上找了各种资源去实践&#xff0c;现在将总结发出来&#xff0c;分享给大家&#xff0c;希望帮助大家少走些弯路。 准备工作 # tortoise-orm pip install tortoise-orm # MySQL pip install tortoise-orm[async…

数据库结构文档生成(通过PDMReader)

将数据库的表结构生成数据库结构文档有三种方法&#xff1a; 1、通过 PDMReader生成文档&#xff1b; 2、使用EZDML 工具生成&#xff08;下载地址&#xff1a;EZDML - 下载&#xff09;&#xff1b; 3、使用SCREW 插件&#xff0c;通过java代码生成。 本文章先介绍通过PDM…

ZABBIX根据IP列表,主机描述,或IP子网批量创建主机的维护任务

有时候被ZABBIX监控的主机可能需要关机重启等维护操作,为了在此期间不触发告警,需要创建主机的维护任务,以免出现误告警 ZABBIX本身有这个API可供调用(不同版本细节略有不同,本次用的ZABBIX6.*),实现批量化建立主机的维护任务 无论哪种方式(IP列表,主机描述,或IP子网)创建维护…

Cellinx NVT 摄像机 UAC.cgi 任意用户创建漏洞复现

0x01 产品简介 Cellinx NVT IP PTZ是韩国Cellinx公司的一个摄像机设备。 0x02 漏洞概述 Cellinx NVT 摄像机 UAC.cgi接口处存在任意用户创建漏洞,未经身份认证的攻击者可利用此接口创建管理员账户,登录后台可查看敏感信息,使系统处于极不安全的状态。 0x03 复现环境 FO…

Nas群晖中搭建自己的图片库

1、在套件中心下载synology phtotos 2、点击打开&#xff0c;右上角头像设置中配置 3、这样子就是已经完成了&#xff0c;可以把你的图片进行上传 4、嫌弃上传麻烦的&#xff0c;可以直接去根目录复制粘贴 5、访问 这样子就可以直接访问了
最新文章