BAT架構(gòu)師資料下載:https://github.com/0voice/from_coder_to_expert
[toc]
概述
本項(xiàng)目的目的是逐步掌握cmake的使用,從最基本的單文件開始,到復(fù)雜工程的搭建能力。
實(shí)踐
案例1-單文件構(gòu)建
參考:https://cmake.org/cmake-tutorial/
對(duì)應(yīng)代碼:01-Tutorial
The most basic project is an executable built from source code files. For simple projects a two line CMakeLists.txt file is all that is required. This will be the starting point for our tutorial. The CMakeLists.txt file looks like:
cmake_minimum_required (VERSION 2.8)
project (01-Tutorial)
add_executable(tutorial 01-Tutorial.cpp)
Note that this example uses lower case commands in the CMakeLists.txt file. Upper, lower, and mixed case commands are supported by CMake. The source code for tutorial.cxx will compute the square root of a number and the first version of it 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[])
{
double inputValue;
if(argc == 2)
{
inputValue = atof(argv[1]);
}
else
{
inputValue = 4;
}
double outputValue = sqrt(inputValue);
fprintf(stdout,"The square root of %g is %g\n",
inputValue, outputValue);
return 0;
}
解析
cmake_minimum_required
Set the minimum required version of cmake for a project.
cmake_minimum_required(VERSION major[.minor[.patch[.tweak]]]
[FATAL_ERROR])
比如
cmake_minimum_required (VERSION 2.8)
project
參考地址:https://cmake.org/cmake/help/git-stage/command/project.html
Set the name of the project.
project(<PROJECT-NAME> [LANGUAGES] [<language-name>...])
project(<PROJECT-NAME>
[VERSION <major>[.<minor>[.<patch>[.<tweak>]]]]
[LANGUAGES <language-name>...])
指定項(xiàng)目的名稱寻拂。項(xiàng)目最終編譯生成的可執(zhí)行文件并不一定是這個(gè)項(xiàng)目名稱晨川,而是由另一條命令(add_executable)確定的刷喜,稍候我們?cè)俳榻B荣回。
add_executable
Add an executable to the project using the specified source files.
add_executable(<name> [WIN32] [MACOSX_BUNDLE]
[EXCLUDE_FROM_ALL]
[source1] [source2 ...])
定義了這個(gè)工程會(huì)生成一個(gè)文件名為 name的可執(zhí)行文件
案例2-單文件+版本號(hào)構(gòu)建
參考:https://cmake.org/cmake-tutorial/
對(duì)應(yīng)代碼:02-Tutorial
The first feature we will add is to provide our executable and project with a version number. While you can do this exclusively in the source code, doing it in the CMakeLists.txt file provides more flexibility. To add a version number we modify the CMakeLists.txt file as follows:
cmake_minimum_required (VERSION 2.8)
project (02-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 that we will find TutorialConfig.h
include_directories("${PROJECT_BINARY_DIR}")
# add the executable
add_executable(tutorial 02-Tutorial.cpp)
Since the configured file will be written into the binary tree we must add that directory to the list of paths to search for include files. We then create a TutorialConfig.h.in file in the source tree with the following contents:
// the configured options and settings for Tutorial
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
When CMake configures this header file the values for @Tutorial_VERSION_MAJOR@ and @Tutorial_VERSION_MINOR@ will be replaced by the values from the CMakeLists.txt file. Next we modify tutorial.cxx to include the configured header file and to make use of the version numbers. The resulting source code is listed below.
// 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[])
{
fprintf(stdout,"%s Version %d.%d\n",
argv[0],
Tutorial_VERSION_MAJOR,
Tutorial_VERSION_MINOR);
double inputValue;
if(argc == 2)
{
inputValue = atof(argv[1]);
}
else
{
inputValue = 4;
}
double outputValue = sqrt(inputValue);
fprintf(stdout,"The square root of %g is %g\n",
inputValue, outputValue);
return 0;
}
解析
set
Set a normal, cache, or environment variable to a given value.
設(shè)置變量
參考:https://cmake.org/cmake/help/git-stage/command/set.html?highlight=set
- Set Normal Variable
set(<variable> <value>... [PARENT_SCOPE]) - Set Cache Entry
set(<variable> <value>... CACHE <type> <docstring> [FORCE]) - Set Environment Variable
set(ENV{<variable>} <value>...)
PROJECT_SOURCE_DIR
Top level source directory for the current project.
即是工程的頂級(jí)目錄
PROJECT_BINARY_DIR
Full path to build directory for project.
即是編譯目錄,比如如果你創(chuàng)建了build目錄(cd build和cmake ..)雹锣,則路徑為:
PROJECT_SOURCE_DIR/build
如果在工程頂級(jí)目錄直接進(jìn)行編譯(cmake .)則和PROJECT_SOURCE_DIR一致
案例3-單文件+庫文件調(diào)用
頂層目錄內(nèi)的文件內(nèi)容先編譯庫文件
庫文件放在MathFunctions目錄网沾。
Now we will add a library to our project. This library will contain our own implementation for computing the square root of a number. The executable can then use this library instead of the standard square root function provided by the compiler. For this tutorial we will put the library into a subdirectory called MathFunctions. It will have the following one line CMakeLists.txt file:
- 添加CMakeLists.txt
cmake_minimum_required (VERSION 2.8)
add_library(MathFunctions mysqrt.cpp)
- 添加頭文件MathFunctions.h
#ifndef __MATH_FUNCTION_H__
#define __MATH_FUNCTION_H__
double mysqrt(double input);
#endif
- 添加實(shí)現(xiàn)文件
#include <math.h>
#include <stdio.h>
double mysqrt(double input)
{
printf("call mysqrt\n");
return sqrt(input);
}
此時(shí)目錄文件為:- 創(chuàng)建build目錄并進(jìn)行編譯
mkdir build
cd build
cmake ..
make
此時(shí)build目錄下生成libMathFunctions.a文件,將其拷貝到上一級(jí)目錄(即是MathFunctions)
cp libMathFunctions.a ../
cd ..
ls
#可以看到當(dāng)前l(fā)ibMathFunctions目錄的內(nèi)容
CMakeLists.txt MathFunctions.cpp MathFunctions.h build libMathFunctions.a
編譯main函數(shù)所在文件
- 頂層目錄CMakeLists.txt文件
cmake_minimum_required (VERSION 2.8)
project (03-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 that we will find TutorialConfig.h
include_directories("${PROJECT_BINARY_DIR}")
include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
add_subdirectory (MathFunctions)
# add the executable
add_executable(tutorial 03-Tutorial.cpp)
target_link_libraries (tutorial MathFunctions)
- main函數(shù)所在文件
03-Tutorial.cpp
// A simple program that computes the square root of a number
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "TutorialConfig.h"
#include "MathFunctions.h"
int main (int argc, char *argv[])
{
fprintf(stdout,"%s Version %d.%d\n",
argv[0],
Tutorial_VERSION_MAJOR,
Tutorial_VERSION_MINOR);
double inputValue;
if(argc == 2)
{
inputValue = atof(argv[1]);
}
else
{
inputValue = 4;
}
double outputValue = mysqrt(inputValue);
fprintf(stdout,"The square root of %g is %g\n",
inputValue, outputValue);
return 0;
}
- 編譯和執(zhí)行
mkdir build
cd build
cmake ..
make
執(zhí)行文件./tutorial
lqf@ubuntu:/mnt/hgfs/linux/multimedia/src/project/cmake_learn/03-Tutorial/build$ ./tutorial
./tutorial Version 1.0
call mysqrt
The square root of 4 is 2
參考文檔
[1] cmake-tutorial