Original https://cmake.org/cmake-tutorial/
Here is a step-by CMake tutorial that covers the usual building system topics that help improve. Many of the topics in "Mastering CMake" ("Mastering CMake") have been introduced as separate topics, but using them in an example project will be more useful for learning. Tutorials can then be found in the project open source code tests/tutorial directory under the code tree. Each step has a subdirectory that contains the full code.
First step basic starting point
The most basic project is to build an execution file from the code. For simple projects, only two lines of cmakelists file content are required. The CMakeLists.txt content looks like this:
Cmake_minimum_required (VERSION 2.6)
Project (Tutorial)
Add_executable (Tutorial tutorial.cxx)
Note the example in the CMakeLists.txt file uses the lowercase command. The cmake supports uppercase, lowercase, mixed-case commands. Tutorial.cxx will calculate the square root of a number. The first version of the example is very simple, as follows:
A simple program that computes the square root of a number
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main (int argc, char *argv[])
{
if (ARGC < 2)
{
fprintf (stdout, "Usage:%s number\n", argv[0]);
return 1;
}
Double inputvalue = atof (argv[1]);
Double outputvalue = sqrt (inputvalue);
fprintf (stdout, "the square root of%g is%g\n",
Inputvalue, Outputvalue);
return 0;
}
Add version number and configuration header file
The first feature we added is to add a version number. Providing this functionality in CMakeLists.txt is more flexible than writing the version number in code. To add a version number to the CMakeLists.txt file, modify the following:
Cmake_minimum_required (VERSION 2.6)
Project (Tutorial)
# The version number.
Set (Tutorial_version_major 1)
Set (Tutorial_version_minor 0)
# Configure a header file to pass some of the CMake settings
# to the source code
Configure_file (
"${project_source_dir}/tutorialconfig.h.in"
"${project_binary_dir}/tutorialconfig.h"
)
# Add the binary tree to the search path for include files
# So, we'll find TutorialConfig.h
Include_directories ("${project_binary_dir}")
# Add the Executable
Add_executable (Tutorial tutorial.cxx)
Because the configuration file is written to the binary tree, we must add it to the include file search path. We create a tutorialconfig.h.in file that contains the following content:
The configured options and settings for Tutorial
#define TUTORIAL_VERSION_MAJOR @[email Protected]
#define Tutorial_version_minor @[email Protected]
When CMake configures this header file, the value of @[email protected] and @[email protected] is replaced by the value in the CMakeLists.txt file. When we modify the Tutorial.cxx to include the configuration header file and the version number. The code is as follows:
A simple program that computes the square root of a number
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "TutorialConfig.h"
int main (int argc, char *argv[])
{
if (ARGC < 2)
{
fprintf (stdout, "%s Version%d.%d\n",
Argv[0],
Tutorial_version_major,
Tutorial_version_minor);
fprintf (stdout, "Usage:%s number\n", argv[0]);
return 1;
}
Double inputvalue = atof (argv[1]);
Double outputvalue = sqrt (inputvalue);
fprintf (stdout, "the square root of%g is%g\n",
Inputvalue, Outputvalue);
return 0;
}
The main change is to add the TutorialConfig.h header file and print the version information as part of the user information
//=========================================================
Second step to add a library
We will add a library to our project. This library contains our own implementations of the square root of the computed number. The executable file can use this library instead of the standard square root function provided by the compiler. In this tutorial, we put this library into a subdirectory mathfunctions. The directory contains a CMakeLists.txt file of one line of content:
Add_library (mathfunctions mysqrt.cxx)
Source file Mysqrt.cxx has a function called MYSQRT, which provides the function of the SQRT function of the type compiler. In the top-level CMakeFiles.txt file, call the library that we added to the subdirectory so that the library can be constructed. We also use another include directory for the mathfunctions/mysqrt.h header file to find function prototypes. The final change is to add this new library to the execution file. The last few lines of the top-level CMakeLists.txt file look like this:
Include_directories ("${project_source_dir}/mathfunctions")
Add_subdirectory (mathfunctions)
# Add the Executable
Add_executable (Tutorial tutorial.cxx)
Target_link_libraries (Tutorial mathfunctions)
Now let's consider how this library can be made optional. There is no reason for this in this tutorial, but you may need to do so in a larger library provided by a third party. The first step is to add an option to the CMakeLists.txt file
# Should we use our own math functions?
Option (Use_mymath
"Use tutorial provided math implementation" on)
This would show the CMake GUI with a default value of over that the user can change as desired. This setting'll be stored in the cache so the user does not need to keep setting it each time they run CMake on thi S project. The next change was to make the build and linking of the Mathfunctions library conditional. To does this we change the end of the "top level" CMakeLists.txt file to look like the following:
The user can change the value that shows how CMake works at the default value. This setting is stored in the cache so that the user does not need to keep this setting for each run of CMake. The next step is to build and connect the library to build on conditions. In the top level CMakeLists.txt file we add the following:
# Add the Mathfunctions library?
#
if (Use_mymath)
Include_directories ("${project_source_dir}/mathfunctions")
Add_subdirectory (mathfunctions)
Set (Extra_libs ${extra_libs} mathfunctions)
endif (Use_mymath)
# Add the Executable
Add_executable (Tutorial tutorial.cxx)
Target_link_libraries (Tutorial ${extra_libs})
The use of the Use_mymath setting determines whether the mathfunctions is compiled and used.
CMAKE Tutorial First two chapters learn