Use cmake to teach the use of install

Keywords: C++ cmake

Here we mainly introduce the operations of install ing using cmake:

The content that can be installed through cmake can include target binary, dynamic library, static library, files, directories, scripts, etc., which are briefly described below:

For details, please refer to the official website. Here is a brief description

1. Installation of target files

install(TARGETS targets... [EXPORT <export-name>]
        [RUNTIME_DEPENDENCIES args...|RUNTIME_DEPENDENCY_SET <set-name>]
        [[ARCHIVE|LIBRARY|RUNTIME|OBJECTS|FRAMEWORK|BUNDLE|
          PRIVATE_HEADER|PUBLIC_HEADER|RESOURCE]
         [DESTINATION <dir>]
         [PERMISSIONS permissions...]
         [CONFIGURATIONS [Debug|Release|...]]
         [COMPONENT <component>]
         [NAMELINK_COMPONENT <component>]
         [OPTIONAL] [EXCLUDE_FROM_ALL]
         [NAMELINK_ONLY|NAMELINK_SKIP]
        ] [...]
        [INCLUDES DESTINATION [<dir> ...]]
        )

The following table shows the target type and its associated variables and built-in default values, which are applicable when no target is specified:

Target type

GNUInstallDirs variable

Built in defaults

RUNTIME

${CMAKE_INSTALL_BINDIR}

bin

LIBRARY

${CMAKE_INSTALL_LIBDIR}

lib

ARCHIVE

${CMAKE_INSTALL_LIBDIR}

lib

PRIVATE_HEADER

${CMAKE_INSTALL_INCLUDEDIR}

include

PUBLIC_HEADER

${CMAKE_INSTALL_INCLUDEDIR}

include

The TARGETS in the parameter are followed by our ADD_EXECUTABLE or add_ The object file defined by library may be executable binary, dynamic library or static library.
There are three corresponding target types: ARCHIVE refers to static LIBRARY, LIBRARY refers to dynamic LIBRARY, and RUNTIME refers to executable target binary.
DESTINATION defines the installation path. If the path starts with /, it refers to the absolute path. At this time, CMAKE_INSTALL_PREFIX is actually invalid
If you want to use cmake_ INSTALL_ Prefix to define the installation path, you should write it as a relative path, that is, do not start with /. Then the path after installation is ${CMAKE_INSTALL_PREFIX} / < the path defined by destination >
Take a simple example:

INSTALL(TARGETS myrun mylib mystaticlib
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION libstatic
)

The above example will:
Install the executable binary myrun into the ${CMAKE_INSTALL_PREFIX}/bin directory
Install the dynamic library libmylib into the ${CMAKE_INSTALL_PREFIX}/lib directory
Install the static library libmystaticlib into the ${CMAKE_INSTALL_PREFIX}/libstatic directory
In particular, you don't need to care about the specific generated path of TARGETS. You just need to write the name of TARGETS
Yes.

2. Installation of ordinary documents

install(<FILES|PROGRAMS> files...
        TYPE <type> | DESTINATION <dir>
        [PERMISSIONS permissions...]
        [CONFIGURATIONS [Debug|Release|...]]
        [COMPONENT <component>]
        [RENAME <name>] [OPTIONAL] [EXCLUDE_FROM_ALL])

TYPE Argument

GNUInstallDirs Variable

Built-In Default

BIN

${CMAKE_INSTALL_BINDIR}

bin

SBIN

${CMAKE_INSTALL_SBINDIR}

sbin

LIB

${CMAKE_INSTALL_LIBDIR}

lib

INCLUDE

${CMAKE_INSTALL_INCLUDEDIR}

include

SYSCONF

${CMAKE_INSTALL_SYSCONFDIR}

etc

SHAREDSTATE

${CMAKE_INSTALL_SHARESTATEDIR}

com

LOCALSTATE

${CMAKE_INSTALL_LOCALSTATEDIR}

var

RUNSTATE

${CMAKE_INSTALL_RUNSTATEDIR}

<LOCALSTATE dir>/run

DATA

${CMAKE_INSTALL_DATADIR}

<DATAROOT dir>

INFO

${CMAKE_INSTALL_INFODIR}

<DATAROOT dir>/info

LOCALE

${CMAKE_INSTALL_LOCALEDIR}

<DATAROOT dir>/locale

MAN

${CMAKE_INSTALL_MANDIR}

<DATAROOT dir>/man

DOC

${CMAKE_INSTALL_DOCDIR}

