CMake installation and packaging

The book continues from the previous chapter. We have introduced the construction and use of dynamic libraries and static libraries. Here we will introduce installation and packaging and publishing after the project is built.

Previous section:

Construction and use of dynamic libraries and static libraries – Programmer Sought

Directory

1. Installation

1. Understand the install command

2. CMakeLists.txt

3. Execute the installation

2. Package release

CMakeLists.txt

package release

3. Personal summary


1. Installation

1. Understand the install command

The nstall command is used to specify the output path of the file, which can be a binary executable file, a dynamic library file, a static library file, or even a directory. In layman’s terms, install is actually to copy the generated files to a certain directory for easy packaging or transfer.

The format used by install will be different in different occasions, which can be roughly divided into installation target files and installation directories. It should be noted that the install command will not be executed when the make command is run, and the install command will be executed only after make install is run.

2, CMakeLists.txt

Just look at the last three commands

# Add source file directory
aux_source_directory(./SOURCES)

# generate executable
add_executable(example ${SOURCES})


# Add dynamic library to get libhaha.so dynamic library file
add_library(haha SHARED haha.cpp)
# Add the version number to the dynamic library to get libhaha.so.1.2 with two links at the same time
set_target_properties(haha PROPERTIES VERSION 1.2 SOVERSION 1)

# Add static library to get libhaha.a static library file
add_library(haha_static STATIC haha.cpp)
# Change the name of the output library from libhah.a to libhello.a
set_target_properties(haha_static PROPERTIES OUTPUT_NAME "hello")

# Set the output path of the executable file This is the lib directory under the project directory
SET(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/lib")
# Set the output path of the library file This is the lib directory under the project directory
SET(LIBRARY_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/lib")


# Set the unified installation path prefix The path must exist
set(CMAKE_INSTALL_PREFIX /home/jason/work/my-deploy/example/my_install_test)



# Install target files (TARGETS)
install(TARGETS ${PROJECT_NAME} haha_static haha #Waiting to install the target file executable file static library dynamic library
    RUNTIME DESTINATION bin # Install the executable file to the bin directory under the specified directory
    LIBRARY DESTINATION lib # Install the dynamic library file to the lib directory under the specified directory
    ARCHIVE DESTINATION static_lib) # Install static library files to the static_lib directory under the specified directory

# Install common files (FILES)
install(FILES haha.h DESTINATION include)

3, perform installation

Open the terminal in the project directory and execute:

mkdir build & amp; & amp; cd build
cmake..
make
make install

Check whether all installations specify a directory:

These directories are automatically generated by CMake install

Check if the file is in the directory:

2. Package release

CMakeLists.txt

# Add source file directory
#aux_source_directory(./SOURCES)
#file(GLOB SRC_LIST "*.cpp" "src/*.cpp" "src/modules/*.cpp")

# generate executable
add_executable(example main.cpp)

# Add dynamic library to get libhaha.so dynamic library file
add_library(haha SHARED haha.cpp)
# Add the version number to the dynamic library to get libhaha.so.1.2 with two links at the same time
set_target_properties(haha PROPERTIES VERSION 1.2 SOVERSION 1)


# Link the dynamic link library just generated to the executable file
target_link_libraries (${PROJECT_NAME} haha)

############################### deb packaging configuration ################## ################


# Indicates that the deb package is to be generated
SET(CPACK_GENERATOR "DEB")

# set version
set(CPACK_PACKAGE_VERSION "1.0.0")
# You can also set the version by setting each version field number
#Set the version information as follows
#SET(CPACK_PACKAGE_VERSION_MAJOR "1")
#SET(CPACK_PACKAGE_VERSION_MINOR "0")
#SET(CPACK_PACKAGE_VERSION_PATCH "0")

# Set the package name of the installation package, the packaged package will be <packagename>-<version>-<sys>.deb, if not set, the default is the project name
set(CPACK_PACKAGE_NAME "haha")

# Set the program name, which is the name after the program is installed
set(CPACK_DEBIAN_PACKAGE_NAME "haha" )

# set the schema
set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "amd64") # amd64 agrees with x86_64

# set dependencies
#SET(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6 (>= 2.3.1-6)") # libc6, the C function library under Linux

# set description
SET(CPACK_PACKAGE_DESCRIPTION "just a cpack test")

# Set contact information
SET(CPACK_PACKAGE_CONTACT "[email protected]")

# Set maintainer
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "jason")

# Include Cpack
include(CPack)

Package and release

Open the terminal under the project and run:

mkdir buid & amp; & amp; cd build
cmake..
make
make package

The deb package was successfully generated
Let’s use pkdg to check the basic information of the deb package

We can also look at the path where the deb package will be installed

Finally, share some common commands related to dpkg

3. Personal summary

The effect of the source code of the haha package above is to print haha on the center console and package it as a deb package. After installation, it is not very useful. To remember that there are functions, the workload should not be small.

For the author, it may make more sense to build a project into a static library or dynamic library, or install executable files, library files, and header files elsewhere. To give an inappropriate example: the new computer does not need to install some software, for example, I have already installed the library files, header files and executable files of OpenCV.

refer to:

[cmake articles] install command_Midsummer Night’s Dream~’s Blog-CSDN Blog

C++ compilation (4)-advanced-cmake setting install and package configuration_cmake install command_Fengse Muxi’s Blog-CSDN Blog

[cmake learning] cmake introduces third-party libraries (header file directory, library directory, library files)_cmake adds third-party libraries_Midsummer Night’s Dream~’s Blog-CSDN Blog