[置顶] CMake 实例学习(一) 开始



转载   http://blog.chinaunix.net/uid-25696269-id-603825.html

如有版权问题,请联系QQ: 858668791


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 identificationis GNU
  3. -- The CXX compiler identificationis GNU
  4. -- Checkfor working C compiler:/usr/bin/gcc
  5. -- Checkfor working C compiler:/usr/bin/gcc-- works
  6. -- Detecting C compiler ABI info
  7. -- Detecting C compiler ABI info- done
  8. -- Checkfor working CXX compiler:/usr/bin/c++
  9. -- Checkfor 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 writtento:/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.

你可能感兴趣的:(makefile,cmake,gnu)