[20210428更新]:补充了 CMakeList.txt 文件中遗漏的 include 指令相关内容。


本文主要介绍 CMake 中 include 指令的相关知识。

1 概述

引用 CMake 官网对于 include 指令的介绍,如下:

Load and run CMake code from a file or module.

include 指令的用法如下:

include(<file|module> [OPTIONAL] [RESULT_VARIABLE <VAR>] [NO_POLICY_SCOPE])

Load and run CMake code from the file given. Variable reads and writes access the scope of the caller (dynamic scoping). If OPTIONAL is present, then no error is raised if the file does not exist. If RESULT_VARIABLE is given, the variable will be set to the full filename which has been included or NOTFOUND if it failed.

If a module is specified instead of a file, the file with name <modulename>.cmake is searched first in CMAKE_MODULE_PATH, then in the CMake module directory. There is one exception to this: if the file which calls include() is located itself in the CMake builtin module directory, then first the CMake builtin module directory is searched and CMAKE_MODULE_PATH afterwards. See also policy CMP0017.

See the cmake_policy() command documentation for discussion of the NO_POLICY_SCOPE option.

2 作用

从前面所述,可知 include 指令用来载入并运行来自于文件或模块的 CMake 代码。

在这里针对一些具体的问题场景,介绍 include 指令的具体用法。

2.1 多C++标准版本指定

有时遇到这样一种需求,在使用同一个外层 CMakeLists.txt 的前提下,每个源码子目录中要求使用的 C++ 标准版本不同,有的源码要求使用 C++98 标准编译、有的源码要求使用 C++11 标准编译,这时就可以使用 include 指令来满足该需求。

2.1.1 项目代码结构及内容

此处使用《CMake介绍及其用法示例》中的项目代码结构,并在其基础上做一些改动,改动后的项目代码结构如下:

相比于之前的项目代码结构,这里新增了“cmake_dir3”这个源码目录,同时,修改了最外层的  CMakeLists.txt。

cmake_dir3 目录中包含的文件列表如下:

[root@node1 /opt/liitdar/mydemos/simples/cmake_test]# l cmake_dir3
total 8
-rw-r--r--. 1 root root 257 Jul 21 14:19 CMakeLists.txt
-rw-r--r--. 1 root root 258 Jul 21 14:19 main.cpp
[root@node1 /opt/liitdar/mydemos/simples/cmake_test]#

其中,CMakeLists.txt 内容如下:

# 遍历当前路径下的所有源文件,并将其添加到变量DIR_SRCS中
aux_source_directory(. DIR_SRCS)# 添加名为cmake_test3的可执行文件,该文件会由变量DIR_SRCS中的源文件构建生成
add_executable(cmake_test3 ${DIR_SRCS})

源码文件 main.cpp 内容如下:

#include <iostream>
#include <string>using namespace std;int main()
{int a = 100;string strTest;strTest = to_string(a) + " is a string.";cout << "a is: " << a << endl;cout << "pszTest is: " << strTest << endl;return 0;
}

最外层的 CMakeLists.txt 改动部分(新增了 cmake_dir3 源码目录)如下:

2.1.2 项目构建

对上述项目使用 CMake 进行构建,过程信息如下:

通过上图可以看到,项目构建失败了,因为在 cmake_dir3 中存在“to_string”函数,该函数需要在 C++11 标准下进行编译,而项目默认使用的是 C++98 标准。

2.1.3 解决方案

此时,就需要为 cmake_dir3 设置不同的 C++ 标准进行编译了。具体步骤如下:

1. 在最外层的 CMakeList.txt 的同级目录下,增加一个文件 set_cxx_norm.cmake,如下:

文件 set_cxx_norm.cmake 的内容如下:

# set c++ norm value, these values will be used for comparision later
set(CXX_NORM_CXX98 1)   # C++98
set(CXX_NORM_CXX03 2)   # C++03
set(CXX_NORM_CXX11 3)   # C++11# Set the wanted C++ norm
# Adds the good argument to the command line in function of the compiler
macro(set_cxx_norm NORM)# Extract c++ compiler --version outputexec_program(${CMAKE_CXX_COMPILER}ARGS --versionOUTPUT_VARIABLE _compiler_output)# Keep only the first linestring(REGEX REPLACE"(\n.*$)"""cxx_compiler_version "${_compiler_output}")# Extract the version numberstring(REGEX REPLACE"([^0-9.])|([0-9.][^0-9.])"""cxx_compiler_version "${cxx_compiler_version}")# Set the specific C++ norm According 'NORM'if(${NORM} EQUAL ${CXX_NORM_CXX98})add_definitions("-std=c++98")elseif(${NORM} EQUAL ${CXX_NORM_CXX03})add_definitions("-std=c++03")elseif(${NORM} EQUAL ${CXX_NORM_CXX11})if(${cxx_compiler_version} VERSION_LESS "4.7.0")add_definitions("-std=c++0x")else()add_definitions("-std=c++11")endif()endif()endmacro()

2. 然后,通过修改最外层的 CMakeLists.txt,使用include指令引入 set_cxx_norm.cmake 文件,这样就可以在源码目录中设置想要使用的 C++ 标准了。CMakeList.txt 中新增的 include 指令如下:

