跨平台的自动化建构系统:cmake入门

前面有一篇文章介绍了autotools使用入门(http://www.linuxeden.com/html/develop/20090612 /66167.html),autotools的强大毋庸置疑,但是他们通常太复杂,生成一大堆数据文件而且这些文件与C源文件混杂在一起看起来无比混 乱。而且autotools依赖于shell,这些配置文件在没有 Bourne Shell或者类似标准/bin/sh的平台上是无效的。
    从2001年诞生到现在,cmake已经在KDE4,rosegarden等大型项目完全使用cmake来进行构建证明了cmake的可用性和大项目管理 能力。正是由于autotools的复杂性使得KDE项目中只有少数几个“编译专家”能够掌握 KDE现在的构建体系,引用一位KDE开发者的话:“Cmake不再使你在构建项目时郁闷地想自杀了”。最终促成了KDE转而使用cmake来构建项目。
废话不多说,开始我们的cmake入门标准程序“helloworld”。首先当然要确认你的linux是否安装了cmake,目前cmake已经成为各 大Linux发行版提供的标准组件因此绝大部分linux都默认安装了cmake。只需在终端输入命令cmake或者cmake --help,输出一大堆cmake说明就证明cmake已经安装在你的电脑,如果报错command not found的话使用yum/apt等工具安装即可,想源码编译就直接去http://www.cmake.org官方网站下载安装。
   
    新建目录mkdir cmake,然后在cmake目录中新建hello.c文件,内容如下:

//hello.c
#include <stdio.h>
int main()
{
     printf(“Hello World !!!!!!!!!!!!!!!!!!/n”);
     return 0;
}

 

    接着在cmake目录中新建CMakeLists.txt文件,内容如下:

PROJECT (HELLO)
ADD_EXECUTABLE(hello hello.c)   



    最后在cmake目录中新建一个目录build,并进入build目录进行编译,命令如下:
$ mkdir build
$ cd build
$ cmake ..

-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /usr/lib/ccache/gcc
-- Check for working C compiler: /usr/lib/ccache/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/lib/ccache/c++
-- Check for working CXX compiler: /usr/lib/ccache/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- This is SOURCE file hello.c
-- This is BINARY dir /home/mgqw/Desktop/test/cmake/build
-- This is SOURCE dir /home/mgqw/Desktop/test/cmake
-- Configuring done
-- Generating done
-- Build files have been written to: /home/mgqw/Desktop/test/cmake/build



$ make

Scanning dependencies of target hello
[100%] Building C object CMakeFiles/hello.dir/hello.c.o
Linking C executable hello
[100%] Built target hello



$ ./hello

Hello World !!!!!!!!!!!



    所有步骤就这样全部完成了,是不是比autotools要简单得多?所有生成的数据文件全部在build目录下(在哪个目录运行cmake命令那些数据文 件就生成在哪里,只需要说明CMakeLists.txt路径就行),不像autotools那样和源码文件混杂在一起,看得人头晕。
    当然并不是说有了cmake就要放弃autotools,cmake和autotools各有各的优点什么时候该用什么,自己去衡量。
    上面的步骤只是简单介绍了cmake的入门使用过程,其中具体含义以及其他功能上百度/狗狗一搜一大把,记住百度/狗狗永远是我们最好的老师。

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值