序言
CMake 入门共分为五个小节, 规划如下:
基于阿里云 ECS 搭建体验环境
第一个程序 helloworld
体验有目录结构的项目构建
构建共享库
构建静态库
实验环境
本系列基于阿里云 ECS 环境进行, 具体基本信息如下:
ECS 配置
CPU 1 核, 内存 1G, 网络 1M, 磁盘 20G
登录后的信息
Welcome to Alibaba Cloud Elastic Compute Service !
OS 版本
- [root@myecs]#uname -a
- Linux 3.10.0-514.26.2.el7.x86_64 #1 SMP Tue Jul 4 15:04:05 UTC 2017 x86_64 GNU/Linux
- [root@myecs]# cat /etc/RedHat-release
- CentOS Linux release 7.2.1511 (Core)
OS 自带的 gcc 版本
- [root@myecs]# gcc --version
- gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-11)
- Copyright © 2015 Free Software Foundation, Inc.
包括没有适销性和某一专用目的下的适用性担保.
搭建体验环境
1. 安装 CMake
ECS 默认没有安装 CMake, 需自行安装.
安装命令
[root@myecs]#yum install cmake
安装结果检查
- [root@myecs]#cmake -version
- cmake version 2.8.12.2
详细过程输出参考, 参见附一.
2. 安装 gcc-c++
非必须, 但后续体验需要, 否则会遇到附三的错误信息.
安装命令
[root@myecs]# yum install gcc-c++
安装结果检查
[root@myecs]# g++ -v
使用内建 specs.
- COLLECT_GCC=g++
- COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-RedHat-Linux/4.8.5/lto-wrapper
目标: x86_64-RedHat-Linux
配置为:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-Bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-RedHat-Linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-RedHat-Linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-RedHat-Linux
线程模型: posix
gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
详细过程输出参考, 参见附二.
CMake 相关链接
官网下载地址: https://cmake.org/download/
在线帮助文档: https://cmake.org/documentation
关于 gcc-c++ https://pkgs.org/download/gcc-c++
附录
附一 CMake 安装过程
[root@myecs]# yum install cmake
已加载插件: fastestmirror
- Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
- base | 3.6 kB 00:00
- epel | 4.7 kB 00:00
- extras | 3.4 kB 00:00
- updates | 3.4 kB 00:00
- (1/6): base/7/x86_64/group_gz | 166 kB 00:00
- (2/6): extras/7/x86_64/primary_db | 156 kB 00:00
- (3/6): epel/x86_64/updateinfo | 956 kB 00:00
- (4/6): updates/7/x86_64/primary_db | 1.4 MB 00:00
- (5/6): base/7/x86_64/primary_db | 6.0 MB 00:00
- (6/6): epel/x86_64/primary_db | 6.6 MB 00:00
- Determining fastest mirrors
正在解决依赖关系
--> 正在检查事务
---> 软件包 cmake.x86_64.0.2.8.12.2-2.el7 将被 安装
--> 正在处理依赖关系 libarchive.so.13()(64bit), 它被软件包 cmake-2.8.12.2-2.el7.x86_64 需要
--> 正在检查事务
---> 软件包 libarchive.x86_64.0.3.1.2-10.el7_2 将被 安装
--> 解决依赖关系完成
依赖关系解决
================================================================================
Package 架构 版本 源 大小
================================================================================
正在安装:
cmake x86_64 2.8.12.2-2.el7 base 7.1 M
为依赖而安装:
libarchive x86_64 3.1.2-10.el7_2 base 318 k
事务概要
================================================================================
安装 1 软件包 (+1 依赖软件包)
总下载量: 7.4 M
安装大小: 27 M
- Is this ok [y/d/N]: y
- Downloading packages:
- (1/2): libarchive-3.1.2-10.el7_2.x86_64.rpm | 318 kB 00:00
- (2/2): cmake-2.8.12.2-2.el7.x86_64.rpm | 7.1 MB 00:00
- --------------------------------------------------------------------------------
总计 28 MB/s | 7.4 MB 00:00
- Running transaction check
- Running transaction test
- Transaction test succeeded
- Running transaction
正在安装 : libarchive-3.1.2-10.el7_2.x86_64 1/2
正在安装 : cmake-2.8.12.2-2.el7.x86_64 2/2
验证中 : cmake-2.8.12.2-2.el7.x86_64 1/2
验证中 : libarchive-3.1.2-10.el7_2.x86_64 2/2
已安装:
cmake.x86_64 0:2.8.12.2-2.el7
作为依赖被安装:
libarchive.x86_64 0:3.1.2-10.el7_2
完毕!
- [root@myecs]# cmake -version
- cmake version 2.8.12.2
附二 gcc-c++ 安装过程
[root@myecs]# yum install gcc-c++
已加载插件: fastestmirror
- base | 3.6 kB 00:00
- epel | 4.7 kB 00:00
- extras | 3.4 kB 00:00
- updates | 3.4 kB 00:00
- (1/3): extras/7/x86_64/primary_db | 156 kB 00:00
- (2/3): epel/x86_64/updateinfo | 954 kB 00:00
- (3/3): epel/x86_64/primary_db | 6.6 MB 00:00
- Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 gcc-c++.x86_64.0.4.8.5-36.el7 将被 安装
--> 正在处理依赖关系 libstdc++-devel = 4.8.5-36.el7, 它被软件包 gcc-c++-4.8.5-36.el7.x86_64 需要
--> 正在处理依赖关系 libstdc++ = 4.8.5-36.el7, 它被软件包 gcc-c++-4.8.5-36.el7.x86_64 需要
--> 正在处理依赖关系 gcc = 4.8.5-36.el7, 它被软件包 gcc-c++-4.8.5-36.el7.x86_64 需要
--> 正在检查事务
---> 软件包 gcc.x86_64.0.4.8.5-11.el7 将被 升级
---> 软件包 gcc.x86_64.0.4.8.5-36.el7 将被 更新
--> 正在处理依赖关系 libgomp = 4.8.5-36.el7, 它被软件包 gcc-4.8.5-36.el7.x86_64 需要
--> 正在处理依赖关系 cpp = 4.8.5-36.el7, 它被软件包 gcc-4.8.5-36.el7.x86_64 需要
--> 正在处理依赖关系 libgcc>= 4.8.5-36.el7, 它被软件包 gcc-4.8.5-36.el7.x86_64 需要
---> 软件包 libstdc++.x86_64.0.4.8.5-11.el7 将被 升级
---> 软件包 libstdc++.x86_64.0.4.8.5-36.el7 将被 更新
---> 软件包 libstdc++-devel.x86_64.0.4.8.5-36.el7 将被 安装
--> 正在检查事务
---> 软件包 cpp.x86_64.0.4.8.5-11.el7 将被 升级
---> 软件包 cpp.x86_64.0.4.8.5-36.el7 将被 更新
---> 软件包 libgcc.x86_64.0.4.8.5-11.el7 将被 升级
---> 软件包 libgcc.x86_64.0.4.8.5-36.el7 将被 更新
---> 软件包 libgomp.x86_64.0.4.8.5-11.el7 将被 升级
---> 软件包 libgomp.x86_64.0.4.8.5-36.el7 将被 更新
--> 解决依赖关系完成
依赖关系解决
================================================================================
Package 架构 版本 源 大小
================================================================================
正在安装:
gcc-c++ x86_64 4.8.5-36.el7 base 7.2 M
为依赖而安装:
libstdc++-devel x86_64 4.8.5-36.el7 base 1.5 M
为依赖而更新:
- cpp x86_64 4.8.5-36.el7 base 5.9 M
- gcc x86_64 4.8.5-36.el7 base 16 M
- libgcc x86_64 4.8.5-36.el7 base 102 k
- libgomp x86_64 4.8.5-36.el7 base 157 k
- libstdc++ x86_64 4.8.5-36.el7 base 304 k
事务概要
================================================================================
安装 1 软件包 (+1 依赖软件包)
升级 ( 5 依赖软件包)
总下载量: 31 M
- Is this ok [y/d/N]: y
- Downloading packages:
- Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
- (1/7): cpp-4.8.5-36.el7.x86_64.rpm | 5.9 MB 00:00
- (2/7): gcc-4.8.5-36.el7.x86_64.rpm | 16 MB 00:00
- (3/7): gcc-c++-4.8.5-36.el7.x86_64.rpm | 7.2 MB 00:00
- (4/7): libgcc-4.8.5-36.el7.x86_64.rpm | 102 kB 00:00
- (5/7): libstdc++-4.8.5-36.el7.x86_64.rpm | 304 kB 00:00
- (6/7): libgomp-4.8.5-36.el7.x86_64.rpm | 157 kB 00:00
- (7/7): libstdc++-devel-4.8.5-36.el7.x86_64.rpm | 1.5 MB 00:00
- --------------------------------------------------------------------------------
总计 46 MB/s | 31 MB 00:00
- Running transaction check
- Running transaction test
- Transaction test succeeded
- Running transaction
正在更新 : libgcc-4.8.5-36.el7.x86_64 1/12
正在更新 : libstdc++-4.8.5-36.el7.x86_64 2/12
正在安装 : libstdc++-devel-4.8.5-36.el7.x86_64 3/12
正在更新 : libgomp-4.8.5-36.el7.x86_64 4/12
正在更新 : cpp-4.8.5-36.el7.x86_64 5/12
正在更新 : gcc-4.8.5-36.el7.x86_64 6/12
正在安装 : gcc-c++-4.8.5-36.el7.x86_64 7/12
清理 : gcc-4.8.5-11.el7.x86_64 8/12
清理 : libstdc++-4.8.5-11.el7.x86_64 9/12
清理 : libgcc-4.8.5-11.el7.x86_64 10/12
清理 : cpp-4.8.5-11.el7.x86_64 11/12
清理 : libgomp-4.8.5-11.el7.x86_64 12/12
验证中 : cpp-4.8.5-36.el7.x86_64 1/12
验证中 : libgomp-4.8.5-36.el7.x86_64 2/12
验证中 : gcc-4.8.5-36.el7.x86_64 3/12
验证中 : libgcc-4.8.5-36.el7.x86_64 4/12
验证中 : gcc-c++-4.8.5-36.el7.x86_64 5/12
验证中 : libstdc++-4.8.5-36.el7.x86_64 6/12
验证中 : libstdc++-devel-4.8.5-36.el7.x86_64 7/12
验证中 : libgcc-4.8.5-11.el7.x86_64 8/12
验证中 : cpp-4.8.5-11.el7.x86_64 9/12
验证中 : libgomp-4.8.5-11.el7.x86_64 10/12
验证中 : gcc-4.8.5-11.el7.x86_64 11/12
验证中 : libstdc++-4.8.5-11.el7.x86_64 12/12
已安装:
gcc-c++.x86_64 0:4.8.5-36.el7
作为依赖被安装:
libstdc++-devel.x86_64 0:4.8.5-36.el7
作为依赖被升级:
- cpp.x86_64 0:4.8.5-36.el7 gcc.x86_64 0:4.8.5-36.el7
- libgcc.x86_64 0:4.8.5-36.el7 libgomp.x86_64 0:4.8.5-36.el7
- libstdc++.x86_64 0:4.8.5-36.el7
完毕!
附三 未安装 gcc-c++ 时可能遇到的报错信息
- [root@myecs]# cmake .
- -- The C compiler identification is GNU 4.8.5
- -- The CXX compiler identification is unknown
- -- 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
- CMake Error: your CXX compiler: "CMAKE_CXX_COMPILER-NOTFOUND" was not found. Please set CMAKE_CXX_COMPILER to a valid compiler path or name.
- -- Configuring incomplete, errors occurred!
- See also "/home/cmakedemo/demo1/CMakeFiles/CMakeOutput.log".
- See also "/home/cmakedemo/demo1/CMakeFiles/CMakeError.log".
来源: https://yq.aliyun.com/articles/689454