CMake tasks.json launch.json

发布时间:2024年01月14日
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
{
    // 使用 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
{
    "version": "2.0.0",
    "options": {
        "cwd": "${workspaceFolder}/build"
    },
    "tasks": [
        {
            "type":"shell",
            "label": "cmake",
            "command": "cmake",
            "args": [
                ".."
            ]
        },
        {
            "label": "make",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "command":"make",
            "args": [

            ]
        },
        {
            "label": "Build",
            "dependsOrder": "sequence",// 按列出的顺序执行任务依赖项
            "dependsOn":[
                "cmake",    
                "make"
            ] 
        }
    ]
}
  • settings.json
{
    "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$

文章来源:https://blog.csdn.net/weixin_41987016/article/details/135582209
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。