Implementation of C/C + + development based on VSCode and CMake | Linux

Implementation of C/C + + development based on VSCode and CMake | Linux

Construction of development environment

Before installing the software, perform
sudo apt update update software installation package=
Package and install gcc g++ gbd with sudo apt install build essential DBG

To view the installation results:


Installing CMake

sudo apt install cmake

GCC compiler

Compilation process

  1. Preprocessing - pre processing / /. I file

  2. Compiling - compiling / /. S file

  3. Assembly - assembling / /. O file

  4. Linking - Linking // bin file

One step instead of four:

g + + important parameters

-g compile executable file with debugging information

-O [n] optimize source code

-L and - L specify library file | specify library file path

-I specifies the header file search directory

-Wall print warning message

-w turn off warning message

-std=c++11

-o specify the output file name

-D define macros

g + + command line compilation

File structure:

)

Direct compilation will make an error and does not contain a header file

Add - Iinlcude to add the header file directory
Simply compile and run:

Add parameters:

Generate library files and compile

Static library link

Dynamic library link

GDB debugging

Common debugging command parameters

Start debugging gdb [execilename], and enter the gdb debugging program, where execilename is the name of the executable file to be debugged


be careful:
1. Add - g when compiling the program before debugging with gdb: gcc -g main.c -o main
2. Enter: repeat the previous command

IDE-VSCode

install

Execute in sequence:
sudo apt update
sudo apt install software-properties-common apt-transport-https wget
wget -q https://packages.microsoft.com/keys/microsoft.asc -O- | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main"
sudo apt install code

Interface

Plug in installation

Shortcut key


Actual compilation and operation

High frequency technique

hello world

swap

CMake

CMake is a cross platform installation and compilation tool, which can describe the installation (compilation process) of all platforms with simple statements.
CMake has become the standard configuration for most C + + open source projects

Cross-Platform Devilopment

CMake:

Introduction to syntax features

Important instructions

cmake_minimum_required - specifies the minimum version requirement for CMake

cmake_minimum_required(VERSION versionNumber[FATAL_ERROR])

Project - defines the project name and can specify the project language

project(projectName [CXX][C][JAVA]

set - explicitly defined variable

set(VAR [VALUE][CACHE TYPE DOCSTRING ][FORVE])

include_directories - add multiple specific header file search paths to the project -- > equivalent to - I specified by G + +

include_directories([AFTER | BEFORE][SYSTEM] dir1 dir2 ...)

link_directories - add multiple specific library file search paths to the project -- > equivalent to - L specified by G + +

ink_directories(dir1 dir2)

add_library - generate library files

add_library(libname [SHARED | STATIC | MODULE][EXCLUDE_FEOM_ALL] source1 source2...)

add_compile_options - add compilation parameters

add_compile_options(< option >...)

add_executable - generate executable

add_executable(exename source1 source2...)

target_link_libraries - add shared libraries to target that need to be linked


Common variables



CMake compilation project


actual combat

Internal construction



External build


swap project

Complete project development


file structure

include directory into h file
Put the src directory into the cpp file
Put main.cpp into the current directory

code

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

#include "Gun.h"
#include "iostream"
void Gun::addBullet(int bullet_num){
    this->_bullet_count += bullet_num;
}

bool Gun::shoot(){
    if(this->_bullet_count > 0){
        --this->_bullet_count;
        std::cout << "Shoot successfully" << std::endl;
        return true;
    }
    else{
        std::cout << "There is no bullet" << std::endl;
        return false;
    }
}

Soldier.h

#pragma once
#include <string>
#include "Gun.h"
class Soldier
{
private:
    std::string _name;
    Gun *_ptr_gun;
    /* data */
public:
    Soldier(std::string name);
    ~Soldier();
    void addBulletToGun(int num);
    bool fire();
    void addGun(Gun* ptr_gun);
};

Soldier.cpp

#include "Soldier.h"
#include <iostream>
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;
}

main.cpp

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

void test()
{
    Solider sanduo("xusanduo");
    sanduo.addGun(new Gun("AK47"));
    sanduo.addBulletToGun(20);
    sanduo.fire();
}

int main()
{
    test();
    return 0;
}

Compile using g + +

Compile using CMake

cmake_minimum_required(VERSION 3.0)

project(SOLIDERFIRE)

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

set(CMAKE_BUILD_TYPE Debug)

include_directories(${CMAKE_SOURCE_DIR}/include)

add_executable(my_cmake_exe main.cpp src/Gun.cpp src/Solider.cpp)

Configure json file and debug


program: executable path
preLaunchTask: it can be used together to realize self compilation
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", // Execute task dependencies in the order listed
            "dependsOn":[
                "cmake",
                "make"
            ]
        }
    ]

}

summary

Tip: here is a summary of the article:
For example, the above is what we want to talk about today. This paper only briefly introduces the use of pandas, which provides a large number of functions and methods that enable us to process data quickly and conveniently.

Tags: C++ leetcode Dynamic Programming

Posted on Sun, 28 Nov 2021 05:11:03 -0500 by donbre