CMake_Demo

Ref

Ref.1 (99+ 封私信 / 80 条消息) CMake 如何入门? - 知乎 (zhihu.com)
Ref.2 CMake Tutorial — CMake 3.27.2 Documentation

Demo

如何使用CMake工具构建C++工程,本文介绍了一个简单的demo,完成从C++源文件到可执行文件构建的过程。

创建CMakeList.txt文件

CMakeList.txt

# specify the CMAKE tool version
cmake_minimum_required(VERSION 3.14.7)

# specify the project name
project(move)

# specify the target
add_executable(move testMove.cpp)

构建build文件

执行以下命令

root@localhost:~/testWorkSpace$ cmake -B build -S .
-- Configuring done
-- Generating done
-- Build files have been written to: /home/lki8szh/testWorkSpace/build
root@localhost:~/testWorkSpace$ cmake -B build -S .
CMake Error at CMakeLists.txt:1 (cmake_minimum_required):
  CMake 3.20 or higher is required.  You are running version 3.14.7


-- Configuring incomplete, errors occurred!

如上述内容提示,CMakeList.txt 文件中“”指定CMake工具的版本,若工具版本与其不符合,会提示错误。
修改CMakeList.txt文件,执行如下命令,生成成功。

root@localhost:~/testWorkSpace$ cmake -B build -S .
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.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/lki8szh/testWorkSpace/build

编译C++源文件

root@localhost:~/testWorkSpace$ cmake --build ./build/
-- Configuring done
-- Generating done
-- Build files have been written to: /home/lki8szh/testWorkSpace/build
Scanning dependencies of target move
[ 50%] Building CXX object CMakeFiles/move.dir/testMove.cpp.o
[100%] Linking CXX executable move
[100%] Built target move

执行目标文件

root@localhost:~/testWorkSpace$ ./build/move
After copy, str is "Hello"
After move, str is ""
The contents of the vector are "Hello", "Hello"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值