CMake入门

CMake入门教程

参考文献:http://www.ibm.com/developerworks/cn/linux/l-cn-cmake/index.html

官方网址:http://www.cmake.org/

下载网址: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应用程序。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值