<DATAROOT dir>/doc

It can be used to install general files and specify access rights. The file name is the relative path under the path of this instruction. If PERMISSIONS are not defined by default, the PERMISSIONS after installation are:
OWNER_WRITE, OWNER_READ, GROUP_READ, and WORLD_READ, i.e. 644 permissions.  

3. Directory installation

install(DIRECTORY dirs...
        TYPE <type> | DESTINATION <dir>
        [FILE_PERMISSIONS permissions...]
        [DIRECTORY_PERMISSIONS permissions...]
        [USE_SOURCE_PERMISSIONS] [OPTIONAL] [MESSAGE_NEVER]
        [CONFIGURATIONS [Debug|Release|...]]
        [COMPONENT <component>] [EXCLUDE_FROM_ALL]
        [FILES_MATCHING]
        [[PATTERN <pattern> | REGEX <regex>]
         [EXCLUDE] [PERMISSIONS permissions...]] [...])

Here we mainly introduce the DIRECTORY, PATTERN and PERMISSIONS parameters.
The relative path of the Source DIRECTORY is connected behind the DIRECTORY, but it must be noted that abc and abc / are very different.
If the directory name does not end with /, the directory will be installed as abc under the target path. If the directory name ends with /,
Represents installing the contents of this directory to the target path, but does not include the directory itself.
PATTERN is used to filter using regular expressions, and PERMISSIONS is used to specify the file PERMISSIONS after PATTERN filtering.
Let's take an example:

INSTALL(DIRECTORY icons scripts/ DESTINATION share/myproj
PATTERN "CVS" EXCLUDE
PATTERN "scripts/*"
PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ
GROUP_EXECUTE GROUP_READ)

The execution result of this instruction is:
Install the icons directory to < prefix > / share / myproj, and install the contents in scripts / to
<prefix>/share/myproj
It does not contain a directory named CVS. For the scripts / * file, specify the permission as OWNER_EXECUTE
OWNER_WRITE OWNER_READ GROUP_EXECUTE GROUP_READ.

Here's a test: according to Last Install and test the following articles:

4. Test

Modify the CMakeLists.txt file in the root directory as follows. In fact, install is added. Here, you need to create several files before trying to install:

# Multiple directories multiple source files
cmake_minimum_required(VERSION 2.8)

project(test1)
# Traverse all files in the current directory and use DIR_SRCS save can also be specified through SET(SRC_LIST main.c)
aux_source_directory(. DIR_SRCS)

# Add a directory to save the generated executable file. The intermediate product will not be saved. If this directory is not specified, it will be saved together with the intermediate product
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin )
# Add header file
include_directories(${PROJECT_SOURCE_DIR}/include)
# Here, the subdirectory conditions are added to the project, and the compiled output products are specified. The default output products are saved to the build/src folder,
# If you do not specify a path to save the library directory, it will also be saved to this path
add_subdirectory(src)

# The above definition adds the src subdirectory to the project, and specifies the path of the compilation output (including the compilation intermediate results) as
# Bin directory. If the bin directory is not specified, the compilation results (including intermediate results) will be stored in the
# build/src directory (this directory corresponds to the original src directory). After specifying the bin directory, it is equivalent to at compile time
# Rename src to bin, and all intermediate results and target binaries will be stored in the bin directory.

# Specify build target
add_executable(test1 main.cpp)

# Add link library
# Link the library file compiled from the subdirectory set src here
target_link_libraries(test1 add)

message(STATUS  ${PROJECT_SOURCE_DIR} "------Compile and link to generate executable file-----")

# Add installation information such as copyright readme
install(FILES copyright README.md DESTINATION share/doc/cmake/test)
# Install runtest.sh
install(PROGRAMS runtest.sh DESTINATION bin)

# Here, follow the files in the doc directory
install(DIRECTORY doc/ DESTINATION share/doc/cmake/test)

Enter build and execute the following commands:

cmake -DCMAKE_INSTALL_PREFIX=/home/ubuntu/work/usr ..
make
make install 

This is only for test use, so it can also be installed by default under home/ubuntu/work/usr. The default installation path is:

If I don't define cmake_ INSTALL_ Where will prefix be installed?
You can try the following, cmake; make; Make install, you will find
CMAKE_ INSTALL_ The default definition of prefix is / usr/local

 

The next section describes the construction of static and dynamic libraries

Posted by buckit on Thu, 02 Dec 2021 18:40:13 -0800