介绍#

本节展示一个非常基本的hello world的例子。

本节中的文件如下:

A-hello-cmake$ tree
.
├── CMakeLists.txt
├── main.cpp
  • [CMakeLists.txt] - 包含你希望运行的 CMake 命令

    # Set the minimum version of CMake that can be used
    # To find the cmake version run
    # $ cmake --version
    cmake_minimum_required(VERSION 3.5)# Set the project name
    project (hello_cmake)# Add an executable
    add_executable(hello_cmake main.cpp)
    
  • [main.cpp-]一个简单的"Hello World"的C++文件。

    #include <iostream>int main(int argc, char *argv[])
    {std::cout << "Hello CMake!" << std::endl;return 0;
    }
    

概念#

CMakeLists.txt#

CMakeLists.txt是存储所有CMake命令的文件。当cmake在文件夹中运行时,它将查找此文件,如果不存在,cmake 将因错误退出。

最小 CMake 版本#

使用 CMake 创建项目时,你可以指定支持的最低版本的 CMake。

cmake_minimum_required(VERSION 3.5)

项目#

一个CMake构建文件可以包括一个项目名称,以便在使用多个项目时更容易引用某些变量。

project (hello_cmake)

创建可执行文件#

add_executable()命令规定,应从指定的源文件构建可执行文件,在此示例中是main.cpp。add_executable()函数的第一个参数是要构建的可执行文件的名称,第二个参数是要编译的源文件列表。

add_executable(hello_cmake main.cpp)
cmake_minimum_required(VERSION 2.6)
project (hello_cmake)
add_executable(${PROJECT_NAME} main.cpp)

二进制目录#

运行cmake命令的根文件夹或顶级文件夹称为CMAKE_BINARY_DIR,是所有二进制文件的根文件夹。CMake既支持就地构建和生成二进制文件,也支持在源代码外构建和生成二进制文件。

就地构建#

就地构建将会在与源代码相同的目录结构中生成所有临时文件。这意味着所有的Makefile和目标文件都散布在你的普通代码中。要创建就地构建目标,请在根目录中运行cmake命令。例如:

A-hello-cmake$ cmake .
-- The C compiler identification is GNU 4.8.4
-- The CXX compiler identification is GNU 4.8.4
-- 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/matrim/workspace/cmake-examples/01-basic/A-hello-cmakeA-hello-cmake$ tree
.
├── CMakeCache.txt
├── CMakeFiles
│   ├── 2.8.12.2
│   │   ├── CMakeCCompiler.cmake
│   │   ├── CMakeCXXCompiler.cmake
│   │   ├── CMakeDetermineCompilerABI_C.bin
│   │   ├── CMakeDetermineCompilerABI_CXX.bin
│   │   ├── CMakeSystem.cmake
│   │   ├── CompilerIdC
│   │   │   ├── a.out
│   │   │   └── CMakeCCompilerId.c
│   │   └── CompilerIdCXX
│   │       ├── a.out
│   │       └── CMakeCXXCompilerId.cpp
│   ├── cmake.check_cache
│   ├── CMakeDirectoryInformation.cmake
│   ├── CMakeOutput.log
│   ├── CMakeTmp
│   ├── hello_cmake.dir
│   │   ├── build.make
│   │   ├── cmake_clean.cmake
│   │   ├── DependInfo.cmake
│   │   ├── depend.make
│   │   ├── flags.make
│   │   ├── link.txt
│   │   └── progress.make
│   ├── Makefile2
│   ├── Makefile.cmake
│   ├── progress.marks
│   └── TargetDirectories.txt
├── cmake_install.cmake
├── CMakeLists.txt
├── main.cpp
├── Makefile

源外构建#

使用源外构建,你可以创建单个生成文件夹,该文件夹可以位于文件系统的任何位置。所有临时构建的目标文件都位于此目录中,以保持源码目录结构的整洁。要进行源外构建,请运行build文件夹中的cmake命令,并将其指向根CMakeLists.txt文件所在的目录。使用源外构建时,如果你想从头开始重新创建cmake环境,只需删除构建目录,然后重新运行cmake。

举个例子:

