VSCode在linux下的实战

3 篇文章 0 订阅

源文件

Gun.h

#pragma once
#include <string>

class Gun
{
private:
    /* data */
    int bullet_count;
    std::string type;
public:
    Gun(std::string type){
        this->bullet_count = 0;
        this->type = type;
    }

    void addBullet(int num);
    void shoot();
    ~Gun();
};

Gun.cpp

#include "../include/Gun.h"
#include <iostream>

void Gun::addBullet(int num){
    this->bullet_count = num;
}

void Gun::shoot(){
    if(this->bullet_count<=0){
        std::cout<<"there is no bullet, please add bullet" <<std::endl;
    }else{
        this->bullet_count--;
        std::cout<<"shoot"<<std::endl;
    }
}

Gun::~Gun(){ 
}

Soldier.h

#pragma once
#include <string>
#include "Gun.h"

class Soldier
{
private:
    /* data */
    std::string name;
    Gun* ptr_gun;
public:
    Soldier(std::string name);
    ~Soldier();
    void addBulletToGun(int num);
    void fire();
    void addGun(Gun* gun);
};

Soldier.cpp

#include <iostream>
#include "../include/Soldier.h"
// #include "Gun.h"

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

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

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

void Soldier::fire(){
    this->ptr_gun->shoot();
}

Soldier::~Soldier()
{
    if(this->ptr_gun == nullptr){
        return;
    }else{
        delete this->ptr_gun;
        this->ptr_gun=nullptr;
    }
}

CMakeLists编辑

cmake_minimum_required(VERSION 3.0)

project(SOLIDER)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O2 -Wall")

include_directories(include)

add_executable(my_cmake_exe main.cpp src/Gun.cpp src/Soldier.cpp)
  • CMakeLists.txt常见必选项
  • cmake_minimum_required(VERSION 3.0)
    • 设置CMAKE最低支持的版本
  • project(SOLIDER)
    • 设置项目名称
  • set(CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS} -g -O2 -Wall”)
    • 设置编译时的选项,-g最后的执行文件可以debug; -O2代码优化; -Wall 显示所有警告
  • include_directories(include)
    • 将include文件夹内容可以被识别到
  • add_executable(my_cmake_exe main.cpp src/Gun.cpp src/Soldier.cpp)
    • 相当于执行g++ main.cpp src/Gun.cpp src/Soldier.cpp -o my_cmake_exe

**上述set设置可能在debug时会有冲突,可进行以下修改
**

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}  -Wall")
set(CMAKE_BUILD_TYPER Debug)

配置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++ - 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/my_cmake_exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "Build",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

主要修改模板中的

# 可执行文件
 "program": "${workspaceFolder}/build/my_cmake_exe"
 # 详细编译配置在task.json中设置
  "preLaunchTask": "Build",

task.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"
            ]
        }
    ]

}

相当于自动运行cmake …
make

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值