CMake provides a more concise syntax than AutoConfig.
The process of generating Makefile and compiling it using CMake under the Linux platform is as follows: Writing CmakeLists.txt. Execute the command "CMake path" or "Ccmake path" to generate Makefile (PATH is the directory where CMakeLists.txt resides). Compile with the Make command.
CMakeLists.txt need to be written by hand, or they can be generated semi-automatically by scripting
First Project
Now suppose that there is only one source file in our project main.cpp Listing 1 source file Main.cpp
1 #include <iostream>
2
3 int main ()
4 {
5 std::cout<< "Hello word!" <<std::endl;
6 return 0;
7}
To build the 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, consisting of commands, comments, and spaces, where the command is case-insensitive and the content after the symbol "#" is considered a comment. Commands are made up of command names, parentheses, and parameters, and spaces are used between the parameters. For example, the CMakeLists.txt file for listing 2: The first line is a command, the name is Project, the parameter is main, and the command indicates that the project name is main. The second line of the command qualifies the CMake version. The third line uses the command aux_source_directory to assign the name of the source file in the current directory to the variable DIR_SRCS. The command aux_source_directory is described in the CMake manual as follows:
Aux_source_directory (<dir> <variable>)
This command assigns all the source file names in the parameter <dir> to the parameter <variable>. Row four uses the command add_executable to indicate that the source file in the variable Dir_srcs needs to be compiled into an executable file named Main.
After you have finished writing file CMakeLists.txt, you need to generate makefile using the CMake or Ccmake command. The difference between Ccmake and command CMake is that Ccmake provides a graphical interface for the operation. The CMake command executes in the following manner:
CMake [Options] <path-to-source>
Here we go into the directory where the Main.cpp is located and execute "cmake." After you get Makefile and compile with make, as shown in the following figure.
ways to work with multi-source file directories
It is also easy to CMake the source code in different directories. Now assume that our source code distribution is as follows: Figure 2. Distribution of source Codes
The files in the SRC directory are compiled into a link library. The first step, the CMakeLists.txt in the Project master directory
Create the file CMakeLists.txt in the directory Step2. The contents of the file are as follows: listing 3 CMakeLists.txt in directory Step2
1 PROJECT (Main)
2 cmake_minimum_required (VERSION 2.6)
3 add_subdirectory (SRC)
4 aux_source_directory (. DIR_SRCS)
5 add_executable (main ${dir_srcs})
6 target_link_libraries (main Test)
Relative to Listing 2, the file adds the following: The third line, using the command add_subdirectory, indicates that this item contains a subdirectory src. Line six, using the command target_link_libraries to indicate that the executable file main needs to connect a link library named Test. The second step, the CmakeLists.txt in the sub-directory
Create the CmakeLists.txt in the subdirectory src. The contents of the file are as follows: listing 4. CmakeLists.txt in the catalog src
1 aux_source_directory (. DIR_TEST1_SRCS)
2 add_library (Test ${dir_test1_srcs})
Use the command add_library in this file to compile the source files in the SRC directory into a shared library. step three, execute CMake
Now that we have finished writing all the CMakeLists.txt files in the project, go to the directory Step2 and execute the command "CMake." and "make" to get the results as follows: Figure 3. CMake execution results when working with multiple source file directories
In the process of executing cmake, first parse the CMakeLists.txt in directory Step2, when the program executes command add_subdirectory (SRC) into the directory src to parse the CMakeLists.txt in it.