CMake入门教程
参考文献:http://www.ibm.com/developerworks/cn/linux/l-cn-cmake/index.html
下载网址:http://www.cmake.org/download/
当前版本:3.1rc3
本文基于CMake 2.8版本编写。
CMake 是一个跨平台的,开源的构建系统(BuildSystem)。CMake 可以通过 CMakeLists.txt 文件来产生特定平台的标准的构建文件,例如:为 Unix 平台生成makefiles文件(使用GCC编译),为 Windows MSVC 生成 projects/workspaces(使用VS IDE编译)或Makefile文件(使用nmake编译)。
CMake Hello World
首先编写一个简单的程序(hello.cpp):
#include <stdio.h>
int main()
{
printf("Hello World");
return 0;
}
编写CMakeList.txt,并与hello.cpp放在同一个目录。
project(hello)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. DIR_SRCS)
add_executable(hello ${DIR_SRCS})
在 CMakeLists.txt 所在的目录下创建一个build目录,进入该目录执行 CMake 命令生成构建文件:
打开Visual Studio 2008命令行提示窗口,它会执行加载一些VS的环境变量。
mkdir build
cd build
cmake -G "NMake Makefiles" ../
-- The C compiler identification is MSVC 15.0.30729.1
-- The CXX compiler identification is MSVC15.0.30729.1
….
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to:I:/cmake-hello/build
这里使用的平台为 Windows 并且安装了 VS2008,CMake 为我们生成了 VS2008的构建文件Makefile,我们可以使用nmake来构建应用程序,执行:
nmake
Microsoft (R) Program Maintenance UtilityVersion 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.
Scanning dependencies of target hello
[100%] Building CXX objectCMakeFiles/hello.dir/hello.cpp.obj
hello.cpp
Linking CXX executable hello.exe
[100%] Built target hello
编译成功完成后,会当前目录下生成hello应用程序。