zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Use CMake notes

2023-04-18 15:16:09 时间

Whay need the CMake?

如果只是构建一个只有一个main.cpp的小型项目,那么确实不需要CMake, 直接GCC、G++编译,或者写个build.sh脚本即可, 不需要把简单的问题搞复杂化。

$ g++ main.cpp -o cmake_hello

但是如果你的项目分了很多模块,导入了很第三方源码、库文件等,那么编译也是个复杂问题,之前的先驱们为了将编译这一复杂过程封装起来,就发明了Makefile,过了一段时间,人们觉得Makefile还是过于复杂,又在基于Makeile的基础上又封装出了CMake,这下现在的人就能使用几条简单的语法就能编译一个复杂工程了。

My goal of using CMake

我现在使用CMake还没有遇到高级的需求,都是做一些简单的事情,比如:

Compilation record in Windows

  1. 环境安装记录
  1. 编译记录
  • 在项目根目录下写好CMakeLists.txt后
  • 打开Cygwin64 Terminal
  • cd d:/ (在cygwin下转到D盘)
  • 进到你的项目
  • 执行编译命令
$ cmake CMakeLists.txt
// You will notice that, cmake is identifying compiler versions and configurations with default information.
$ make all
#or
$ make cmake_hello
  • 进入~your projectcmake-build-debug
./main.exe

以上的步骤,在Clion里只需要Shift + F10 即可。

Reference

CMake Turorial
https://medium.com/@onur.dundar1/cmake-tutorial-585dd180109b