CMake is an open-source cross platform build system, according to CMake's creator, Kitware.

But CMake is not actually  a build system. What CMake provides is an easy way to build C/C++ projects accross platform. CMake generates a cofiguration for your existing compiler system, e.g. make. CMake focus on cross platform configuration, dependecies, testing, packing, installation.

CMake also includes tools for finding libraries, e.g. boost. That make it simpler to build project that have external dependencites and by using the tools rather than hard coding paths.

Included with CMake is CTest, a test deriver program. That make it easy to run a project's test program and/or scripts.

Once you have configured your project to be installed, you can also package your project using the included CPack utility. A varity of packages can be created including tar files, zip files or an installer.

Example 1

CMakeLists.txt

# cmake_minimum_requried(VERSION version [FATAL_ERROR])
cmake_minimum_required(VERSION 2.8 FATAL_ERROR) # project(name)
project("To Do List") # add_executable(target sources...)
add_executable(toDo main.cc ToDo.cc)

you may notice that header files are not listed. CMake handles dependencies automatically, so headers dont' need to be listed

main.cc

cmake_minimum_required should be used in all top level CMakeLists.txt. If you are not sure which version to use, then use the version of CMake you have installed

#include "ToDo.h"

int main(int argc, char** argv){
ToDo list;
return ;
}

ToDo.h

#ifndef TODO_H
#define TODO_H class ToDo{
public:
ToDo();
~ToDo();
}; #endif // TODO_H

ToDo.cc

#include "ToDo.h"

ToDo::ToDo() { }
ToDo::~ToDo() { }

CMake's documentation suggest that out-of-source build be done rather than in-source build, so that we can clear build by simply delete the build folder.

> cd <project root directory>
> mkdir build
> cd build
> cmake -G "Unix Makefiles" .. # cmake -G <generator name> <path to soruce>
> ls
CMakeCache.txt CMakeFiles Makefile cmake_install.cmake
> make

The path of <path to source> contains your top level CMakeLists.txt.

cmake command generates serveral files, which should not be edited by hands.

  • Makefile is the most important one to us as we use it to build our projeect
  • CMakeCache.txt is important to CMake as it stores a varity of information and settings for the project.

Run make to build our target executable, based on the generated Makefile. Makefile suppress standard output. We may output detail by using:

> make VERBOSE=

If you modified the files you had used before, you simply need to run make again. The Makefile generated by CMake will automatically run cmake again if you modified your CMakeListx.txt.

Reference:

CMake Tutorial, JohnLamp.net

CMake Tutorial – Chapter 1: Getting Started, JohnLamp.net

[c++] Getting Started - CMake的更多相关文章

  1. 使用cmake自动构建工程

    公司引擎是用cmake根据目标平台来构建工程的,刚接触的时候深深体会到cmake的方便:如果目标平台是windows,它可以帮你自动构建出vs工程:如果是安卓,自动构建出eclipse工程,如果是IO ...

  2. CMake

    使用CMake编译跨平台静态库 http://www.tuicool.com/articles/3uu2Yj cmake命令 安装.用法简介 https://fukun.org/archives/04 ...

  3. CMake学习笔记

    C++开发者必备技能CMake  先简单介绍一下,CMake是一个跨平台的编译工具,它可以根据不用的平台,不同的编译环境,生成不同的MakeFile,从而控制编译的过程. 使用CMake的步骤: 1. ...

  4. VS 2013编译64位版本QT 4.8.6及使用cmake为依赖QT生成VS项目时Could NOT find Qt4

    对于一些已经解决的问题,本博客不再讨论.只将本人遇到的问题做简单的说明. 一.VS 2013编译64位版本QT 4.8.6 QT项目官网中,对于QT4,其只提供了windows X86的版本,并且支持 ...

  5. cmake cannot find package

    cmake 找不到package,如 find_package (OpenMesh REQUIRED) 出现错误 在项目的文件夹中找到 FindOpenMesh.cmake 文件,将其所在路径添加到 ...

  6. Cmake的交叉编译

    http://www.cmake.org/Wiki/CMake_Cross_Compiling

  7. CMake命令/函数汇总(翻译自官方手册)

    查看官方文档 cmake命令 选项 CMake变量 CMake命令汇总 / add_custom_command add_custom_target/add_definitions/add_depen ...

  8. 《CMake实践》笔记一:PROJECT/MESSAGE/ADD_EXECUTABLE

    <CMake实践>笔记一:PROJECT/MESSAGE/ADD_EXECUTABLE <CMake实践>笔记二:INSTALL/CMAKE_INSTALL_PREFIX &l ...

  9. 《CMake实践》笔记二:INSTALL/CMAKE_INSTALL_PREFIX

    <CMake实践>笔记一:PROJECT/MESSAGE/ADD_EXECUTABLE <CMake实践>笔记二:INSTALL/CMAKE_INSTALL_PREFIX &l ...

  10. 《CMake实践》笔记三:构建静态库(.a) 与 动态库(.so) 及 如何使用外部共享库和头文件

    <CMake实践>笔记一:PROJECT/MESSAGE/ADD_EXECUTABLE <CMake实践>笔记二:INSTALL/CMAKE_INSTALL_PREFIX &l ...

