CMake study notes 1: a simple example


CMake is used CMakeLists.txt to describe the organizational relationship between code.

The simplest one CMakeLists.txtcontains 3 parts: the required CMake version, project information and compilation process.

CMake version

It is used toCMakeLists.txt specify the minimum version of CMake required, for example, the following statement shows that the minimum version of CMake required for compilation is 3.2:cmake_minimum_required

cmake_minimum_required(VERSION 3.2)

project information

Every CMake project needs a project()statement to describe the project information, usually the project information appears cmake_minimum_requiredafter the statement. Statements used to describe item information follow the form:

project(projectName
    [VERSION major[.minor[.patch[.tweak]]]]
    [LANGUAGES languageName ...]
)

Among them, projectNameis the project name; [VERSION major[.minor[.patch[.tweak]]]]represents the version number of the project, including the major version number (major), sub-version number (minor), revision version number (patch) and fine-tuning version number (tweak), where only the major version number can be written; [LANGUAGES languageName ...]means For the languages ​​involved in the project, CMake will check the corresponding language compiler/interpreter and other information during configuration. If multiple languages ​​are involved, separate them with spaces.

What must be written here is the project name, while the other two may not be written. If there is no language involved in writing the project, CMake will look for C and C++ languages ​​​​by default. The following is an example of describing the project information, the project name is MyApp, the version 1.0, and the language used C++:

project(MyApp VERSION 1.0 LANGUAGES CXX)

compilation process

compile target object

CMakeLists.txtOf course, the compilation target must be included. Compiled target objects can actually be divided into two categories, one is executable files and the other is library files.

executable file

The following is the general form of a statement describing a compiled executable:

add_executable(targetName [WIN32] [MACOSX_BUNDLE]
    [EXCLUDE_FROM_ALL]
    source1 [source2 ...]
)

in,

  • targetNameIs the name of the executable file, for example, it will be generated under Windows system targetName.exe.
  • WIN32It is a parameter that will be used on the Windows platform, and this parameter will be ignored on other platforms. This parameter indicates that the target executable file is a Windows GUI program, which corresponds to the setting of the project properties-linker-subsystem in the system in Visual Studio 窗口(/SUBSYSTEM:WINDOWS). For example, when using a library wxWidgets, this parameter must be added when generating a GUI executable file, otherwise an error will be reported.
  • MACOSX_BUNDLEThe parameter indicates that an application bundle (app bundle) is built on the Apple platform.
  • EXCLUDE_FROM_ALLThe parameter indicates that when the target is not specified, it will not be generated by default. When the compilation target is not specified, ALLthe target will be compiled by default, that is, all targets. This parameter indicates that this target is not ALLin the target. This target will not be built unless it is explicitly specified to be built, or other targets that need to be compiled depend on it.

Library file

In larger projects, many libraries need to be built in addition to the executable. The general form of describing a compilation target as a library file is as follows:

add_library(targetName [STATIC | SHARED | MODULE]
    [EXCLUDE_FROM_ALL]
    source1 [source2 ...]
)

in,

  • targetNameis the name of the compiled library.
  • STATIC | SHARED | MODULECorresponding to different types of libraries, including dynamic libraries, static libraries, and module libraries. It is recommended to omit this parameter, and then add parameters to indicate that the dynamic library should be compiled when running cmake -DBUILD_SHARED_LIBS=YES.
  • source1, source2for the source files needed to compile the library.

Link

In projects, there are often dependencies between libraries and libraries, applications and libraries, that is, libraries will be linked to libraries or applications. Generally speaking, dependencies can be divided into the following three types:

  • PRIVATE (private)
    Private dependency means that library A uses library B in its own internal implementation, and any code that uses library A does not need to know library B.
  • PUBLIC (public)
    public dependency means that library A not only uses library B in its internal implementation, but also uses library B in its interface, which means that library A cannot be used without library B, and all codes that use library A Both directly depend on the B library.
  • INTERFACE (interface)
    The interface dependency means that in order to use the A library, a part of the B library must be used. The difference between interface dependency and public dependency is that in interface dependency, library B is not used in the internal implementation of library A, but library B is only used in the interface.

In CMake, target_link_librariesit is used to describe dependencies, and its general form is as follows:

target_link_libraries(targetName
    <PRIVATE|PUBLIC|INTERFACE> item1 [item2 ...]
    [<PRIVATE|PUBLIC|INTERFACE> item3 [item4 ...]]
    ...
)

in,

  • targetNameis the library/application name to link against.
  • <PRIVATE|PUBLIC|INTERFACE>is an optional parameter that describes the dependencies between.
  • item1The name of the dependent library.

complete example

Suppose the files in the project directory look like this:

project
    |-- func.h
    |-- func.cpp
    |-- main.cpp
    |-- CMakeLists.txt

func.hContents in:

#ifndef __FUNC_H
#define __FUNC_H

int func();

#endif

func.cppContents in:

#include "func.h"

int func(){
    
    
    return 2;
}

main.cppContents in:

#include <iostream>
#include "func.h"

int main(){
    
    
    std::cout << func() << std::endl;
    return 0;
}

CMakeListsContents in:

# 版本
cmake_minimum_required(VERSION 3.2)

# 项目信息
project(simple VERSION 1.0 LANGUAGES CXX)

# 编译目标
add_library(func func.cpp)
add_executable(main main.cpp)

# 依赖关系
target_link_libraries(main PRIVATE func)

CMake compilation process:

  1. Create a new builddirectory and enter
  2. command line inputcmake ..
  3. After the CMake configuration is complete, the command line inputcmake --build .
  4. Find the executable in Debugthe folder

Related Links

Guess you like

Origin blog.csdn.net/willian113/article/details/107917113