CMakeList.txt的简单使用

CMake 简介

CMake 是一个跨平台的自动化建构系统,它使用一个名为 CMakeLists.txt 的文件来描述构建过程,可以产生标准的构建文件,如 Unix 的 Makefile 或Windows Visual C++ 的 projects/workspaces 。文件 CMakeLists.txt 需要手工编写,也可以通过编写脚本进行半自动的生成。CMake 提供了比 autoconfig 更简洁的语法。在 linux 平台下使用 CMake 生成 Makefile 并编译的流程如下:

  • 1)编写 CmakeLists.txt。
  • 2)执行命令“cmake PATH”或者“ccmake PATH”生成 Makefile ( PATH 是 CMakeLists.txt
    所在的目录 )。
  • 3)使用 make 命令进行编译。

第一个工程

源代码分布情况:

./eg_demo/main.cpp

现假设我们的项目中只有一个源文件 main.cpp
源文件main.cpp内容:

#include <iostream>
int main()
{std::cout<<"Hello world"<<std::endl;return 0;
}

:为了构建该项目,我们需要编写文件 CMakeLists.txt 并将其与 main.cpp 放在 同一个目录下。
CMakeLists.txt内容为:

PROJECT(Hello)
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
AUX_SOURCE_DIRECTORY(. DIR_SRCS)
ADD_EXECUTABLE(main ${DIR_SRCS})

CMakeLists.txt 的语法比较简单,由命令、注释和空格组成,其中命令是不区分大小写的,符号"#"后面的内容被认为是注释。命令由命令名称、小括号和参数组成,参数之间使用空格进行间隔。例如对于清单2的 CMakeLists.txt 文件:第一行是一条命令,名称是 PROJECT ,参数是 Hello,该命令表示项目的名称是 Hello 。第二行的命令限定了 CMake 的版本。第三行使用命令 AUX_SOURCE_DIRECTORY 将当前目录中的源文件名称赋值给变量 DIR_SRCS 。 CMake 手册中对命令 AUX_SOURCE_DIRECTORY 的描述如下:aux_source_directory(<dir> <variable>)该命令会把参数 <dir>中所有的源文件名称赋值给参数<variable>。 第四行使用命令 ADD_EXECUTABLE 指示变量 DIR_SRCS 中的源文件需要编译 成一个名称为 main 的可执行文件。
CMake的编译基本就两个步骤:
cmake 指向CMakeLists.txt所在的目录,例如cmake … 表示CMakeLists.txt在当前目录的上一级目录。cmake后会生成很多编译的中间文件以及makefile文件,所以一般建议新建一个新的目录,专门用来编译,例如

mkdir build
cd build
cmake ..
make

make根据生成makefile文件,编译程序。

camke 的运行结果:

