1:CMake概述
说简单点,CMake 就是为我们生成 makefile 文件的,你是否在为撰写makefile而头疼呢,那就试试CMake吧,我也是一边学习,一边做一些笔记,难免会有一些理解错误的地方,还望各位看客及时指出,Thanks.
2:从“Hello world”开始
- [onezeroone@ ex-1]$ pwd
-
/home/onezeroone/work/backup/cmake/ex-1
-
[onezeroone@ ex-1]$ ls
-
hello.c
- #include <stdio.h>
-
int
-
main(void)
-
{
-
printf("Hello world\n");
-
-
return 0;
-
}
3: 撰写CMakeLists.txt
- [onezeroone@ ex-1]$ ls
-
CMakeLists.txt hello.c
-
[onezeroone@ ex-1]$ cat CMakeLists.txt
-
PROJECT(HELLO)
-
ADD_EXECUTABLE(hello hello.c)
CMakeLists.txt内容够简单吧,当然相对makefile来说。
PROJECT语法:
PROJECT(projectname [CXX] [C] [JAVA])
用于指定工程名字,[]为可选内容,默认表示支持所有语言。
ADD_EXECUTABLE(hello hello.c)
定义工程生产的可执行文件名为hello, 源文件为hello.c
4:执行cmake .
- [onezeroone@ ex-1]$ cmake .
-
-- The C compiler identification is GNU
-
-- The CXX compiler identification is GNU
-
-- Check for working C compiler: /usr/bin/gcc
-
-- Check for working C compiler: /usr/bin/gcc -- works
-
-- Detecting C compiler ABI info
-
-- Detecting C compiler ABI info - done
-
-- Check for working CXX compiler: /usr/bin/c++
-
-- Check for working CXX compiler: /usr/bin/c++ -- works
-
-- Detecting CXX compiler ABI info
-
-- Detecting CXX compiler ABI info - done
-
-- Configuring done
-
-- Generating done
-
-- Build files have been written to: /home/onezeroone/work/backup/cmake/ex-1
我们可以看到,cmake过程中为我们打印出了很多信息,现在我们不需要关心这些,没有ERROR就OK
- [onezeroone@ ex-1]$ ls
-
CMakeCache.txt CMakeFiles cmake_install.cmake CMakeLists.txt hello.c Makefile
我们可以看到cmake为我们生成了很多文件,我们只关心我们的Makefile,现在我们可以make了。
5.make
- [onezeroone@ ex-1]$ 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了
- [onezeroone@ ex-1]$ ls
-
CMakeCache.txt CMakeFiles cmake_install.cmake CMakeLists.txt hello hello.c Makefile
-
[onezeroone@ ex-1]$ ./hello
-
Hello world
当然,这只是最简单的cmake在Linux下的应用,后面我们继续学习我们的cmake.
阅读(350) | 评论(0) | 转发(0) |