cmake从入门到精通 cmake ..有什么用


cmake从入门到精通 cmake ..有什么用

文章插图
概述
本项目的目的是逐步掌握cmake的使用,从最基本的单文件开始,到复杂工程的搭建能力 。
实践
案例1-单文件构建
参考:https://cmake.org/cmake-tutorial/
对应代码: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 #include #include int main (int argc, char *argv[]){ double inputValue; if(argc == 2) { inputValue = https://www.goobye.net/atof(argv[1]); } else { inputValue = 4; } double outputValue = sqrt(inputValue); fprintf(stdout,"The square root of %g is %gn", 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)
cmake从入门到精通 cmake ..有什么用

文章插图
project
参考地址:https://cmake.org/cmake/help/git-stage/command/project.html
Set the name of the project.
project(
[LANGUAGES] [...])project(
[VERSION [.[.
[.]]]] [LANGUAGES ...])
指定项目的名称 。项目最终编译生成的可执行文件并不一定是这个项目名称,而是由另一条命令(add_executable)确定的,稍候我们再介绍 。
add_executable
Add an executable to the project using the specified source files.
add_executable( [WIN32] [MACOSX_BUNDLE] [EXCLUDE_FROM_ALL] [source1] [source2 ...])
定义了这个工程会生成一个文件名为 name的可执行文件
案例2-单文件+版本号构建
参考:https://cmake.org/cmake-tutorial/
对应代码: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 #include #include #include "TutorialConfig.h"int main (int argc, char *argv[]){ fprintf(stdout,"%s Version %d.%dn", argv[0], Tutorial_VERSION_MAJOR, Tutorial_VERSION_MINOR); double inputValue; if(argc == 2) { inputValue = https://www.goobye.net/atof(argv[1]); } else { inputValue = 4; } double outputValue = sqrt(inputValue); fprintf(stdout,"The square root of %g is %gn", inputValue, outputValue); return 0;}
cmake从入门到精通 cmake ..有什么用

文章插图

解析
set
Set a normal, cache, or environment variable to a given value.
设置变量
参考:https://cmake.org/cmake/help/git-stage/command/set.html?highlight=set