An overview,

1, CMake function

CMake is a cross-platform, open source build system. It is software built, tested and packaged all in one piece. For example, it can build Visual Studio’s.sln files or Linux makefiles from source code and pre-configured build profiles, start the build process, start unit tests, package executables, and dependencies to specified directories.

2, CMake advantages

  1. Cross-platform, supporting Windows, Linux, Mac, and Android systems;
  2. Good scalability, support logic control syntax, conditional compilation, etc.

CMake Helloworld example

CMake configures the project build process through the cmakelists.txt file, which is usually located in the project root directory. There are also xxx.cmake files, which function like header files in C++ and can be included in cmakelists.txt to extend build configurations.

1. Project directory, configure cmakelists.txt

  • Create a SRC directory containing the hello. CPP file.
  • Create root directory cmakelists.txt;
  • The Build directory is an empty directory that performs the build process (the build process produces configuration files that are built in a separate folder to prevent contamination of source code);

Above cmakelists.txt file explanation:

  • project(hello)The configuration project name is Hello;
  • add_executable(hello src/hello.cpp)Configure the build executable file namedhello, the file participating in the link issrc/hello.cpp;

2. Generate solutions

Generate the project solution by specifying the cmakelists.txt location of the root directory using the cmake command.

This generates hello. Vcxproj for the Visual Studio solution Hello. SLN and the hello project project.

3, build

The previous step only generated the solution, the hello.exe we want has not been generated yet. Hello. Vcproj can be generated using a graphical interface, or directly from the command line.

Execute cmake –build. In the build directory:

CMake common keywords

1. Project management

(1) include_directory

Configure header file paths. You can configure multiple paths at the same time. For example: include_directory(CMAKE_SOURCE_DIR/include1 CMAKE_CURRENT_SOURCE_DIR/.. /include2)

CMAKE_SOURCE_DIR and CMAKE_CURRENT_SOURCE_DIR:

  • CMAKE_SOURCE_DIR: the path to the original cmakelists. TXT, such as usingcmake ..When built, refers to the cmakelists.txt path in the project root directory;
  • CMAKE_CURRENT_SOURCE_DIR: current path of cmakelists. TXT (there can be multiple cmakelists. TXT);

(2) add_executable, add_library

The former configures how to generate executable files and the latter configures how to generate library files. Such as:

add_executable(Executable file name source list)add_library(Dynamic link library name SHARED source list)add_librarySTATIC link library name STATIC source listCopy the code

When a link is performed, other link libraries are typically specified. CMake specifies the link library path via library_directory and the link library name via target_link_libraries, equivalent to -l and -l in makefiles.

library_directory(CMAKE_BINARY_DIR/lib)    # CMAKE_BINARY_DIR is the directory where the cmake command is executed
target_link_libraries(Binary file name link library name list)Copy the code

(3) the add_subdirectory, include

For the simple use of HelloWorld above, you might think it would be easier to write a Makefile, but would you still write a Makefile if the entire solution looked like this?

A single solution project is too large, and CMake uses divide-and-conquer solutions. These are introduced in the root directory by using a separate CMake configuration file for each module.

  • add_subdirectoryIs a module directory. CMake will look in that directoryCMakeLists.txtFile, and execute the configuration script inside;
  • includeThe argument is a filename, usually similarxxx.cmakeThe file;

2. Switch options

CMake Enables or disables a compilation option by using the -d option of the CMake command. For example, there is the following cmakelists.txt:

project(hello)
option("ENABLE_FEATURE"."whether enable some feature".OFF)    # define a switch, whether to enable a specified function, default off

if(ENABLE_FEATURE)
add_executable(hello src/hello.cpp feature.cpp)    # Assume that the specific function is to link one more file
else
add_executable(hello src/hello.cpp)
endif
Copy the code

Build with cmake.. -denable_feature =ON Enables the specified function. If there is no -d option or ON is changed to OFF, the specified function is disabled.

The options defined by option can only be used in cmakelists. TXT. Add_definition can do this. Such as:

if(ENABLE_FEATURE)
add_definition(FEATURE)    # If ENABLE_FEATURE is enabled, define the FEATURE macro
elsec
endif
Copy the code

By adding macro definitions, the same switch function is achieved in the source code.

3. Debug information

(1) message

Print information. Format: message(

“debug info: value of ARg: ${arg}”) The message function has at least the following modes: STATUS, INFO, WARNING, FATAL_ERROR. FATAL_ERROR mode stops the build and exits with a failure.

4. Other CMake variables

(1) CMAKE_C_FLAGS, CMAKE_CXX_FLAGS

Resolve control C/C++ compilation options. Set (CMAKE_CXX_FLAGS “std-c++11 ${CMAKE_CXX_FLAGS}”) # extend the value of CMAKE_CXX_FLAGS to add support for c++11.

(2) CMAKE_BUILD_TYPE

Specifies the build type, passed in when you build using the cmake command. For example: cmake.. -DCMAKE_BUILD_TYPE=Release