CMake is a cross-platform installation (compilation) tool that can be used to describe the installation of all platforms (the compilation process) with simple statements. He is able to output a variety of Makefile or project files that can test the C + + features supported by the compiler, similar to the Automake under UNIX.
CMake How to use
All of the CMake statements are written in a file called: CMakeLists.txt. When the CMakeLists.txt file is determined, the associated variable value can be configured with the Ccmake command. This command must point to the directory where the CMakeLists.txt is located. After the configuration is complete, apply the CMake command to generate the appropriate makefile (under the UNIX like system) or the project file (specified with the appropriate programming tools under window).
Its basic operating procedures are:
$> Ccmake Directory
$> CMake Directory
$> make
Where directory is the directory where CMakeList.txt resides;
- The first statement is used to configure the compilation options, such as the Vtk_dir directory, the general step does not need to configure, directly execute the second statement, but when there is an error, it is necessary to consider the configuration, this step is really useful;
- The second command is used to generate makefile files based on CMakeLists.txt;
- The third command is used to execute the makefile file, compile the program, and generate the executable file;
CMake implementation is so simple, the difficulty lies in how to write CMakeLists.txt files, the following example of a simple introduction to CMakeLists.txt writing, see the following CMakeLists.txt
#project Name
PROJECT (Test_math)
#head file path
Include_directories (
Include
)
#source Directory
Aux_source_directory (src Dir_srcs)
#set environment variable
SET (Test_math
${DIR_SRCS}
)
#set extern Libraries
SET (LIBRARIES
Libm.so
)
#add executable file
Add_executable (.. /bin/bin ${test_math})
#add link Library
Target_link_libraries (.. /bin/bin ${libraries})
or use the following CMakeLists.txt
#project Name
PROJECT (Test_math)
#head file path
Include_directories (
Include
)
#source Directory
Aux_source_directory (src Dir_srcs)
#set environment variable
SET (Test_math
${DIR_SRCS}
)
#add executable file
Add_executable (.. /bin/bin ${test_math})
#add link Library
Target_link_libraries (.. /bin/bin m)
This is the CMakeLists.txt of a program that tests mathematical functions, followed by "#" as the contents of a comment, and CMake commands are all uppercase
Line 2nd specifies that the generated project is named Test_math
Line 4th specifies that the header file directory is include
Line 8th specifies that the source file directory is SRC and assigns it to the environment variable Dir_srcs
Line 10th sets the value of the environment variable Test_math to the value of the environment variable DIR_SRCS, which is used here to show how environment variables are assigned to an environment variable
The 14th line is to assign the math library to the environment variable libraries, which, of course, can be used without this environment variable, and directly using the library name later
Line 18th is used to specify the build file to compile all the files in the environment variable Test_math directory. Executable file bin in the/bin directory
Line 20th specifies: /bin/bin the link library at execution time is the value of the environment variable libraries-libm.so
The source file is given below
/SRC/MAIN.C:
#include <stdio.h>
#include ". /include/a.h "
int main ()
{
Double b=25.0;
Double a=0.0;
A=get_sqrt (b);
printf ("A is%LF, B is%lf\n", A, A, a, a);
return 0;
}
/src/a.c
#include ". /include/a.h "
Double get_sqrt (double var1)
{
return sqrt (VAR1);
}
/include/a.h
#ifndef A_file_header_inc
#define A_file_header_inc
#include <math.h>
Double get_sqrt (double var1);
#endif
Place the CMakeLists.txt in the current directory and execute the CMakeLists.txt
$> CMake.
$> make
You can build the executable file in the bin file under directory/bin, so run to see if it works the way you want it to.
CMake use Method (RPM)