随机推荐

  1. 竞赛题解 - NOIP2018 赛道修建

    \(\mathcal {NOIP2018}\) 赛道修建 - 竞赛题解 额--考试的时候大概猜到正解,但是时间不够了,不敢写,就写了骗分QwQ 现在把坑填好了~ 题目 (Copy from 洛谷) 题 ...

  2. MySQL必知必会 读书笔记四:数据过滤

    过滤数据 WHERE 只检索所需数据需要指定搜索条件( search criteria) ,搜索条件也称为过滤条件( filtercondition) . 在SELECT语句中,数据根据WHERE子句 ...

  3. 结合Bootbox将后台JSON数据填充Form表单

    本文介绍了如何结合Bootbox将后台JSON数据填充到Form表单中,同时也介绍了一些需要使用的知识的学习途径,并附上了参考文档地址与学习网址,对此感兴趣的伙伴可以直接访问学习.为了方便介绍,使用了 ...

  4. asp.net core上使用Redis demo

    整体思路:(1.面向接口编程 2.Redis可视化操作IDE 3.Redis服务) [无私分享:ASP.NET CORE 项目实战(第十一章)]Asp.net Core 缓存 MemoryCache ...

  5. h5禁用手机input点击放大

    最近项目做的是h5的手机移动端,在用苹果浏览器测试时,弹出框输入信息会自动拉伸屏幕,并且不会像安卓一样回来. 网上查找说设置浏览器自适应头,但是并没有效果, <meta name="v ...

  6. 微信网页授权,错误40163,ios正确,安卓错误?

    2017-07-29:结贴昨天研究了半天,也没解决,看到出错的http头里面有PHPSESSID,回头去修改了一下程序里的session部分的代码(这部分代码在微信网页授权之后),,也不知道是腾讯那边 ...

  7. MapReduce输入输出的处理流程及combiner

    MapReduce 的输入输出 MapReduce 框架运转在<key,value> 键值对上,也就是说,框架把作业的输入看成是一组<key,value>键值对,同样也产生一组 ...

  8. 一、linux基本操作

    1.linux界面的切换 DOS界面终端打开:Ctrl+Alt+F1  /F2/F3   退出:Ctrl+Alt+F7 终端的打开与退出 打开:Ctrl+Alt+t   退出:Ctrl+d 2.第一次 ...

  9. VS中添加lib与dll

    参考与拓展阅读:https://blog.csdn.net/u012043391/article/details/54972127 lib: 1.附加包含目录---添加工程的头文件目录:       ...

  10. 广州Uber优步司机奖励政策(1月4日~1月10日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...