CMake 入门2/5:第一个程序 helloworld

CMake 入门1/5:基于阿里云 ECS搭建体验环境,我们搭建了 CMake的运行环境,下面我们以 helloworld 为例,体验 CMake 工作的基本流程。

1 源文件说明

共包含2个文件,一个 c++文件 helloworld.cpp,另一个是CMakeLists.txt。也可到这里下载:https://github.com/huangjunlei/How2Work/tree/master/DevOps/cmakedemo

1.1 helloworld.cpp

helloworld.cpp为你开发的源码文件,自由命名,自由书写内容。

#include<iostream>

int main(int argc, char *argv[]){
   std::cout << "Hello World!" << std::endl;
   return 0;
}
1.2 CMakeLists.txt

CMakeLists.txt为 cmake 工作的输入文件,命名固定,格式语法固定,以下为示例,实际开发中要根据工程结构来编写。

cmake_minimum_required(VERSION 2.8.9)
project (hello)
add_executable(hello helloworld.cpp)
第1行声明需要的最低 cmake版本

完整语法为
cmake_minimum_required(VERSION major[.minor[.patch[.tweak]]] [FATAL_ERROR])

扩展阅读:https://cmake.org/cmake/help/v2.8.12/cmake.html#command:cmake_minimum_required

第2行声明项目名

完整语法为: project(<projectname> [languageName1 languageName2 ... ] )

扩展阅读:https://cmake.org/cmake/help/v2.8.12/cmake.html#command:project

第3行声明执行体名称及源码列表

完整语法如下:

  add_executable(<name> [WIN32] [MACOSX_BUNDLE]
                 [EXCLUDE_FROM_ALL]
                 source1 source2 ... sourceN)

扩展阅读:https://cmake.org/cmake/help/v2.8.12/cmake.html#command:add_executable

2 编译过程

2.1 生成 Makefile

cmake 的命令形式为:

  cmake [options] <path-to-source>
  cmake [options] <path-to-existing-build>

扩展阅读:https://cmake.org/cmake/help/v2.8.12/cmake.html#section_Usage

Demo示例执行过程如下:

[root@myecs]# cmake .
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- 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
-- 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
-- Configuring done
-- Generating done
-- Build files have been written to: /home/cmakedemo/demo1
解释

首先,CMake检测运行环境,如没有 c++编译器会报附件三的错误;检测通过后,CMake 会生成工程所对应的 Makefile。需要强调的是,Makefile允许查看,但不要尝试编辑,而且下次执行时它也会被覆盖。

2.2 查看生成结果
[root@myecs]# ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  CMakeLists.txt  helloworld.cpp  Makefile
[root@myecs]# ls -l Makefile 
-rw-r--r-- 1 root root 4767 1月  30 20:04 Makefile
[root@myecs]# more Makefile 
# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 2.8
......
# because they might be regenerated.
cmake_check_build_system:
    $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
.PHONY : cmake_check_build_system
2.3 编译

在 Makefile 生成后,可以使用make 进行编译工程。

[root@myecs]# make
Scanning dependencies of target hello
[100%] Building CXX object CMakeFiles/hello.dir/helloworld.cpp.o
Linking CXX executable hello
[100%] Built target hello
2.4 查看编译结果
[root@myecs]# ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  CMakeLists.txt  hello  helloworld.cpp  Makefile
[root@myecs]# ls -l hello
-rwxr-xr-x 1 root root 9176 1月  30 20:13 hello
2.5 执行著名程序 helloworld
[root@myecs]# ./hello
Hello World!

至此 cmake 使用完整流程就结束了。当然对于 helloworld 这个程序,有些杀鸡用牛刀,但利于整体理解 cmake 的应用流程。

 3 相关文章

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值