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

相关文章

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

如何自定义Nginx JSON日志格式配置

《如何自定义NginxJSON日志格式配置》Nginx作为最流行的Web服务器之一,其灵活的日志配置能力允许我们根据需求定制日志格式,本文将详细介绍如何配置Nginx以JSON格式记录访问日志,这种... 目录前言为什么选择jsON格式日志?配置步骤详解1. 安装Nginx服务2. 自定义JSON日志格式各

python dict转换成json格式的实现

《pythondict转换成json格式的实现》本文主要介绍了pythondict转换成json格式的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下... 一开始你变成字典格式data = [ { 'a' : 1, 'b' : 2, 'c编程' : 3,

Java实现XML与JSON的互相转换详解

《Java实现XML与JSON的互相转换详解》这篇文章主要为大家详细介绍了如何使用Java实现XML与JSON的互相转换,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. XML转jsON1.1 代码目的1.2 代码实现2. JSON转XML3. JSON转XML并输出成指定的

Jackson库进行JSON 序列化时遇到了无限递归(Infinite Recursion)的问题及解决方案

《Jackson库进行JSON序列化时遇到了无限递归(InfiniteRecursion)的问题及解决方案》使用Jackson库进行JSON序列化时遇到了无限递归(InfiniteRecursi... 目录解决方案‌1. 使用 @jsonIgnore 忽略一个方向的引用2. 使用 @JsonManagedR

Pycharm安装报错:Cannot detect a launch configuration解决办法

《Pycharm安装报错:Cannotdetectalaunchconfiguration解决办法》本文主要介绍了Pycharm安装报错:Cannotdetectalaunchconfigur... 本文主要介绍了Pycharm安装报错:Cannot detect a launch configuratio

SpringBoot接收JSON类型的参数方式

《SpringBoot接收JSON类型的参数方式》:本文主要介绍SpringBoot接收JSON类型的参数方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、jsON二、代码准备三、Apifox操作总结一、JSON在学习前端技术时,我们有讲到过JSON,而在

基于.NET编写工具类解决JSON乱码问题

《基于.NET编写工具类解决JSON乱码问题》在开发过程中,我们经常会遇到JSON数据处理的问题,尤其是在数据传输和解析过程中,很容易出现编码错误导致的乱码问题,下面我们就来编写一个.NET工具类来解... 目录问题背景核心原理工具类实现使用示例总结在开发过程中,我们经常会遇到jsON数据处理的问题,尤其是

Java对象和JSON字符串之间的转换方法(全网最清晰)

《Java对象和JSON字符串之间的转换方法(全网最清晰)》:本文主要介绍如何在Java中使用Jackson库将对象转换为JSON字符串,并提供了一个简单的工具类示例,该工具类支持基本的转换功能,... 目录前言1. 引入 Jackson 依赖2. 创建 jsON 工具类3. 使用示例转换 Java 对象为