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. React Native从零到一搭建开发环境

    React Native从零到一搭建开发环境 ReactNative环境搭建 安装Homebrew 安装rvm 安装nvm 安装node 安装react-native-cli 安装watchman i ...

  2. 更新UI放在主线程的原因

    1.在子线程中是不能进行UI 更新的,而可以立刻更新的原因是:子线程代码执行完毕了,又自动进入到了主线程,这中间的时间非常的短,让我们误以为子线程可以更新UI.如果子线程一直在运行,则无法更新UI,因 ...

  3. H5输入框在输入信息的时候 页面会变形 并且在页面不变形的时候 键盘会遮挡 输入框的解决办法

    $(document).ready(function () { $('body').css({'height':$(window).height()})});//这行是解决输入框在输入信息弹出键盘后页 ...

  4. 关于端口冲突的解决方式Error: listen EACCES 0.0.0.80

    笔者昨天下午临走前安装了vs 2017想要运行一下项目的NET后端来让本机的前端直接对接后端,但是没注意到运行vs后IIS直接占用了本机的80端口.第二天跑nodeJS的时候直接Error: list ...

  5. <CPP学习 第二天> 字符串的输入 及 String类

    今天简单的学习了字符串的输入以及C++的String类. 1.面向行的输入: getline(); getline()函数读取整行,通过回车键输入的换行符来确定输入结尾.要调用这种方法,可以使用cin ...

  6. 红帽RHEL6.8离线环境下升级到RHEL7.3

    Red Hat Enterprise Linux 7 (RHEL 7) 是第一个支持从前一个 RHEL 主发行版本(RHEL 6)进行原位(in-place)升级的 RHEL 主版本.原位升级(in- ...

  7. Web Services简单介绍

    Web Services简单介绍 Web Services入门 一.Web Services简介 1.什么是Web Services? Web Services 是应用程序组件 Web Service ...

  8. 小工具:生成半透明背景色的 CSS 代码,不影响子元素透明度

    工具:http://leegorous.net/tools/bg-alpha.html 工具介绍:http://leegorous.net/blog/2010/07/29/generate-css-c ...

  9. 模拟MBR Grub故障修复

    1.  MBR故障修复 备份 mkdir /pp mount /dev/sdb1 /pp dd if=/dev/sda of=/pp/mrb.bak bs=512 count=1   破坏mrb dd ...

  10. JavaScript预解析

    定义:JavaScript"预解析",可以理解为把变量或函数预先解析到它们被使用的环境中. 通俗点讲,即认为浏览器在正式运行JavaScript代码前, 第一步,会预先根据关键字v ...