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. 剑指offer反转链表

    way1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 3 ...

  2. 转 如何使用V7包中ActionBar(Eclipse版)

    http://blog.csdn.net/appte/article/details/11712591 以前3.0以前的版本要使用ActionBar,必须使用国外大牛写的ActionBarSherlo ...

  3. 转: 两个 Shell 网站: explainshell 和 shellcheck

    今天向大家介绍两个有意思的 Shell 网站,一个是 explainshell.com,另一个是 shellcheck.net. explainshell 先说 explainshell.explai ...

  4. Hibernate配置过程可能发生的问题及解决方法

    1.问题:Exception in thread "main" java.lang.NoClassDefFoundError: org/dom4j/DocumentExceptio ...

  5. CentOS 修改DNS,固定IP等操作(网络)

    1.修改DNS 修改对应网卡的DNS的配置文件 vi /etc/resolv.conf 内容格式(西工大) nameserver 114.114.114.114 nameserver 202.117. ...

  6. ubuntu配置openvpn

    http://www.zhixing123.cn/ubuntu/ubuntu-openvpn-settings-tutorial.html 本文介绍Ubuntu连接使用OpenVPN方法教程,Ubun ...

  7. 【转】SSL/TLS/WTLS协议原理

    1 SSL(Secure Socket Layer)是netscape公司设计的主要用于web的安全传输协议.这种协议在WEB上获得了广泛的应用.2 IETF(www.ietf.org )将SSL作了 ...

  8. SDAU课程练习--problemA(1000)

    题目描述 The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape i ...

  9. CodeForces 139C Literature Lesson(模拟)

    这个题,读懂了就是水,读不懂就没办法下手,论英语阅读的重要性...只有五种形式,第一种万能型aaaa,是另外3种的特殊情况,第二种克莱里林四行打油诗aabb形式,第三种是交替的abab形式,第四种是封 ...

  10. Codevs 5590 A+B 问题 超级版

    5590 A+B 问题 超级版 时间限制: 1 s 空间限制: 1000 KB 题目等级 : 青铜 Bronze 题目描述 Description 不用+-*/%计算A+B 输入描述 Input De ...