CMAKE 教程前两章节学习
原文 https://cmake.org/cmake-tutorial/
以下是一个循序渐进的教程,它覆盖了CMAKE帮助改进的通常的构建系统的话题。许多话题在《掌握CMAKE》(《Mastering CMake》)已经作为单独话题介绍,但是在一个示例工程中运用它们会更有帮助于学习。教程可以再本项目开源代码Tests/Tutorial 目录下找到代码树。每一步操作都有包含完整代码的子目录。
第一步 基础起点
最基础的工程就是从代码中构建一个执行文件。对于简单工程来说,仅仅需要两行CMAKELISTS文件内容。 CMakeLists.txt内容看起来是这样:
cmake_minimum_required (VERSION 2.6)
project (Tutorial)
add_executable(Tutorial tutorial.cxx)
注意CMakeLists.txt文件中的例子使用的是小写命令。CMake支持大写、小写、混合大小写命令。tutorial.cxx 将计算一个数字的平方根。第一个版本的例子十分简单,如下:
// 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;
}
添加版本号和配置头文件
我们添加的第一个功能就是添加版本号。相比在代码中写入版本号,在CMakeLists.txt中提供此功能会更具有灵活性。 CMakeLists.txt文件中添加版本号修改如下:
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 that we will find TutorialConfig.h
include_directories("${PROJECT_BINARY_DIR}")
# add the executable
add_executable(Tutorial tutorial.cxx)
因为配置文件被写入到二进制文件树中我们必须将它加入到包含文件搜索路径中。我们创建一个TutorialConfig.h.in文件,它包含以下内容:
// the configured options and settings for Tutorial
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
当CMake配置这个头文件时,@Tutorial_VERSION_MAJOR@与 @Tutorial_VERSION_MINOR@的值将由CMakeLists.txt文件中的值来替代.当我们修改tutorial.cxx包含配置头文件并且版本号时候。代码如下:
// 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;
}
主要的改变在于添加了TutorialConfig.h头文件和将版本信息作为用户信息的一部分打印出来
//=========================================================
第二步 添加库
我们将添加一个库到我们的工程中。这个库包含我们自己实现的计算数字平方根的实现.执行文件能使用这个库来替代编译器提供的标准平方根函数。本教程中,我们将这个库放进一个子目录MathFunctions中。该目录包含一行内容的CMakeLists.txt 文件:
add_library(MathFunctions mysqrt.cxx)
源码文件mysqrt.cxx 有一个叫做mysqrt的函数,这个函数提供类型编译器的sqrt函数的功能。在顶层CMakeFiles.txt文件中调用我们加进子目录的库,以便于该库得到构造。我们同样使用另一个包含目录以便于MathFunctions/mysqrt.h 头文件能够找到函数原型。最后改变是添加这个新库到执行文件。顶层CMakeLists.txt文件最后几行看起来如下:
include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
add_subdirectory (MathFunctions)
# add the executable
add_executable (Tutorial tutorial.cxx)
target_link_libraries (Tutorial MathFunctions)
现在我们来考虑如何使得这个库能够变得可选。本教程中,没有任何理由需要如此,但是在第三方提供的较大型的库中,你可能需要如此。第一步是在CMakeLists.txt文件中添加一个选项
# should we use our own math functions?
option (USE_MYMATH
"Use tutorial provided math implementation" ON)
This will show up in the CMake GUI with a default value of ON that the user can change as desired. This setting will be stored in the cache so that the user does not need to keep setting it each time they run CMake on this project. The next change is to make the build and linking of the MathFunctions library conditional. To do this we change the end of the top level CMakeLists.txt file to look like the following:
一下显示了CMake如何在默认值工作,该值用户可以改变。这个设置将存储在缓存中以便用户不需要每次运行CMake都保持该设置。下一步是是的构建和连接该库可根据条件构建。顶层CMakeLists.txt文件中我们添加如下:
# 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})
USE_MYMATH的设定的使用决定MathFunctions是否编译和使用。
CMAKE 教程前两章节学习的更多相关文章
- sqlserver -- 学习笔记(七)获取同组数据的前两条记录
不啰嗦,直接上图,大概实现效果如下: 有上面这样一份数据,将他们按照userAccount和submitTime进行分组,然后提前每组数据的前两条记录 提取后数据如下: 实现的SQL如下: selec ...
- 深度学习与CV教程(8) | 常见深度学习框架介绍
作者:韩信子@ShowMeAI 教程地址:http://www.showmeai.tech/tutorials/37 本文地址:http://www.showmeai.tech/article-det ...
- 《Writing Idiomatic Python》前两部分的中文翻译
汇总了一下这本小书前两部分的内容: 翻译<Writing Idiomatic Python>(一):if语句.for循环 翻译<Writing Idiomatic Python> ...
- 学习javascript,您将发现以下两个学习指南
学习javascript,您将发现以下两个学习指南,一个是初学者的,另一个是茄子一号经验丰富的程序员和Web开发人员的.你想学习javascript并对它有兴趣.我想这就是你来这里的原因,你做了一个明 ...
- WORD中如何让前两页不显示页码
WORD中如何让前两页不显示页码 上稿人:ojn 点击率: 15191 我们有时在用word编辑文档时,会遇上第一.二页无需显示页码,第三页才是正文的第一页时,该如何正确插入页码呢? 以wor ...
- jsoi2014前两轮回眸
今天从常州回来了,第二轮考得惨不忍睹 大概来总结一下前两轮: 第一轮是4个小时,3道题,一道网络流,一道环形DP,一道线段树 最后一道题ahoi的原题(传送bzoj1798),非常水的线段树,是个很好 ...
- Js 正则表达式 写了一个正整数或小数点或分数前两个正则表达式
写了一个正整数或小数点或分数前两个正则表达式 /^[0-9]+([.]{1}[0-9]{1,2})? $/ 版权声明:本文博客原创文章.博客,未经同意,不得转载.
- 前两篇转载别人的精彩文章,自己也总结一下python split的用法吧!
前言:前两篇转载别人的精彩文章,自己也总结一下吧! 最近又开始用起py,是为什么呢? 自己要做一个文本相似度匹配程序,大致思路就是两个文档,一个是试题,一个是材料,我将试题按每题分割出来,再将每题的内 ...
- JavaBean的属性变量名前两个字母大小写问题
Java属性命名规范! 一般情况下.Java的属性变量名都已小写字母开头,如:userName,showMessage等,但也存在着特殊情况,考虑到一些特定的有意思的英文缩略词如(USA,XML等), ...
随机推荐
- 在VS2008中使用WSE 3.0【转】
原文:http://www.cnblogs.com/chenxizhang/archive/2008/07/25/1251626.html 在VS2008中使用WSE 3.0 WSE 是微软推出的一套 ...
- list_for_each与list_for_each_entry具体解释
一.list_for_each 1.list_for_each原型#define list_for_each(pos, head) \ for (pos = (head)->next, ...
- 不同复制模式下,如何忽略某些binlog事件
在MySQL复制中,如果slave节点上遇到错误,比如数据不存在或者主键冲突等错误时,想要忽略这些错误,可以采用以下几种方法: 1.未启用GTID模式时 只需通过设定 SQL_SLAVE_SKIP_C ...
- .net下所有DLL(API)查询,转换C#代码
地址: http://www.pinvoke.net/default.aspx/coredll.SetDevicePower 实例: SetDevicePower (coredll) coredl ...
- C# 播放器, 收藏
C#写了一个调用libvlc api实现的万能视频播放器 http://www.cnblogs.com/haibindev/archive/2011/12/21/2296173.html 引用库 ht ...
- 关于OkHttp–支持SPDY协议的高效HTTP库 com.squareup.okhttp
转载:http://liuzhichao.com/p/1707.html OkHttp–支持SPDY协议的高效HTTP库 柳志超博客 » Program » Andriod » OkHttp–支持SP ...
- 使用anaconda安装tensorflow (windows10环境)
版权声明:勤学 修德 明辨 笃实 - CSDN周雄伟 https://blog.csdn.net/ebzxw/article/details/80701613 已有环境:python3.7.1 ana ...
- rm 递归删除目录下某一类型文件
命令:find -name "*.js.map" | xargs rm -f 解释:find -name "*.js.map" 可以查到当前目录下(包括子目录, ...
- 匹配ip等的正则式
- Firemonkey Button 颜色
delphi FMX Firemonkey Button 按钮 颜色 TintColor 颜色 Button1.TintColor:=TAlphaColorRec.Green;