Article Title: Use CMake in linux to build applications. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
This article describes how to use CMake, a cross-platform automated build system, on linux. CMake is a tool easier to use than automake, which can free programmers from complicated compilation and connection processes. This article describes how to use CMake to process multi-source file directories, how to find and use other development kits, and how to Generate debug and release programs.
CMake Introduction
CMake is a cross-platform automated construction system. It uses a file named CMakeLists.txt to describe the building process and generate standard building files, for example, Makefile of Unix or projects/workspaces of Windows Visual C ++. The file CMakeLists.txt needs to be manually written. You can also write a script to generate it semi-automatically. CMake provides more concise syntax than autoconfig. The process for generating and compiling Makefile using CMake on linux is as follows:
1. Write CmakeLists.txt.
2. Run "cmake PATH" or "ccmake PATH" to generate the Makefile (PATH is the directory where CMakeLists.txt is located ).
3. Use the make command for compilation.
First Project
Assume that there is only one source file main. cpp in our project.
Listing 1 source file main. cpp
1 #include
2 3 int main()4 {5 std::cout<<"Hello word!"<
|
To build this project, we need to write the file CMakeLists.txt and put it in the same directory as main. cpp:
Listing 2 CMakeLists.txt
1 PROJECT(main)2 CMAKE_MINIMUM_REQUIRED(VERSION 2.6)3 AUX_SOURCE_DIRECTORY(. DIR_SRCS)4 ADD_EXECUTABLE(main ${DIR_SRCS}) |
The syntax of CMakeLists.txt is simple and consists of commands, comments, and spaces. commands are case-insensitive and the content after the symbol "#" is considered as comments. The command consists of the command name, Parentheses, and parameters. The parameters are separated by spaces. For example, for the CMakeLists.txt file in Listing 2, the first line is a command named PROJECT and the parameter is main. This command indicates that the PROJECT name is main. The command line 2 limits the CMake version. The third line uses the command AUX_SOURCE_DIRECTORY to assign the source file name in the current directory to the variable DIR_SRCS. The description of the command AUX_SOURCE_DIRECTORY in the CMake manual is as follows:
This command sets the Parameter
All source file names are assigned to parameters. . The fourth line uses the command ADD_EXECUTABLE to indicate that the source file in the DIR_SRCS variable needs to be compiled into an executable file named main.
After compiling the file CMakeLists.txt, you must use the cmake or ccmake command to generate the Makefile. The difference between ccmake and cmake is that ccmake provides a graphical operation interface. The cmake command is executed as follows:
Here we enter the directory where main. cpp is located and run "cmake." To Get The Makefile and compile it using make, as shown in.
Figure 1. camke running result
[1] [2] [3] [4] Next page