cmake入门1之hello world

3 篇文章 0 订阅

目录

环境

准备文件

使用cmake

1、生成配置

2、构建

3、运行查看结果

改善使用方式 out-of-source build

文件准备

构建

查看运行结果

总结


环境

ubuntu18

cmake version 3.17.3

准备文件

新建一个文件夹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阶段会使用
Makefilemake命令的配置文件

 

 

 

 

 

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

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值