1.最简实例

  使用cmake的最简实例是由一个源程序文件生成一个可执行文件。例如由下述C++源程序文件生成可执行文件tutorial。

  main.cpp

#include<iostream>
using namespace std;
int main(){ cout<<"hello world"<<endl;
}

  需要编辑CMakeLists.txt文件如下:

cmake_minimum_required(VERSION 2.6)
project (tutorial)
add_executable(tutorial main.cpp)

  其中cmake_minimum_required指定了cmake最低版本限制,project指定了项目名称,add_executable指定了生成的可执行文件名称为tutorial,源程序文件为main.cpp。

  需要生成项目可以cd到此目录,然后依次执行下述命令:

cmake .
make

  可见,cmake帮助我们快速生成了项目的makefile文件,从而可以通过make命令直接生成项目的可执行文件。此时,在该路径下就生成了可执行文件tutorial,执行该程序,有:

  使用cmake就是如此简单快捷!

2.设置版本号和配置头文件

  如果在cmake中指定版本号这样更加灵活,我们可以这么操作:通过CMakeLists.txt文件指定版本号,然后通过CMake链接到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}/config.h.in"
"${PROJECT_BINARY_DIR}/config.h"
) # add the binary tree to the search path for include files
# so that we will find config.h
include_directories("${PROJECT_BINARY_DIR}") # add the executable
add_executable(Tutorial main.cpp)

  config.h.in 中引用了CMakeLists.txt中定义的两个版本号,如下所示:

#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@

  main.cpp文件直接引用config.h头文件中定义的版本号,如下所示:

#include "config.h"
#include<iostream>
using namespace std;
int main(){
cout<<"Tutorial_VERSION_MAJOR: "<<Tutorial_VERSION_MAJOR<<endl;
cout<<"Tutorial_VERSION_MINOR: "<<Tutorial_VERSION_MINOR<<endl;
cout<<"hello world"<<endl;
return ; }

  此时,执行命令cmake不仅生成了makefile文件,还链接生成了config.h文件,如下所示:

#define Tutorial_VERSION_MAJOR 1
#define Tutorial_VERSION_MINOR 0

  执行命令make,然后顺利生成了可执行文件Tutorial,执行该文件有:

  完美!

3.在项目中添加可选库

  在大型项目中,我们需要根据实际情况决定到底引用哪个库中的函数实现。我们可以借助CMake便捷的实现库的可选性。

  如下,设置CMakeLists.txt文件如下所示:

# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)
# 项目信息
project (Tutorial)
# 是否使用自己的 MathFunctions 库 这是可选的,ON使用,OFF不使用
option (USE_MYMATH "Use provided math implementation" ON)
# 加入一个配置头文件,用于处理 CMake 对源码的设置,与option相对应
configure_file (
"${PROJECT_SOURCE_DIR}/config.h.in"
"${PROJECT_BINARY_DIR}/config.h"
) # 是否加入 MathFunctions 库
if (USE_MYMATH)
include_directories ("${PROJECT_SOURCE_DIR}/math")
add_subdirectory (math)
set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions )
endif (USE_MYMATH) # 查找当前目录下的所有源文件
# 并将名称保存到 DIR_SRCS 变量
aux_source_directory(. DIR_SRCS) # 指定生成目标
add_executable (Tutorial ${DIR_SRCS})
target_link_libraries (Tutorial ${EXTRA_LIBS})

  相对应的,为了使得CMake能够自动的根据option自动生成相对应的头文件config.h,我们编辑config.h.in文件如下:

#cmakedefine USE_MYMATH

  当我们设置option为ON的时候,CMake链接生成的config.h头文件如下所示:

#define USE_MYMATH

  所以,我们可以在主程序源文件中根据USE_MYMATH是否定义来决策是否使用自定义库。主源程序文件main.cpp如下所示:

#include <stdio.h>
#include <stdlib.h>
#include "config.h" #ifdef USE_MYMATH
#include "math/MathFunctions.h"
#else
#include <math.h>
#endif int main(int argc, char *argv[])
{ double base = 2.0;
int exponent = ; #ifdef USE_MYMATH
printf("Now we use our own Math library. \n");
double result = power(base, exponent);
#else
printf("Now we use the standard library. \n");
double result = pow(base, exponent);
#endif printf("%g ^ %d is %g\n", base, exponent, result);
return ;
}

  我们自行定义的库在子文件夹math中,其中主要有两个文件,分别是头文件和源程序文件,如下所示:

  MathFunctions.h

#ifndef POWER_H
#define POWER_H extern double power(double base, int exponent); #endif

  MathFunctions.cpp

/**
* power - Calculate the power of number.
* @param base: Base value.
* @param exponent: Exponent value.
*
* @return base raised to the power exponent.
*/
double power(double base, int exponent)
{
int result = base;
int i; for(i = ; i < exponent; ++i){
result = result * base;
} return result;
}

  为了打包生成库文件,也需要在库文件夹中定义CMakeLists.txt文件,如下所示:

# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_LIB_SRCS 变量
aux_source_directory(. DIR_LIB_SRCS) # 指定生成 MathFunctions 链接库
add_library (MathFunctions ${DIR_LIB_SRCS})

  最后,测试一下我们的项目,执行命令:

cmake .
make

  项目顺利编译通过,执行生成的可执行文件Tutorial,有:

  pass!

  

CMake Tutorial的更多相关文章

  1. CMake Tutorial & Example

    Tutorial CMakeLists用于告诉CMake我们要对这个目录下的文件做什么事情 cmake 的特点主要有: 1,开放源代码,使用类 BSD 许可发布.http://cmake.org/HT ...

  2. ubuntu 16.04 上编译和安装C++机器学习工具包mlpack并编写mlpack-config.cmake | tutorial to compile and install mplack on ubuntu 16.04

    本文首发于个人博客https://kezunlin.me/post/1cd6a04d/,欢迎阅读最新内容! tutorial to compile and install mplack on ubun ...

  3. [cmake] Basic Tutorial

    Basic Project The most basic porject is an executable built from source code file. CMakeLists.txt cm ...

  4. CMake使用教程

    转自 RichardXG 原文 CMake使用教程 CMake是一个比make更高级的编译配置工具,它可以根据不同平台.不同的编译器,生成相应的Makefile或者vcproj项目. 通过编写CMak ...

  5. CMake入门指南-编译教程

    CMake是一个比make更高级的编译配置工具,它可以根据不同平台.不同的编译器,生成相应的Makefile或者vcproj项目.通过编写CMakeLists.txt,可以控制生成的Makefile, ...

  6. CMake入门指南

    原文地址:http://www.cnblogs.com/sinojelly/archive/2010/05/22/1741337.html CMake是一个比make更高级的编译配置工具,它可以根据不 ...

  7. CMake Intro - CMakeLists.txt

    Notes:  directory structure:  cmake, cmake/Tutorial, cmake/Tutorial/MathLibs 1. File lists in cmake/ ...

  8. 基于Cmake+QT+VS的C++项目构建开发编译简明教程

    目录 一.工具下载与安装 1.     Qt 2.     Visual Studio 2015 3.     Cmake 二.C++及Qt项目构建 1.     基于VS构建Qt项目 2.     ...

  9. CMake 用法导览

    Preface : 本文是CMake官方文档CMake Tutorial (http://www.cmake.org/cmake/help/cmake_tutorial.html) 的翻译.通过一个样 ...

随机推荐

  1. 解读QML之二

    QML文档 QML文档是用QML语法组成的字符串.一个文档定义了一个QML对象类型.文档以”.qml”最为后缀,可以保存在本地和网络上,可以使用代码生成.一 个在文档中定义的对象类型的实例,也可以使用 ...

  2. Hadoop集群搭建的密钥配置SSH实现机制的配置(2)

    [hadoop@weekend110 ~]$ ssh-keygen -t rsa 用来生产密钥对 Generating public/private rsa key pair. Enter file ...

  3. USACO Section 1.2 Dual Palindromes 解题报告

    题目 题目描述 有一些数(如 21),在十进制时不是回文数,但在其它进制(如二进制时为 10101)时就是回文数. 编一个程序,从文件读入两个十进制数N.S.然后找出前 N 个满足大于 S 且在两种以 ...

  4. sql server 2008 把远程的数据库的数据转移到本地数据数据库里

    如题:把远程的数据库对应表里的数据转移到本地数据数据库的对应表里 比如把192.168.188.160的DB的A表的数据转移到本地的DB的A表里 第一步:连接远程服务器前准备 exec sp_addl ...

  5. ubuntu安装mysql数据库

    http://www.cnblogs.com/zhuyp1015/p/3561470.html http://www.2cto.com/database/201401/273423.html http ...

  6. BootStrap中关于input-group的问题(未解决)

    ****************************************2016年1月6日 23:08******************************* 本来想实现的如下功能: 但 ...

  7. web beacon

    网络臭虫又叫 网络信标(Web beacon), 是可以暗藏在任何网页元素或邮件内的1像素大小的透明GIF或PNG图片 可以理解为<img src="http://www.webbea ...

  8. Persistent Bookcase

    Persistent Bookcase time limit per test 2 seconds memory limit per test 512 megabytes input standard ...

  9. Trim(',')的作用去除最有一个','

    public bool XMLDataImport()        { List<string> sqllist = new List<string>();          ...

  10. [Big Data]hadoop分布式系统在Linux系统安装

    待完善 Hadoop2.6.0部署与应用 一.    环境准备 硬件:3台虚拟机 内存:至少512MB 硬盘:至少20G 操作系统:rhel6.4  64位 最小化安装 主机名 IP(局域网) 描述 ...