CMake入门-01-从HelloWorld开始
工作环境
- 系统:macOS Mojave 10.14.6
- CMake: Version 3.15.0-rc4
从 Hello,World! 开始
(1) 新建 hello 目录,创建文件 CMakeLists.txt、main.cpp
$ mkdir hello
$ cd hello
$ touch CMakeLists.txt main.cpp
$ ll
-rw-r--r-- 1 staff staff 124B 8 14 17:19 CMakeLists.txt
-rw-r--r--@ 1 staff staff 145B 8 14 21:33 main.cpp
(2) 编写程序代码 main.cpp,其代码如下:
#include <iostream>
using namespace std;
int main(int argc, char const *argv[]) {
/* code */
cout << "Hello,World!" << endl;
return 0;
}
(3) 编写 CMakeLists.txt,如下:
# cmake 最低版本需求,不加入此行会受到警告信息
cmake_minimum_required(VERSION 3.15.0)
# 项目名设置为 hello
project(hello)
# 将当前目录中的源文件名称赋值给变量 SRC_LIST
aux_source_directory(. SRC_LIST)
# 指示变量 SRC_LIST 中的源文件需要编译成一个名称为 hello 的可执行文件
add_executable(hello ${SRC_LIST})
(4) 执行 cmake & make 编译
# 进入 hello 目录
$ pwd
/Users/staff/Desktop/hello
$ ll
-rw-r--r--@ 1 staff staff 124B 8 14 17:19 CMakeLists.txt
-rw-r--r--@ 1 staff staff 145B 8 14 21:33 main.cpp
# 执行 cmake . 命令
$ cmake .
-- The C compiler identification is AppleClang 10.0.1.10010046
-- The CXX compiler identification is AppleClang 10.0.1.10010046
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/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: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/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: /Users/staff/Desktop/hello
# 执行 make 命令
$ make
Scanning dependencies of target hello
[ 50%] Building CXX object CMakeFiles/hello.dir/main.cpp.o
[100%] Linking CXX executable hello
[100%] Built target hello
# 运行我们的 hello 程序
$ ./hello
Hello,World!
(5) 外部编译方式
cmake 可以直接在当前目录进行编译,但是,这种做法会将所有生成的中间文件和源代码混在一起,而且 cmake 生成的 makefile 无法跟踪所有的中间文件,即不容易将所有的中间文件删除。
- 之前编译方式的目录(中间文件和源代码混在一起):
$ pwd
/Users/staff/Desktop/hello
$ ll
-rw-r--r-- 1 staff staff 13K 8 14 21:50 CMakeCache.txt
drwxr-xr-x 12 staff staff 384B 8 14 21:51 CMakeFiles
-rw-r--r--@ 1 staff staff 124B 8 14 17:19 CMakeLists.txt
-rw-r--r-- 1 staff staff 4.7K 8 14 21:50 Makefile
-rw-r--r-- 1 staff staff 1.3K 8 14 21:50 cmake_install.cmake
-rwxr-xr-x 1 staff staff 18K 8 14 21:51 hello
-rw-r--r--@ 1 staff staff 145B 8 14 21:33 main.cpp
$ ./hello
Hello,World!
- 使用外部编译方式:
$ pwd
/Users/staff/Desktop/hello
$ ll
-rw-r--r--@ 1 staff staff 124B 8 14 17:19 CMakeLists.txt
-rw-r--r--@ 1 staff staff 145B 8 14 21:33 main.cpp
# 在 hello 文件夹中新建 build 文件夹
$ mkdir build
$ cd build
$ pwd
/Users/staff/Desktop/hello/build
# 在 hello/build 文件夹中,执行 cmake & make 编译
$ cmake ..
-- The C compiler identification is AppleClang 10.0.1.10010046
-- The CXX compiler identification is AppleClang 10.0.1.10010046
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/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: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/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: /Users/hubin/Desktop/hello/build
$ make
Scanning dependencies of target hello
[ 50%] Building CXX object CMakeFiles/hello.dir/main.cpp.o
[100%] Linking CXX executable hello
[100%] Built target hello
$ ./hello
Hello,World!
- 外部编译方式的目录为:
$ pwd
/Users/staff/Desktop/hello
$ tree .
.
├── CMakeLists.txt
├── build
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ │ ├── 3.15.0-rc4
│ │ │ ├── ... 省略
│ │ └── progress.marks
│ ├── Makefile
│ ├── cmake_install.cmake
│ └── hello
└── main.cpp
相关参考:
CMake 官方网站
联系作者:
CMake入门-01-从HelloWorld开始的更多相关文章
- CMake入门-03-还是HelloWorld
工作环境 系统:macOS Mojave 10.14.6 CMake: Version 3.15.0-rc4 Hello,World! 扩展-math 目录里的文件编译成静态库再由 main 函数调用 ...
- CMake入门教程(转帖)
本文转自:https://www.cnblogs.com/never--more/p/6921837.html CMake入门教程 参考文献:http://www.ibm.com/developerw ...
- CMake入门(二)
CMake入门(二) 最后更新日期:2014-04-25 by kagula 阅读前提:<CMake入门(一)>.Linux的基本操作 环境: Windows 8.1 64bit英文版.V ...
- CMake入门
CMake入门 CMake是一个跨平台的安装编译工具,可以用简单的语句来描述所有平台的安装(编译过程).他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似 ...
- C++ CMake 入门实战[转载]
C++ CMake 入门实战 2016-11-05 CMake用于跨平台的编译系统,对于通常的c/c++工程,都是通过make来进行编译的,CMake可以通过指令生成Makefile文件来指导整个项目 ...
- VScode 使用 CMake 入门
参考 CMake 入门实战 在 linux 平台下使用 CMake 生成 Makefile 并编译的流程如下: 编写 CMake 配置文件 CMakeLists.txt . 执行命令 cmake PA ...
- iOS系列 基础篇 01 构建HelloWorld,剖析并真机测试
iOS基础 01 构建HelloWorld,剖析并真机测试 前言: 从控制台输出HelloWorld是我们学习各种语言的第一步,也是我们人生中非常重要的一步. 多年之后,我希望我们仍能怀有学习上进的心 ...
- CMake 入门实战 | HaHack
CMake 入门实战 | HaHack undefined
- React-Native入门指南之HelloWorld
iOS React-Native入门指南之HelloWorld React-native 作为facebook开源项目,最近是火的一塌糊涂,它采用node.js能够写ios和android的nativ ...
- 不用搭环境的10分钟AngularJS指令简易入门01(含例子)
不用搭环境的10分钟AngularJS指令简易入门01(含例子) `#不用搭环境系列AngularJS教程01,前端新手也可以轻松入坑~阅读本文大概需要10分钟~` AngularJS的指令是一大特色 ...
随机推荐
- 安卓APP在线升级
安卓APP在线升级 通过IDHTTP组件在线下载APP到手机中,然后自动安装这个APP程序. 1)在线下载APP程序 需引用单元: {$IFDEF ANDROID} FMX.Helpers.Andro ...
- Git如何永久删除某个重要文件文件或文件夹 (包括历史记录) 强制
有些时候不小心上传了一些敏感文件(例如密码), 或者不想上传的文件(没及时或忘了加到.gitignore里的), 而且上传的文件又特别大的时候, 这将导致别人clone你的代码或下载zip包的时候也必 ...
- 微信小程序之圆形进度条(自定义组件)
思路 使用2个canvas 一个是背景圆环,一个是彩色圆环. 使用setInterval 让彩色圆环逐步绘制. 在看我的文章前,必须先看 ,下面转的文章,因为本文是在它们基础上修改的. 它们的缺点为: ...
- 阶段5 3.微服务项目【学成在线】_day17 用户认证 Zuul_05-用户认证-认证服务查询数据库-调用查询用户接口
用户认证服务调用根据账号查询用户的信息 怎么远程调用呢?要创建一个客户端,这个客户端其实就是一个接口 标明服务的名称是ucenter服务 这是ucenter服务里面 复制过来接口的定义,GetMapp ...
- 阶段5 3.微服务项目【学成在线】_day16 Spring Security Oauth2_15-认证接口开发-Redis配置
4.2 Redis配置 4.2.1 安装Redis 1.安装Redis服务 下载Windows版本的redis:https://github.com/MicrosoftArchive/redis/ta ...
- kafka和zookeeper的配置文件优化配置
zookeeper的配置 日志自动清理这两个参数都是在zoo.cfg中配置的: autopurge.purgeInterval 这个参数指定了清理频率,单位是小时,需要填写一个1或更大的整数,默 ...
- java 利用poi 实现excel合并单元格后出现边框有的消失的解决方法
使用工具类RegionUtil CellRangeAddress cra = new CellRangeAddress(nowRowCount, nowRowCount + followSize-1, ...
- 123456---com.twoapp.huanYingMotro--- 幻影摩托
123456---com.twoapp.huanYingMotro--- 幻影摩托
- (二)UML之类图、接口、包
一.概念 类图(Class Diagram): 类图是面向对象系统建模中最常用和最重要的图,是定义其它图的基础.类图主要是用来显示系统中的类.接口以及它们之间的静态结构和关系的一种静态模型. 类图的3 ...
- 使用redis做为MySQL的缓存-C语言编写UDF
介绍 在实际项目中,MySQL数据库服务器有时会位于另外一台主机,需要通过网络来访问数据库:即使应用程序与MySQL数据库在同一个主机中,访问MySQL也涉及到磁盘IO操作(MySQL也有一些数据预读 ...