A-hello-cmake$ mkdir buildA-hello-cmake$ cd build/A-hello-cmake/build$ make ..
make: Nothing to be done for `..'.
matrim@freyr:~/workspace/cmake-examples/01-basic/A-hello-cmake/build$ cmake ..
-- The C compiler identification is GNU 4.8.4
-- The CXX compiler identification is GNU 4.8.4
-- 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/matrim/workspace/cmake-examples/01-basic/A-hello-cmake/buildA-hello-cmake/build$ cd ..A-hello-cmake$ tree
.
├── build
│   ├── CMakeCache.txt
│   ├── CMakeFiles
│   │   ├── 2.8.12.2
│   │   │   ├── CMakeCCompiler.cmake
│   │   │   ├── CMakeCXXCompiler.cmake
│   │   │   ├── CMakeDetermineCompilerABI_C.bin
│   │   │   ├── CMakeDetermineCompilerABI_CXX.bin
│   │   │   ├── CMakeSystem.cmake
│   │   │   ├── CompilerIdC
│   │   │   │   ├── a.out
│   │   │   │   └── CMakeCCompilerId.c
│   │   │   └── CompilerIdCXX
│   │   │       ├── a.out
│   │   │       └── CMakeCXXCompilerId.cpp
│   │   ├── cmake.check_cache
│   │   ├── CMakeDirectoryInformation.cmake
│   │   ├── CMakeOutput.log
│   │   ├── CMakeTmp
│   │   ├── hello_cmake.dir
│   │   │   ├── build.make
│   │   │   ├── cmake_clean.cmake
│   │   │   ├── DependInfo.cmake
│   │   │   ├── depend.make
│   │   │   ├── flags.make
│   │   │   ├── link.txt
│   │   │   └── progress.make
│   │   ├── Makefile2
│   │   ├── Makefile.cmake
│   │   ├── progress.marks
│   │   └── TargetDirectories.txt
│   ├── cmake_install.cmake
│   └── Makefile
├── CMakeLists.txt
├── main.cpp

在本教程的所有例子中,都将使用源外构建。

构建示例#

以下是构建此示例的输出:

$ mkdir build$ cd build$ cmake ..
-- The C compiler identification is GNU 4.8.4
-- The CXX compiler identification is GNU 4.8.4
-- 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: /workspace/cmake-examples/01-basic/hello_cmake/build$ make
Scanning dependencies of target hello_cmake
[100%] Building CXX object CMakeFiles/hello_cmake.dir/hello_cmake.cpp.o
Linking CXX executable hello_cmake
[100%] Built target hello_cmake$ ./hello_cmake
Hello CMake!

CMake基础 第1节 初识CMake相关推荐

  1. CMake基础 第8节 包含第三方库

    介绍# 几乎所有重要的项目都需要包含第三方库.头文件或程序.CMake支持使用find_package()函数查找这些工具的路径.这将从CMAKE_MODULE_PATH中的文件夹列表中搜索格式为Fi ...

  2. CMake基础 第7节 编译标志

    引言# CMake支持以多种不同方式设置编译标志: 使用target_compile_definitions()函数 使用CMAKE_C_FLAGS和CMAKE_CXX_FLAGS变量. 本教程中的文 ...

  3. CMake基础 第5节 安装项目

    介绍# 此示例说明如何生成make install目标以在系统上安装文件和二进制文件.这基于前面的共享库示例. 本教程中的文件如下: $ tree . ├── cmake-examples.conf ...

  4. CMake基础 第4节 动态库

    介绍# 继续展示一个hello world示例,它将首先创建并链接一个共享库. 这里还显示了如何创建别名目标 本教程中的文件如下: $ tree . ├── CMakeLists.txt ├── in ...

  5. CMake基础 第3节 静态库

    介绍# 继续展示一个hello world示例,它首先创建并链接一个静态库.这是一个简化示例,这里的库和二进制文件在同一个文件夹中.通常,这些将会被放到子项目中,这些内容将会在以后描述. 本教程中的文 ...

  6. CMake基础 第2节 分离编译

    介绍# 展示一个hello world示例,它使用不同的文件夹来存储源文件和头文件. 本教程中的文件包括: B-hello-headers$ tree . ├── CMakeLists.txt ├── ...

  7. CMake基础教程(3)cmake变量

    文章目录 1.自定义变量 2.变量引用方式 3.cmake内置变量(环境变量) 1.提供指示信息 2. 系统信息 3.控制选项 4.查看cmake的变量 几乎每一个编译工具,都支持设置变量,来传递编译 ...

  8. 学习C++:C++进阶(三)CMake基础篇---用一个小型项目了解CMake及环境构建

    V1.1 于2022年7月15日第二次修改:添加了比较多的解释图,解读了各类库的CMakelist.txt文件 目录 第一部分 基础篇(Basics) 1.0 本部分主要学什么(Intro) 1.1 ...

  9. 现代 CMake 简明教程(一)- CMake 基础

    系列文章目录 现代 CMake 简明教程(一)- CMake 基础 现代 CMake 简明教程(二)- 设计理念与使用 文章目录 系列文章目录 前言 CMake 基础 1. Modern CMake ...

最新文章

  1. java 之 异常
  2. [To be translated] Nova:libvirt image 的生命周期
  3. MFC串口通信上位机(采用静态库编译生成的)不能在其他电脑运行的问题
  4. TortoiseGit- 创建本地新分支,提交推送到远程,本地新分支合并到工作分支,提交到远程工作分支等。...
  5. FTP两种工作模式:主动模式(Active FTP)和被动模式(Passive FTP)
  6. windows副本不是正版怎么解决_解决Windows沙盒怎么联网问题
  7. arduino ps2摇杆程序_PS2手柄在arduino上进行测试,可用,供喜欢diy的朋友借鉴
  8. c++ map是有序还是无序的_go 学习笔记之数组还是切片都没什么不一样
  9. 20191228_Python语言课程设计
  10. 剑指offer 33 把数组排成最小的数
  11. IIS的状态代码(微软文档)
  12. OCS Inventory NG使用之win平台下的AGENT端安装与信息收集(一)
  13. asp.net删除cookie
  14. 昆仑通态通用版找不到驱动_昆仑通态无法连接1200
  15. 微信公众号开发系列三:响应关注和取关事件
  16. 江苏省基础云计算机服务平台,江苏省一师一优课、一课一名师活动-江苏省基础教育云计算服务平台.doc...
  17. php将一维数组转换成二维数组
  18. Mac下查看本机IP地址
  19. C++ 实现对选手、评委的计分
  20. Java并发(四)BlockingQueue的使用

热门文章

  1. 运维与自动化运维发展概括
  2. Matlab Tricks(二十九) —— 使用 deal 将多个输入赋值给多个输出
  3. sql基线建立-知识准备
  4. 利用自定义分页技术提高数据库性能
  5. 运算符之:5、位运算符(7个)
  6. PHP学习记录第一篇:Ubuntu14.04下LAMP环境的搭建
  7. 【数据结构与算法】之深入解析“丑数”的求解思路与算法示例
  8. 【数据结构与算法】之深入解析“平衡二叉树”的求解思路与算法示例
  9. LeetCode Algorithm 剑指 Offer 57 - II. 和为s的连续正数序列
  10. Jenkins 程序目录