This series of blog is from the CMake official learning, equivalent to his Chinese version of it, the original see https://cmake.org/cmake-tutorial/
The official tutorial has 7 steps, I intend to speak only (Fanyi) the first 2 steps, the following basic are similar, see the official tutorial can be.
Step 1: Create a new executable program
First make sure you have installed CMake and g++, if not installed, just:
sudo apt-get install CMake g++
Then prepare a workspace and prepare some material:
mkdir LEARN_CMAKE_AGAINCD Learn_cmake_again Touch CMakeLists.txt Touch Tutoria. CPP
Then add content to the file:
2.8 ) #项目名project (Tutorial) #生成可执行程序 syntax: add_executable ( the CPP to be compiled by the executable program name) add_executable (Tutorial Tutorial. CPP)
//CPP File Contents//A simple program that computes the square root of a number#include <stdio.h>#include<stdlib.h>#include<math.h>intMain (intargcChar*argv[]) { if(ARGC <2) {fprintf (stdout,"Usage:%s number\n", argv[0]); return 1; } DoubleInputvalue = Atof (argv[1]); DoubleOutputvalue =sqrt (inputvalue); fprintf (stdout,"The square root of%g is%g\n", Inputvalue, Outputvalue); return 0;}
You can now try to compile the link into an executable program.
CMake. Make
./tutorial 16
The result is obviously right, haha, the next task is going to add a little variable to the program.
The content in CMakeLists.txt is:
Cmake_minimum_required (VERSION2.6) Project (Tutorial) # The version number.
# Set the version number to a variable, and its specific effect will be described in the sixth step of set (Tutorial_version_major1) Set (Tutorial_version_minor0) # Configure a headerfileTo pass some of the CMake settings# to the source code#.h.in file is a file that can be automatically compiled by using the following command to get the. h file
# The difference between Project_binary_dir and Project_source_dir can be viewed: http://blog.csdn.net/sukhoi27smk/article/details/ 46388711, this tutorial does not make a distinction between configure_file ("${project_source_dir}/tutorialconfig.h.in" "${project_binary_dir}/tutorialconfig.h") # Add the binary tree to the search path forinclude files# so, we'llFindTutorialconfig.hinclude_directories ("${project_binary_dir}") # Add the executableadd_executable (Tutorial tutorial.cpp)
//the contents of the CPP file//A simple program that computes the square root of a number#include <stdio.h>#include<stdlib.h>#include<math.h>#include"TutorialConfig.h" intMain (intargcChar*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; } DoubleInputvalue = Atof (argv[1]); DoubleOutputvalue =sqrt (inputvalue); fprintf (stdout,"The square root of%g is%g\n", Inputvalue, Outputvalue); return 0;}
// tutorialconfig.h.in (need to create new) file contents // The configured options and settings for Tutorial #define Tutorial_version_major @[email protected]#define tutorial_version_minor @[email protected]
And then:
CMake. Make
You will see the automatic generation of the TutorialConfig.h file, and at this time in the CPP can also print out the variables defined in the CMakeLists.txt, throughout, the. in file is equivalent to a bridge, the transfer of variables in the cmakelists to CPP.
It's not that easy to see a project CMakeLists.txt, often including the new library, and then the steps to link the library.
Step 2: Add a library
At this point we need to change the file structure,
Sub-directories
The following are the contents of each file:
#外部的CMakeLists. txtcmake_minimum_required (VERSION2.8) Project (Tutorial) # The version number.set (Tutorial_version_major1) Set (Tutorial_version_minor0) # Configure a headerfileTo pass some of the CMake settings# to the source Codeconfigure_file ("${project_source_dir}/tutorialconfig.h.in" "${project_binary_dir}/tutorialconfig.h") # Should we use our own math functions?
# The equivalent of a flagoption (Use_mymath"Use tutorial provided math implementation"On ) # Add the binary tree to the search path forinclude files# so, we'llFindtutorialconfig.h# Add the Mathfunctions library?#if(Use_mymath) include_directories ("${project_source_dir}/mathfunctions") add_subdirectory (mathfunctions) #将MathFunctions中的子目录也包含进去 set (Extra_libs ${extra_libs} mathfunctions) # Set syntax : Set (< variables > < values > < values >), assigning values to variables, multiple values, assigning multiple values to the variable endif (use_mymath) # Add the Executableadd_executable ( Tutorial Tutorial.CPP) target_link_libraries (Tutorial ${extra_libs})
//Tutorial.cpp's content//A simple program that computes the square root of a number#include <stdio.h>#include<stdlib.h>#include<math.h>#include"TutorialConfig.h"#ifdef use_mymath#include"MathFunctions.h"#endif intMain (intargcChar*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; } DoubleInputvalue = Atof (argv[1]);
Depending on the external situation, choose which part of the program, in the actual project often used, such as there are multiple versions of OPENCV, can be a number of choices, improve compatibility #ifdef Use_mymathDoubleOutputvalue =mysqrt (inputvalue);#else DoubleOutputvalue =sqrt (inputvalue);#endiffprintf (stdout,"The square root of%g is%g\n", Inputvalue, Outputvalue); return 0;}
// tutorialconfig.h.in file content,. h files are automatically generated without writing // The configured options and settings for Tutorial
Its role is mainly used to generate. h files, so specifically see. h file to realize its function can be #define tutorial_version_major @[email protected]#define Tutorial_version_minor @[email protected]#cmakedefine Use_mymath
#子文件夹中的CMakeLists. Txtadd_library (mathfunctions mysqrt. CPP)
// mathfunctions.h content #include < Iostream> #include <math.h>double mysqrt (double a);
// mysqrt.cpp content #include <iostream>#include<math.h>#include< mathfunctions.h>usingnamespace std; double mysqrt (double a) {cout<<"itsmy sqrt"< <Endl; return sqrt (a);}
Then CMake. And make, that's it.
CMake Learning under Ubuntu