cmake入门之hello world
作者:互联网
目录
准备文件
新建一个文件夹demo1,新建两个文件:CMakeLists.txt、helloworld.c
parallels@Lincoln:~/workspace/cmakedemo/demo1$ pwd
/home/parallels/workspace/cmakedemo/demo1
parallels@Lincoln:~/workspace/cmakedemo/demo1$ tree
.
├── CMakeLists.txt
└── helloworld.c
helloworld.c文件源码如下:
#include <stdio.h>
int main() {
printf("hello world\n");
return 0;
}
CMakeLists.txt:cmake的配置文件
#设置cmake的最小版本要求
cmake_minimum_required(VERSION 3.10)
#设置工程名字为 ylk_hello
project(ylk_hello)
#使用helloworld.c生成可执行程序hello
add_executable(hello helloworld.c)
使用cmake
1、生成配置
cmake . 其中 . 表示在当前目录搜索CMakeLists.txt(上面创建的)
parallels@Lincoln:~/workspace/cmakedemo/demo1$ cmake .
-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - 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
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/parallels/workspace/cmakedemo/demo1
查看结果
parallels@Lincoln:~/workspace/cmakedemo/demo1$ ll
total 48
drwxrwxr-x 3 parallels parallels 4096 Jan 2 10:55 ./
drwxrwxr-x 3 parallels parallels 4096 Jan 2 09:55 ../
-rw-rw-r-- 1 parallels parallels 14072 Jan 2 10:55 CMakeCache.txt
drwxrwxr-x 5 parallels parallels 4096 Jan 2 10:55 CMakeFiles/
-rw-rw-r-- 1 parallels parallels 1528 Jan 2 10:55 cmake_install.cmake
-rw-rw-r-- 1 parallels parallels 190 Jan 2 10:27 CMakeLists.txt
-rw-rw-r-- 1 parallels parallels 77 Jan 2 09:57 helloworld.c
-rw-rw-r-- 1 parallels parallels 5399 Jan 2 10:55 Makefile
可以看多了不少文件: 大概看一下就,没必要记住
CMakeCache.txt | 缓存相关文件,使得重新运行cmake时更快 |
CMakeFiles | 临时文件,用来检测操作系统、编译器等 |
cmake_install.cmake | 处理安装的规则,在install阶段会使用 |
Makefile | make命令的配置文件 |
2、构建
cmake --build . 会根据平台选择相应的生成器,我这边是ubuntu选择的是make命令
parallels@Lincoln:~/workspace/cmakedemo/demo1$ cmake --build .
Scanning dependencies of target hello
[ 50%] Building C object CMakeFiles/hello.dir/helloworld.c.o
[100%] Linking C executable hello
[100%] Built target hello
查看结果
生成了可执行文件hello,名字就是之前CMakeLists.txt中的add_executable(hello helloworld.c)的hello
3、运行查看结果
改善使用方式 out-of-source build
上面的使用方式,使得构建生成的文件和源代码混合在一起,耦合比较多不清晰,这种方式叫做:in-source build
下面看一下更通用的构建方式:源码和构建生成的文件分离,也叫做 out-of-source build
文件准备
新建文件夹demo2、复制demo1文件夹中的CMakeLists.txt和hellworld.c
新建build目录
生成配置
cd到build、执行cmake .. 其中..表示CMakeLists.txt在上层目录,也就是demo2
parallels@Lincoln:~/workspace/cmakedemo/demo2/build$ cmake ..
-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - 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
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/parallels/workspace/cmakedemo/demo2/build
构建
在build目录执行cmake --build .
可以看到生成了hello可执行文件。
查看运行结果
总结
从 构建文件 和 源代码 的位置 分成了:
- in-source build: 构建文件和源码混在一块(demo1例子)
- out-of-source build: 构建文件和源码分开(demo2例子)
一般更偏向于使用 out-of-source build
标签:cmake,parallels,--,build,world,Detecting,hello,compiler 来源: https://blog.csdn.net/java_yanglikun/article/details/112094005