[eg_demo]$ mkdir build
[eg_demo]$ cd build/
[build]$ 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
-- Detecting C compile features
-- Detecting C compile features - 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
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /eg_demo/build
[build]$ make
make: Warning: File `Makefile' has modification time 969 s in the future
make[1]: Warning: File `CMakeFiles/Makefile2' has modification time 969 s in the future
make[2]: Warning: File `CMakeFiles/main.dir/flags.make' has modification time 969 s in the future
Scanning dependencies of target main
make[2]: warning:  Clock skew detected.  Your build may be incomplete.
make[2]: Warning: File `CMakeFiles/main.dir/flags.make' has modification time 969 s in the future
[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[100%] Linking CXX executable main
make[2]: warning:  Clock skew detected.  Your build may be incomplete.
[100%] Built target main
make[1]: warning:  Clock skew detected.  Your build may be incomplete.
make: warning:  Clock skew detected.  Your build may be incomplete.
[build]$ ./main
Hello world

第二个工程

源代码分布情况:

./eg_demo/main.cpp 、test.cpp 、test.h

主函数main.cpp:

#include <iostream>
#include "test.h"
int main()
{std::cout<<"Hello world"<<std::endl;print_test();return 0;
}

test.cpp内容:

#include "test.h"void print_test()
{std::cout<< "src:test"<<std::endl;
}

test.h中内容:

#include <iostream>
void print_test();

CMakeLists.txt内容:

PROJECT(Hello)
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
AUX_SOURCE_DIRECTORY(. DIR_SRCS)
ADD_EXECUTABLE(main ${DIR_SRCS})

注:和第一个项目一致
camke 的运行结果:

mkdir build
[eg_demo2]$ cd build/
[build]$ 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
-- Detecting C compile features
-- Detecting C compile features - 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
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: eg_demo2/build
[build]$ make
make: Warning: File `Makefile' has modification time 970 s in the future
make[1]: Warning: File `CMakeFiles/Makefile2' has modification time 970 s in the future
make[2]: Warning: File `CMakeFiles/main.dir/flags.make' has modification time 970 s in the future
Scanning dependencies of target main
make[2]: warning:  Clock skew detected.  Your build may be incomplete.
make[2]: Warning: File `CMakeFiles/main.dir/flags.make' has modification time 970 s in the future
[ 33%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[ 66%] Building CXX object CMakeFiles/main.dir/test.cpp.o
[100%] Linking CXX executable main
make[2]: warning:  Clock skew detected.  Your build may be incomplete.
[100%] Built target main
make[1]: warning:  Clock skew detected.  Your build may be incomplete.
make: warning:  Clock skew detected.  Your build may be incomplete.
[build]$ ./main
Hello world
src:test

第三个工程

源代码分布情况:

./eg_demo/main.cpp
./eg_demo/src/test.cpp 、test.h

程序中的内容和第二个项目一致
注: 多目录工程的CmakeLists.txt编写
主文件(eg_demo)的CMakeLists.txt:

PROJECT(Hello)
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
INCLUDE_DIRECTORIES(./ ./src)
ADD_SUBDIRECTORY(src)
AUX_SOURCE_DIRECTORY(. DIR_SRCS)
ADD_EXECUTABLE(main ${DIR_SRCS})
TARGET_LINK_LIBRARIES(main test)

子文件(src)的CMakeLists.txt:

AUX_SOURCE_DIRECTORY(. DIR_TEST_SRCS)
ADD_LIBRARY(test ${DIR_TEST_SRCS})

camke 的运行结果:

 mkdir build
[eg_demo3]$ cd build
[build]$ 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
-- Detecting C compile features
-- Detecting C compile features - 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
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Warning (dev) at src/CMakeLists.txt:2 (ADD_LIBRARY):Policy CMP0037 is not set: Target names should not be reserved and shouldmatch a validity pattern.  Run "cmake --help-policy CMP0037" for policydetails.  Use the cmake_policy command to set the policy and suppress thiswarning.The target name "test" is reserved or not valid for certain CMake features,such as generator expressions, and may result in undefined behavior.
This warning is for project developers.  Use -Wno-dev to suppress it.-- Configuring done
-- Generating done
-- Build files have been written to: /eg_demo3/build
[build]$ make
make: Warning: File `Makefile' has modification time 937 s in the future
make[1]: Warning: File `CMakeFiles/Makefile2' has modification time 938 s in the future
make[2]: Warning: File `src/CMakeFiles/test.dir/flags.make' has modification time 937 s in the future
Scanning dependencies of target test
make[2]: warning:  Clock skew detected.  Your build may be incomplete.
make[2]: Warning: File `src/CMakeFiles/test.dir/flags.make' has modification time 937 s in the future
[ 25%] Building CXX object src/CMakeFiles/test.dir/test.cpp.o
[ 50%] Linking CXX static library libtest.a
make[2]: warning:  Clock skew detected.  Your build may be incomplete.
[ 50%] Built target test
make[2]: Warning: File `CMakeFiles/main.dir/flags.make' has modification time 934 s in the future
Scanning dependencies of target main
make[2]: warning:  Clock skew detected.  Your build may be incomplete.
make[2]: Warning: File `CMakeFiles/main.dir/flags.make' has modification time 934 s in the future
[ 75%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[100%] Linking CXX executable main
make[2]: warning:  Clock skew detected.  Your build may be incomplete.
[100%] Built target main
make[1]: warning:  Clock skew detected.  Your build may be incomplete.
make: warning:  Clock skew detected.  Your build may be incomplete.
[build]$ ./main
Hello world
src:test

参考:https://www.ibm.com/developerworks/cn/linux/l-cn-cmake/index.html
https://blog.csdn.net/ktigerhero3/article/details/70313350/
http://www.cnblogs.com/cv-pr/p/6206921.html


注:博众家之所长,集群英之荟萃。

CMakeList.txt的简单使用相关推荐

  1. CMake 使用方法 CMakeList.txt编写简单分析

    cmake 简介 CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程).他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性 ...

  2. CMakeList.txt的简介

    声明:摘自MOOC 机器人操作系统入门 ros-tutorial-icourse163 Table of Contents 1.catin的特点 2.catkin编译的工作原理: 3.CMakelis ...

  3. caffe学习之——CMakeList.txt内容详解

    在对Caffe使用cmake方式编译安装时产生了对这个文件的好奇,打算做个注释理解, 参考:https://blog.csdn.net/fuzi2012/article/details/7245453 ...

  4. linux编程 —— vscode 开发编译 CMakeList.txt 学习笔记

    文档声明: 以下资料均属于本人在学习过程中产出的学习笔记,如果错误或者遗漏之处,请多多指正.并且该文档在后期会随着学习的深入不断补充完善.感谢各位的参考查看. 笔记资料仅供学习交流使用,转载请标明出处 ...

  5. 【Android 高性能音频】hello-oboe 示例解析 ( Oboe 源代码依赖 | CMakeList.txt 构建脚本分析 | Oboe 源代码构建脚本分析 )

    文章目录 一.Oboe 源码路径 二.阅读 CMakeList.txt 查看依赖 三.hello-oboe 中 NDK 的 CMakeList.txt 构建脚本 四.Oboe 源码 的 CMakeLi ...

  6. 【Android RTMP】音频数据采集编码 ( FAAC 头文件与静态库拷贝到 AS | CMakeList.txt 配置 FAAC | AudioRecord 音频采样 PCM 格式 )

    文章目录 安卓直播推流专栏博客总结 一. FAAC 头文件与静态库拷贝到 Android Studio 二. CMakeList.txt 构建脚本配置 三. Java 层 AudioRecord 音频 ...

  7. 【Android RTMP】RTMPDumb 源码导入 Android Studio ( 交叉编译 | 配置 CMakeList.txt 构建脚本 )

    文章目录 安卓直播推流专栏博客总结 一. RTMP 协议 二. RTMP 协议使用 三. RTMPDump 源码下载 四. RTMPDump 源码交叉编译 五. RTMPDump 源码导入 Andro ...

  8. Linux的Mysql安装CMakeList.txt找不到

    复制了一个文件CMakeList.txt到了该文件夹  就可以了!

  9. CMakeList.txt中设置一个可变的变量的值(bool)

    在CMakeList.txt中有个bool变量,在debug模式下需要设置为OFF,在其他模式(release.thread.leak)下设置为ON,需要在makefile中将该值设置不同的值,CMa ...

最新文章

  1. 提取Jar2Exe源代码,JavaAgent监控法
  2. 容器删除元素后迭代器失效_使用迭代器遍历容器元素
  3. FPGA配置模式(Altera版)
  4. js,jquery 根据对象某一属性进行排序
  5. 前端学习(526):等分布局
  6. VSFTPD实战02_需求
  7. 访问控制模型ACL和RBAC
  8. 第一个python解释器哪年问世_Python即Python解释器的发展史
  9. mac docker安装linux,Mac上使用docker安装centos
  10. 用lnmp.org中的lnmp下安装ftp(pureftp)
  11. 跨程序提供及获取内容
  12. Webx MVC分析
  13. 2. mysql 基本命令
  14. delphi调用chrome内核进行浏览
  15. 项目文档----项目描述
  16. C语言墓碑上的字符,古代人墓碑上的文字都有什么讲究
  17. 阿里云的这群“疯子”
  18. 【前端】vue阶段案例:组件化-房源展示
  19. Two-Stream Convolutional Networksfor Action Recognition in Videos——学习笔记
  20. 【高项】一次过 信息系统项目管理师 心得

热门文章

  1. 贝叶斯法则求垄断者阻挠概率的动态博弈问题
  2. 如何化解频临离婚边缘的婚姻危机
  3. 去哪儿网数据同步平台技术演进与实践
  4. 计算机基础文化课认识,【计算机基础论文】计算机基础的教学改革解析(共4653字)...
  5. 【备忘】Oracle商业智能BI产品OBIEE11G深入浅出全套视频教程
  6. vassonic PHP,轻量级、高性能的 VasSonic 框架,听说开源了?
  7. 运维GO-2021年书单-产品运营 篇
  8. bde怎么配置oracle数据库,Oracle数据访问组件ODAC教程:如何从BDE和DOA迁移
  9. 计算机设备统计报告,2017年1-12月通信设备、计算机及其他电子设备制造业增加值统计分析...
  10. PV操作与信号灯例子