illustrate
This series will introduce how MXNet uses CMake to organize projects.
This series of articles covers most of the CMake commands used in actual development.
The code in this article comes from MXNet .
What is CMake? Why learn CMake?
To figure out CMake
what it does, we need to understand C++
the development process of the entire project writing. Due to my limited ability, I will only give a brief introduction here.
After the code is written, the code needs to be compiled through a compiler. When the project becomes large, it becomes a very troublesome thing to manually enter the compilation command after each compilation. In order to solve the problem, manual input is required for each compilation. The problem of compilation command was born Makefile
. The so-called Makefile
is to write the compilation command of the entire project Makeifle
into a file named. You only need to enter make every time to compile the entire project. Makefile implements many convenient operations, such as searching all source files at once, so you don’t have to Each time you add a source file, write a g++ filename.cpp -c -o filename.o
command manually.
Although Makefile
you only need to write a compilation command once, when the project becomes huge and you need to use a third-party library, the Makefile
management will be a little troublesome. In addition to this, Makefile
there is a more fatal problem. , it cannot support cross-platform very well, that is, if you need to implement it for use on two different platforms, you Makefile
often need to write two Makefile
files. In order to solve the problems encountered
above , there is . What it can do, here is a description from the official website:Makefile
CMake
CMake
CMake is an open-source, cross-platform family of tools designed to build, test and package software. CMake is used to control the software compilation process using simple platform and compiler independent configuration files, and generate native makefiles and workspaces that can be used in the compiler environment of your choice. The suite of CMake tools were created by Kitware in response to the need for a powerful, cross-platform build environment for open-source projects such as ITK and VTK.
The rough meaning of the above in English CMake
is a cross-platform construction tool used to generate Makefile
. This is Cmake
the main purpose.
CMake
It has now become C++
a build tool used by many projects, and its use CMake
has become a general trend. So learning CMake
is particularly important.
Introduction to MXNet
MXNet
It is an open source machine learning framework that supports many different languages and can facilitate users to train and deploy machine learning. Everyone is familiar with using Python
installation MXNet
for machine learning training, and MXNet
the code here provides a dynamic library. Developers of different languages will encapsulate it differently, and they will eventually call the dynamic library. MXNet
The dynamic library here is C++
written using , and CMake
the project is built using .
MXNet code organization structure
MXNet
There are many subdirectories under the directory, we only need to pay attention to some of its directories and files:
src directory: stores source code and some header files
include directory: stores header files of externally released libraries
test directory: stores test cases
cmake directory: stores.cmake
files
3rdparty directory: stores third-party library
CMakeList.txt
files:CMake
configuration files
How to use CMake
To use, CMake
we only need to write CMakeList.txt
the file. After completing the writing of the file, we can pass cmake
the command (you need to ensure that it exists in the execution location. CMakeList.txt
If it does not exist, you only need to enter the path of the file after the command). The command will generate Makefile
the file for us. After generating Makefile
the file After that we just need to execute make
the command to start building the project.
CMakeList.txt in MXNet
Next, let's learn MXNet
how to use it CMake
to build. In the above code organization structure, we find that in addition to its own source code, the project also uses third-party libraries, and these third parties are actually built through CMake
. Such third-party libraries CMake
can be well managed.
cmake_minimum_required(VERSION 3.13)
This command is used to specify cmake
the minimum version number. When the version number is smaller than the specified version number, 3.13
it cmake
will not continue to be executed, but will prompt the user to upgrade cmake
the version.
if() elseif() else() endif()
cmake
The logical control statements in MXNet
are as follows:
if(CMAKE_CROSSCOMPILING)
set(__CMAKE_CROSSCOMPILING ${CMAKE_CROSSCOMPILING})
set(__CMAKE_CROSSCOMPILING_OVERRIDE ON)
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0)
message(FATAL_ERROR "MXNet 2 requires a C++17 compatible compiler. Please update to GCC version 7 or newer.")
endif()
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 6.0)
message(FATAL_ERROR "MXNet 2 requires a C++17 compatible compiler. Please update to Clang version 6 or newer.")
endif()
endif()
if
The second parameter in the statement is the judgment condition, which STREQUAL
is used to judge whether the two strings are equal and VERSION_LESS
whether the previous version number is smaller than the latter version number.
The first if
statement above will CMAKE_CROSSCOMPILING
be ON
executed when , and the judgment statement here should be used for cross-compilation.
Note: The sum CMake
in is expressed by sum .true
false
ON
OFF
Note: Cross-compilation is mostly used to implement code compilation for embedded devices. Since the performance of embedded devices is limited, it is generally compiled on a host with strong performance. The compiled target file (or library) can be transplanted to the embedded device for use. .
set()
set
Commands CMake
are used to define variables in, for example:
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS ON)
If you want to get a certain variable, you can ${VARIABLE_NAME}
do it through.
set
Commands can also set buffer variables, which can be used globally.
cmake built-in variables
Generally, CMAKE
the variables starting with them are cmake
built-in variables. You can obtain the value of each variable according to the method of obtaining the variable value. set
The values of these variables can be changed through commands to achieve some special purposes. MXNet
The built-in variables used in:
- CMAKE_CROSSCOMPILING cross-platform compilation macro, this macro will be activated after specifying the system name
- CMAKE_CXX_STANDARD is used to specify the C++ version
- CMAKE_CXX_STANDARD_REQUIRED When turned on, the version specified in CMAKE_CXX_STANDARD will be used. If not, it will stop and report an error. If not, the previous version will be used when turned off.
- CMAKE_CXX_EXTENSIONS Whether to enable compiler extensions for C++
- CMAKE_CXX_COMPILER_ID The name of the compiler such as GNU
- CMAKE_CXX_COMPILER_VERSION Compiler version number
- CMAKE_PROJECT_NAME The name of the top-level project (if a project contains a sub-project, then CMAKE_ PROJECT_NAME in the sub-project will be the name of the top-level project, not the name of its own project)
- PROJECT_NAME The name of the current project
- CMAKE_CURRENT_SOURCE_DIR The absolute path of the source directory currently being processed
project()
This command is used to specify the name of the project and the programming language used by the project. MXNet
The example in specifying the project name as mxnet
the programming language is C/C++
:
project(mxnet C CXX)
message()
Used to print prompt information, similar to log output, which can specify the output level
. MXNet
The following are used level
:
message(FATAL_ERROR "MXNet 2 requires a C++17 compatible compiler. Please update to GCC version 7 or newer.")
message(STATUS "CMAKE_CROSSCOMPILING ${CMAKE_CROSSCOMPILING}")
message(WARNING "Could not find NVML libraries")
message(ERROR " Only one of USE_JEMALLOC and USE_GPERFTOOLS can be defined at once")
message("After choosing blas, linking to ${mxnet_LINKER_LIBS}")
Note: The log level determines the processing that will occur and which execution FATAL_ERROE
will be stopped . Note: When no level is entered, it means the message is an important one and the user should pay attention. Note: There is no such level in , but this level is used in , and no errors occur. This is something I don't quite understand.cmake
cmake
ERROR
MXNet
include()
include
Can be used to introduce macros, modules and a file.
-
If you introduce a file (usually with the suffix
.cmake
), the usage at this time is consistent with the macro usageC/C++
ininclude
mxnet. The usage of introducing a file in mxnet is as follows:include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Utils.cmake)
The above command will import
Utils.cmake
some commonly used tool functions and macros defined in this file. \ -
mxnet
Modules are alsoinclude
introduced through commands in , for example:include(CMakeDependentOption)
CmakeDependentOption
The main methods supported in this module are introduced abovecmake_dependent_option
.
cmake_dependent_option()
This method is used to set some macro variables. The macro variables are set to one value when the dependency is established, and set to another value when the dependency is not met, such as an example mxnet
in:
cmake_dependent_option(USE_NVML "Build with nvml support if found" ON "USE_CUDA" OFF)
USE_NVML
It will be when USE_CUDA
it is done . It will be when it is done . A more complex example:ON
ON
OFF
OFF
cmake_dependent_option(A "this is description" ON "B;NOT C" OFF)
The above code means that when B
it is opened or C
closed, A
it will be opened; in other cases, A
it will be closed.
option()
Used to set an option. The variable of this option can be used to set its value when executing the cmake command -DOPTION_NAME=ON/OFF
.
option(USE_NCCL "Use NVidia NCCL with CUDA" OFF)
For example, an option is set above USE_NCCL
, and its default value is OFF
the string in the middle as its description.
To be updated, last updated on 2023.6.23