3. 最后,修改 cmake_dir3 的 CMakeLists.txt 文件,新增“要使用C++11标准”的语句,如下:

# 使用C++11标准
set_cxx_norm(${CXX_NORM_CXX11})

完成上述修改后,再次进行项目构建,结果如下:

通过上图能够知道,项目构建成功了。此时,cmake_test1 和 cmake_test2 使用的是 C++98(默认)标准;而 cmake_test3 使用的是 C++11 标准。

运行 cmake_test3 程序,运行结果如下:

上面的运行结果表明,cmake_test3 成功调用了 C++11 标准的“to_string”函数,将整型转换为字符串类型了。

CMake中include指令介绍相关推荐

  1. CMake中include指令用法介绍

    转载于:  https://blog.csdn.net/liitdar/article/details/81144461 本文主要介绍CMake中include指令的用法. 1 概述 引用CMake官 ...

  2. Nginx中server_name指令介绍

    Nginx中server_name指令介绍 用途 根据官方文档说明,用来设置虚拟服务器,对于用IP还是请求头部中的Host字段内容设置这个指令的值,没有明确的分别. 用法 指令后跟特定域名,此时第一个 ...

  3. JSP中include指令的功能简介说明

    转自: JSP中include指令起什么用呢? 下文讲述JSP中include指令的功能简介说明,如下所示: JSP中include指令的功能说明:用于将不同的文件插入到当前JSP页面的指定位置 JS ...

  4. 浅谈JSP中include指令与include动作标识的区别

    JSP中主要包含三大指令,分别是page,include,taglib.本篇主要提及include指令. include指令使用格式:<%@ include file="文件的绝对路径 ...

  5. jsp中include指令和动作的区别

    1.include指令是编译阶段的指令,又称为文件加载指令被插入的文件必须保证插入后形成的新文件符合JSP页面的语法规则. nclude可以在JSP页面转换成Servlet之前,将JSP代码插入其中. ...

  6. 20080823-jsp中include指令与动作的不同

    一.include 指令 格式: <%@include file="文件的绝对路径或者相对路径"%> file属性:指定被包含的文件,是必添属性.该属性不支持任何表达式 ...

  7. JSP中include指令和include动作的区别

    include指令是编译阶段的指令,即include所包含的文件的内容是编译的时候插入到JSP文件中,JSP引擎在判断JSP页面未被修改,否则视为已被修改.由于被包含的文件是在编译时才插入的,因此如果 ...

  8. CMake中include的使用

    CMake中的include命令用于从文件或模块(file or module)加载并运行CMake code.其格式如下: include(<file|module> [OPTIONAL ...

  9. angular1.x 中重要指令介绍($eval,$parse和$compile)

    这篇文章是我两年前在博客园写的,现在移植过来,不过Angular 1.x 在国内用的人已经不多了,希望能帮助到有需要的人 在 angular 的服务中,有一些服务你不得不去了解,因为他可以说是 ng ...

  10. JSP中include指令的乱码问题

    1.被包含的JSP需要添加page指令 2.<jsp:include page=""> </jsp:include> 被包含的JSP书写格式 <%@p ...

最新文章

  1. 周跃教授:脊柱微创手术机器人在脊柱手术中的应用前景 (转载)
  2. java io操作_Java IO 操作
  3. open_basedir restriction in effect,解决php引入文件权限问题
  4. ckeditor:基本使用方法
  5. Effective C# 原则12:选择变量初始化而不是赋值语句
  6. 基于PHP的新闻管理系统(基础版)
  7. 算法-贪心算法知识总结
  8. 最早的支付网关(滴滴支付)和最新的聚合支付设计架构
  9. 计算机处理汉字信息时所使用的代码是,计算机处理汉字信息时所用的代码是什么...
  10. Java字符串常见面试题
  11. LiveNVR安防监控直播中无插件web直播方案中实现快照抓取的功能
  12. flutter中App签名
  13. 在word文档中从第3页开始编页码的方法
  14. 增益 Gain 分贝 dB
  15. 计算机管理无法打开权限不足,win10管理员权限不足怎么设置_win10管理员权限不足如何解决...
  16. Mybatis—— 使用注解实现一对一复杂关系映射及延迟加载
  17. iphone两个备份合并_看完这篇干货,再备份iPhone数据
  18. JAVA综合性实验——猜姓氏游戏
  19. 刚子扯扯蛋:说下百度对网站原创文章的个人感受
  20. 使用Python Link SDK接入阿里云物联网平台

热门文章

  1. 使用pip安装tensorflow很慢的问题
  2. 3. VPP源码分析(graph node(1))
  3. 自动化测试基础篇--Selenium发送测试报告邮件
  4. 第一天,搬家到博客园
  5. Windows移动开发(三)——闭关修炼
  6. sourceinsight4.0安装破解( 内含sublime text配色方案)
  7. 20号:JAVA的值传递与引用传递的正确理解
  8. java运行时内存模型
  9. [SHOI2008]堵塞的交通traffic
  10. SpriteBuilderamp;Cocos2D使用CCEffect特效实现天黑天亮过度效果