文章插图
概述
本项目的目的是逐步掌握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
解析
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(
[LANGUAGES] [
[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
文章插图
解析
set
Set a normal, cache, or environment variable to a given value.
设置变量
参考:https://cmake.org/cmake/help/git-stage/command/set.html?highlight=set
- 【游戏世界】王者荣耀:马可波罗为什么能一直火?从技能就可以看出策划偏心
- 【历史故事】知青蒋小磊:从北京知青到上门女婿,他心中有说不出的无奈和心酸
- 手游魅者入门级攻略
- 进口红酒适合做什么生意 如何从法国进口红酒来中国销售
- 面相分析夫妻缘分 从夫妻宫来看(图文)
- 葡萄酒让法国成为 法国从文艺复兴时期开始种植酿酒葡萄
- 体相分析-如何从脖子看你是否富贵?(图文)
- 王相山《坤卦的智慧》从哲学角度认识坤卦
- 开放生二胎,你要知道的易学知识!
- 从窝瓜到南瓜 窝瓜和南瓜的区别