Learning cmake to build the system from scratch (2)

overview

A cmake-based construction system is organized through a series of logical targets. A target corresponds to an executable file or library, or a custom target that contains some custom commands. Between targets Dependencies determine the order of builds and the rules for regenerating changes.

Binary Target

Executables and libraries are defined using the add_executable() and add_library() commands. The resulting binaries will have some prefixes, suffixes, and some platform-based extensions. Binary-only dependencies are defined using target_link_library () command expression:

add_library(archive archive.cpp zip.cpp lzma.cpp)
add_executable(zipapp zipapp.cpp)
target_link_libraries(zipapp archive)

archive is defined as a static library, archive is compiled from archive.cpp, zip.cpp and lzma.cpp, zipapp is defined as a binary file, obtained by compiling and linking zipapp.cpp, and will be linked when linking The previously generated static library archive is linked in.

binary executable

An executable target can be defined by the add_executable command

add_excutable(mytool mytool.cpp)

Some commands in cmake such as add_custom_command can generate some rules used at runtime, so that an executable target looks like a command executable file. This executable file is composed of a series of commands instead of having The source file is compiled, and the build system will ensure that the executable file of this command has been generated before running these commands.

binary library

If the type of library is not specified, the add_library command defines a static library by default, and the type of library can be specified by the following command:

add_library(archive SHARED archive.cpp zip.cpp lzma.cpp)
add_library(archive STATIC archive.cpp zip.cpp lzma.cpp)

In addition to the above method, the type of compiled binary library can also be set through the global flag BUILD_SHARED_LIBS. When this flag is true, if the type of binary library is not specified, it will be compiled as a dynamic library. In the context of a build system definition, it doesn't really matter if it's a dynamic or a static library, since commands, dependency specifications, and other APIs work the same for static and dynamic libraries. But the MODULE library is a bit special, because it is usually not linked, so the MODULE library cannot be used in target_link_libraries(), it will be used as a plug-in at runtime, if the library does not export any symbols, it cannot be compiled into a dynamic library, because The dynamic library will have at least one symbol everywhere, and a MODULE library can be compiled by the following command

add_library(archive MODULE 7z.cpp)

Apple Frameworks

A dynamic library can use the FRAMEWORK attribute to create a macOS or iOS Framework Bundle. If a library has a FRAMEWORK attribute, then the FRAMEWORK_VERSION attribute should be set at the same time. This attribute is set to "A" according to the macOS convention, and MACOSX_FRAMEWORK_IDENTIFIER sets the unique identifier of the bundle.

add_library(MyFramework SHARED MyFramework.cpp)
set_target_properties(MyFramework PROPERTIES
    FRAMEWORK TRUE
    FRMAEWORK_VERSION A
    MACOSX_FRAMEWORK_IDENTIFIER org.cmake.MyFramework)

Object library

An OBJECT library type defines a series of object files compiled from the source code. The object file collection can be used as the input of other targets through the $<TARGET_OBJECT:name> syntax. This is a generated expression that can be used to provide OBJECT to other targets. library:

add_library(archive OBJECT archive.cpp zip.cpp lzma.cpp)

add_library(archiveExtras STATIC $<TARGET_OBJECT:archive> extras.cpp)

add_executable(test_exe $<TARGET_OBJECT:archive> test.cpp)

In addition to using its own source files, other targets will also use a collection of object files during the linking phase. At the same time, an Object library can be linked by multiple targets:

add_library(archive OBJECT archive.cpp zip.cpp lzma.cpp)

add_library(archiveExtras STATIC extras.cpp)
target_link_library(archiveExtras PUBLIC archive)

add_execurable(test_exe test.cpp)
target_link_library(test_exe archive)

The linking steps of other targets will use the object files of the linked OBJECT library, and in addition, when compiling source code in those other targets, the usage requirements of the OBJECT library will be obeyed. In addition, these usage requirements will be passed on to the dependencies of those other targets, and the object library cannot be used as a TARGET when using the add_custom_command(TARGET) command signature. However, by using $<TARGET_OBJECTS:objlib>, add_custom_command(OUTPUT) or file(GENERATE) can use the list of objects.

at last

This article mainly introduces the content of various library files of the cmake build system. For more articles, you can pay attention to the official account QStack.

Supongo que te gusta

Origin blog.csdn.net/QStack/article/details/129034858
Recomendado
Clasificación