Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6707
  • 博文数量: 6
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 20
  • 用 户 组: 普通用户
  • 注册时间: 2015-04-22 00:14
文章分类

全部博文(6)

文章存档

2015年(6)

我的朋友
最近访客

分类: LINUX

2015-11-13 10:17:25

1:CMake概述
   说简单点,CMake 就是为我们生成 makefile 文件的,你是否在为撰写makefile而头疼呢,那就试试CMake吧,我也是一边学习,一边做一些笔记,难免会有一些理解错误的地方,还望各位看客及时指出,Thanks.

2:从“Hello world”开始
  1. [onezeroone@ ex-1]$ pwd
  2. /home/onezeroone/work/backup/cmake/ex-1
  3. [onezeroone@ ex-1]$ ls
  4. hello.c

  1. #include <stdio.h>
  2. int
  3. main(void)
  4. {
  5.         printf("Hello world\n");

  6.         return 0;
  7. }
3: 撰写CMakeLists.txt
  1. [onezeroone@ ex-1]$ ls
  2. CMakeLists.txt hello.c
  3. [onezeroone@ ex-1]$ cat CMakeLists.txt
  4. PROJECT(HELLO)
  5. ADD_EXECUTABLE(hello hello.c)
CMakeLists.txt内容够简单吧,当然相对makefile来说。
PROJECT语法:
PROJECT(projectname [CXX] [C] [JAVA])
用于指定工程名字,[]为可选内容,默认表示支持所有语言。

ADD_EXECUTABLE(hello hello.c)
定义工程生产的可执行文件名为hello, 源文件为hello.c

4:执行cmake .
  1. [onezeroone@ ex-1]$ cmake .
  2. -- The C compiler identification is GNU
  3. -- The CXX compiler identification is GNU
  4. -- Check for working C compiler: /usr/bin/gcc
  5. -- Check for working C compiler: /usr/bin/gcc -- works
  6. -- Detecting C compiler ABI info
  7. -- Detecting C compiler ABI info - done
  8. -- Check for working CXX compiler: /usr/bin/c++
  9. -- Check for working CXX compiler: /usr/bin/c++ -- works
  10. -- Detecting CXX compiler ABI info
  11. -- Detecting CXX compiler ABI info - done
  12. -- Configuring done
  13. -- Generating done
  14. -- Build files have been written to: /home/onezeroone/work/backup/cmake/ex-1
我们可以看到,cmake过程中为我们打印出了很多信息,现在我们不需要关心这些,没有ERROR就OK
  1. [onezeroone@ ex-1]$ ls
  2. CMakeCache.txt CMakeFiles cmake_install.cmake CMakeLists.txt hello.c Makefile
我们可以看到cmake为我们生成了很多文件,我们只关心我们的Makefile,现在我们可以make了。

5.make
  1. [onezeroone@ ex-1]$ make
  2. Scanning dependencies of target hello
  3. [100%] Building C object CMakeFiles/hello.dir/hello.c.o
  4. Linking C executable hello
  5. [100%] Built target hello
我们可以看到成功生成hello文件,至此我们可以我们的hello world了
  1. [onezeroone@ ex-1]$ ls
  2. CMakeCache.txt CMakeFiles cmake_install.cmake CMakeLists.txt hello hello.c Makefile
  3. [onezeroone@ ex-1]$ ./hello
  4. Hello world
当然,这只是最简单的cmake在Linux下的应用,后面我们继续学习我们的cmake.
阅读(309) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~