CMake tasks.json launch.json

2024-01-14 16:20
文章标签 json launch tasks cmake

本文主要是介绍CMake tasks.json launch.json,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

heheda@linux:~/Linux/cmake/cmakeClass$ tree
.
├── CMakeLists.txt
├── include
│   ├── Gun.h
│   └── Soldier.h
├── main.cpp
└── src├── Gun.cpp└── Soldier.cpp2 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; //头文件中必须写stdGun *_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$

这篇关于CMake tasks.json launch.json的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/605780

相关文章

使用Java将实体类转换为JSON并输出到控制台的完整过程

《使用Java将实体类转换为JSON并输出到控制台的完整过程》在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用JSON格式,用Java将实体类转换为J... 在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用j

MySQL 中的 JSON 查询案例详解

《MySQL中的JSON查询案例详解》:本文主要介绍MySQL的JSON查询的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql 的 jsON 路径格式基本结构路径组件详解特殊语法元素实际示例简单路径复杂路径简写操作符注意MySQL 的 J

Spring Boot中JSON数值溢出问题从报错到优雅解决办法

《SpringBoot中JSON数值溢出问题从报错到优雅解决办法》:本文主要介绍SpringBoot中JSON数值溢出问题从报错到优雅的解决办法,通过修改字段类型为Long、添加全局异常处理和... 目录一、问题背景:为什么我的接口突然报错了?二、为什么会发生这个错误?1. Java 数据类型的“容量”限制

Spring 请求之传递 JSON 数据的操作方法

《Spring请求之传递JSON数据的操作方法》JSON就是一种数据格式,有自己的格式和语法,使用文本表示一个对象或数组的信息,因此JSON本质是字符串,主要负责在不同的语言中数据传递和交换,这... 目录jsON 概念JSON 语法JSON 的语法JSON 的两种结构JSON 字符串和 Java 对象互转

MySQL 中查询 VARCHAR 类型 JSON 数据的问题记录

《MySQL中查询VARCHAR类型JSON数据的问题记录》在数据库设计中,有时我们会将JSON数据存储在VARCHAR或TEXT类型字段中,本文将详细介绍如何在MySQL中有效查询存储为V... 目录一、问题背景二、mysql jsON 函数2.1 常用 JSON 函数三、查询示例3.1 基本查询3.2

使用Python将JSON,XML和YAML数据写入Excel文件

《使用Python将JSON,XML和YAML数据写入Excel文件》JSON、XML和YAML作为主流结构化数据格式,因其层次化表达能力和跨平台兼容性,已成为系统间数据交换的通用载体,本文将介绍如何... 目录如何使用python写入数据到Excel工作表用Python导入jsON数据到Excel工作表用

Python获取中国节假日数据记录入JSON文件

《Python获取中国节假日数据记录入JSON文件》项目系统内置的日历应用为了提升用户体验,特别设置了在调休日期显示“休”的UI图标功能,那么问题是这些调休数据从哪里来呢?我尝试一种更为智能的方法:P... 目录节假日数据获取存入jsON文件节假日数据读取封装完整代码项目系统内置的日历应用为了提升用户体验,

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

JSON Web Token在登陆中的使用过程

《JSONWebToken在登陆中的使用过程》:本文主要介绍JSONWebToken在登陆中的使用过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录JWT 介绍微服务架构中的 JWT 使用结合微服务网关的 JWT 验证1. 用户登录,生成 JWT2. 自定义过滤

Java利用JSONPath操作JSON数据的技术指南

《Java利用JSONPath操作JSON数据的技术指南》JSONPath是一种强大的工具,用于查询和操作JSON数据,类似于SQL的语法,它为处理复杂的JSON数据结构提供了简单且高效... 目录1、简述2、什么是 jsONPath?3、Java 示例3.1 基本查询3.2 过滤查询3.3 递归搜索3.4