[c++] Getting Started - CMake
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 – Chapter 1: Getting Started, JohnLamp.net
[c++] Getting Started - CMake的更多相关文章
- 使用cmake自动构建工程
公司引擎是用cmake根据目标平台来构建工程的,刚接触的时候深深体会到cmake的方便:如果目标平台是windows,它可以帮你自动构建出vs工程:如果是安卓,自动构建出eclipse工程,如果是IO ...
- CMake
使用CMake编译跨平台静态库 http://www.tuicool.com/articles/3uu2Yj cmake命令 安装.用法简介 https://fukun.org/archives/04 ...
- CMake学习笔记
C++开发者必备技能CMake 先简单介绍一下,CMake是一个跨平台的编译工具,它可以根据不用的平台,不同的编译环境,生成不同的MakeFile,从而控制编译的过程. 使用CMake的步骤: 1. ...
- 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的版本,并且支持 ...
- cmake cannot find package
cmake 找不到package,如 find_package (OpenMesh REQUIRED) 出现错误 在项目的文件夹中找到 FindOpenMesh.cmake 文件,将其所在路径添加到 ...
- Cmake的交叉编译
http://www.cmake.org/Wiki/CMake_Cross_Compiling
- CMake命令/函数汇总(翻译自官方手册)
查看官方文档 cmake命令 选项 CMake变量 CMake命令汇总 / add_custom_command add_custom_target/add_definitions/add_depen ...
- 《CMake实践》笔记一:PROJECT/MESSAGE/ADD_EXECUTABLE
<CMake实践>笔记一:PROJECT/MESSAGE/ADD_EXECUTABLE <CMake实践>笔记二:INSTALL/CMAKE_INSTALL_PREFIX &l ...
- 《CMake实践》笔记二:INSTALL/CMAKE_INSTALL_PREFIX
<CMake实践>笔记一:PROJECT/MESSAGE/ADD_EXECUTABLE <CMake实践>笔记二:INSTALL/CMAKE_INSTALL_PREFIX &l ...
- 《CMake实践》笔记三:构建静态库(.a) 与 动态库(.so) 及 如何使用外部共享库和头文件
<CMake实践>笔记一:PROJECT/MESSAGE/ADD_EXECUTABLE <CMake实践>笔记二:INSTALL/CMAKE_INSTALL_PREFIX &l ...
随机推荐
- iOS | NSProxy
Objective-C作为一种动态消息型语言,其机制不同于Java ,C#等编译型语言. 它将数据类型的确定等工作推迟到了运行时期来执行,并且它调用方法的方式实质是像对象发送消息,根据selector ...
- LeetCode 中级 - 组合总和II(105)
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...
- mac 无法写入设备的最后一个块 格式化
硬盘,U盘,装在硬盘盒通过USB连接到电脑.但是无法格式化硬盘 失败的页面显示: 正在卸载磁盘 无法写入设备的最后一个块 操作失败 建议您这样做: 1.切换进Windows系统,或者找一台安装有Win ...
- python函数名应用
函数名的应用 函数名 的应用分类: 函数就是一个特殊的变量(可以看成一个变量来用) *函数名对应函数的内存地址 *函数名可以做为容器类数据的元素 *函数名可以作为函数的参数 *函数名可以作为函数的返回 ...
- Git命令行和Xcode结合使用(我来告诉你这行代码谁写的)
现在一直使用Git来管理代码,对于有强迫症的我来说,依旧选择了命令行,下面这段话可以更好的解释我为什么喜欢使用终端敲命令. There are a lot of different ways to u ...
- ethereum(以太坊)(实例)--"安全的远程购买"
pragma solidity ^0.4.10; contract Safebuy{ uint public price; address public seller; address public ...
- .Net Core使用Redis-从安装到使用
一.安装 本文使用的操作系统是Centos7 在Redis中文网下载最新的Redis压缩包:http://www.redis.cn/ 把包上传到Liunx服务器上,cd 到包所在的目录执行以下命令 # ...
- php bug 调试助手 debug_print_backtrace()
debug_print_backtrace() 是一个很低调的函数,很少有人注意过它. 不过当我对着一个对象调用另一个对象再调用其它的对象和文件中的一个函数出错时,它也许正在一边笑呢 如果我们想知道某 ...
- Python学习笔记四:列表,购物车程序实例
列表 切片 中括号,逗号分隔,可以一次取出多个元素,起始位置包括,结束位置不包括(顾头不顾尾) 如果取最后一个,而且不知道列表长度,可以使用负数(-1是最后一个,以此类推) 如果取最后几个,记住从左往 ...
- C语言Windows程序开发—MessageBox函数介绍【第01天】
(一)MessageBox函数的参数介绍: int MessageBox ( HWND hWnd, //弹出MessageBox对话框所属的窗口句柄 LPCTSTR lpText, //指向Messa ...