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

相关文章

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

解决未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4‘问题

《解决未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4‘问题》:本文主要介绍解决未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4... 目录未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4‘打开pom.XM

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

Springboot3+将ID转为JSON字符串的详细配置方案

《Springboot3+将ID转为JSON字符串的详细配置方案》:本文主要介绍纯后端实现Long/BigIntegerID转为JSON字符串的详细配置方案,s基于SpringBoot3+和Spr... 目录1. 添加依赖2. 全局 Jackson 配置3. 精准控制(可选)4. OpenAPI (Spri

MySQL JSON 查询中的对象与数组技巧及查询示例

《MySQLJSON查询中的对象与数组技巧及查询示例》MySQL中JSON对象和JSON数组查询的详细介绍及带有WHERE条件的查询示例,本文给大家介绍的非常详细,mysqljson查询示例相关知... 目录jsON 对象查询1. JSON_CONTAINS2. JSON_EXTRACT3. JSON_TA

Java中JSON格式反序列化为Map且保证存取顺序一致的问题

《Java中JSON格式反序列化为Map且保证存取顺序一致的问题》:本文主要介绍Java中JSON格式反序列化为Map且保证存取顺序一致的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未... 目录背景问题解决方法总结背景做项目涉及两个微服务之间传数据时,需要提供方将Map类型的数据序列化为co

使用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 对象互转