C++ compilation (g++, cmake, c++ library use)

g++ use

$ g++ 111.cpp 
//当前目录下生成了一个a.out文件

$ ./a.out
//运行此程序

cmake uses

Theoretically, any C++ program can be compiled with g++. But when the program size becomes larger and larger, a project may have many folders and source files, and the compilation command input will become longer and longer. Usually a small C++ project may contain more than a dozen classes, and there are complex dependencies between classes. Part of it is compiled into an executable file, and the other part is compiled into a library file. If we only rely on the g++ command, we need to enter a large number of compilation instructions, and the entire compilation process will become extremely cumbersome. Therefore, for C++ projects, it will be more efficient to use some project management tools.

————Fourteen Lectures on Visual Slam

//创建文件CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
//声明cmake要求的最低版本

project(HelloSLAM)
//创建一个cmake工程

add_executable(helloslam init.cpp)
//添加一个可执行文件
//语法:add_excutable( 程序名 源代码文件 )
$ make .       //生成中间文件
$ make         //生成可执行文件
$ ./filename   //执行文件

 A common compilation method is to create a folder first so that intermediate files can be generated there so that they can be deleted later.

$ mkdir build
$ cd bulid
$ camke..
$ make

c++ library

A library is a collection of many algorithms and programs.

Commonly used libraries in SLAM algorithms include the opencv library related to computer vision and the Eigen library for matrix processing.

In Linux, libraries are divided into two types: static libraries and shared libraries. Static libraries have .a as the suffix, and shared libraries have .so. All libraries are packaged collections of functions. The difference is that a static library will generate a copy every time it is called, while a shared library only has one copy, which saves space.

The following demonstrates how to use cmake for packaging

//filename:helloworld.cpp

#include<iostream>
using namespace std;
void PrintHello(){
    cout<<"helloworld"<<endl;
}

A .cpp file containing a function that outputs helloworld

//CMakeLists.txt

add_library(hello helloworld.cpp)
//下面是共享库生成方式
//add_library(hello_shared SHARED helloworld.cpp)

This command tells cmake that we want to compile this file into a library called hello

Then, as above, use cmake in the terminal to compile the entire project.

$ cd build
$ cmake ..
$ make

Afterwards, a libhello.a/libhello_shared.so file will be generated in the build folder. This is the obtained library.

After getting this library, you cannot call it directly. You must provide a header file that contains the function declaration in the library.

For users of the library, as long as they get the header files and library files, they can call the library.

//filename:helloworld.h

#ifndef HELLOWORLD_H_
#define HELLOWORLD_H_
void printHello();

#endif

main function

//filename:useHello.cpp
#include "helloworld.h"
int main(){
    printHello();
    return 0;
}

Then, add an executable program generation command in CMakeLists.txt and link it to the library just now:

//书接上文
add_executable(useHello useHello.cpp)
target_link_libraries(useHello hello hello_shared)

Guess you like

Origin blog.csdn.net/qq_38830492/article/details/127505059