1. 程式人生 > >CMake中include指令用法介紹

CMake中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中包含的檔案列表如下:

[[email protected] /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
[[email protected] /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,如下:

通過在最外層的CMakeLists.txt中使用include指令引入該檔案,我們就可以在原始碼目錄中設定想要使用的c++標準了。

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 output
    exec_program(
        ${CMAKE_CXX_COMPILER}
        ARGS --version
        OUTPUT_VARIABLE _compiler_output
    )
    # Keep only the first line
    string(REGEX REPLACE
        "(\n.*$)"
        ""
        cxx_compiler_version "${_compiler_output}"
    )
    # Extract the version number
    string(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. 然後,我們需要修改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”函式,將整型轉換為字串型別了。

相關推薦

CMakeinclude指令用法介紹

本文主要介紹CMake中include指令的用法。 1 概述 引用CMake官網對於include指令的介紹,如下: Load and run CMake code from a file or module. include指令的用法如下: include(<

JSPinclude指令include動作的區別

1.include指令 語法格式:<%@include file="檔案的URL" %> 2.include動作 語法格式: 1)<jsp:include page="檔案的URL"/> 2)<jsp:include page="檔案的URL

shellIF的用法介紹

一、語法結構 if [ condition ] then      statements  [elif condition      then statements. 

ArcGIS Spatial Adjustment 用法介紹

一、背景   在mapGIS 中向量化的圖無標準座標,只有相對座標,向量圖中心為原點。現要將向量圖與遙感影像進行疊加。所以需將向量圖轉到遙感影像座標下。開啟遙感影像與向量圖如下(上方為遙感影像,下方為向量圖),兩者不僅位置對應不上,比例也不相對應。 二、解決方法  

oracleConnect By用法介紹

為解決oracle中自連線查詢不適合操作大表的情況,採用connect by 方式實現。oracle中可以用START WITH...CONNECT BY PRIOR子句實現遞迴查詢,connect by 在結構化查詢中應用。 基本語法:             selec

關於react-redux的connect用法介紹及原理解析

關於react-redux的一個流程圖 流程圖 connect用法介紹 connect方法宣告: connect([mapStateToProps], [mapDispatchToProps], [mergeProps],[options]) 作用:連線Reac

JSPinclude指令include動作區別詳解

我們都知道在JSP中include有兩種形式,分別是 <%@ include file=” ”%> <jsp:include page=” ” flush=”true”/> 前者是指令元素,後者是動作元素。具體它們將在何處用?如何用及它們有什麼區別?這應該是很多人看

.netusing指令用法

using指令的多種用法   • using語句在Dispose模式中的應用   1. 引言   在.NET大家庭中,有不少的關鍵字承擔了多種角色,例如new關鍵字就身兼數職,除了能夠建立物件,在繼承體系中隱藏基類成員,還在泛型宣告中約束可能用作型別引數的引數,在[第五回:

方括號及其在命令列的不同用法介紹

通配 方括號最簡單的用法就是通配。你可能在知道“ Globbing”這個概念之前就已經通過通配來匹配內容了,列出具有相同特徵的

mysql模糊查詢的四種用法介紹

包含 如果 正則 搜索 name 模糊查詢 長度 use mysql 下面介紹mysql中模糊查詢的四種用法: 1,%:表示任意0個或多個字符。可匹配任意類型和長度的字符,有些情況下若是中文,請使用兩個百分號(%%)表示。 比如 SELECT * FROM [user] W

jspinclude 的兩種用法

1.兩種用法 靜態include:  <%@ inlcude file =”header.jsp” %> 此時引入的是靜態的jsp檔案,它將引入的jsp中的原始碼原封不動地附加到當前檔案中,所以在jsp程式中使用這個指令的時候file裡面的值(即要匯入的檔案)不能帶多餘

SqlCAST用法介紹

1、cast用法簡介: CAST (expression AS data_type)引數說明:expression:任何有效的SQServer表示式。AS:用於分隔兩個引數,在AS之前的是要處理的資料,在AS之後是要轉換的資料型別。data_type:目標系統所提供的資料型別,包括bigint和

Mabitis的#與$符號區別及用法介紹

一、介紹  mybatis 中使用 Mapper.xml裡面的配置進行 sql 查詢,經常需要動態傳遞引數,例如我們需要根據使用者的姓名來篩選使用者時,sql 如下: ?

ARM彙編:彙編proc、endp、ret、near、far指令用法

   ARM彙編:彙編中proc、endp、ret、near、far指令用法 子程式名 PROC NEAR ( 或 FAR ) …… ret 子程式名 ENDP (1)NEAR屬性(段內近呼叫): 呼叫程式和子程式在同一程式碼段中,只能被相同程式碼段的其他程式呼叫;    FAR屬性(段間遠

shell set 指令用法

語法 set [-可選引數] [-o 選項] 功能說明 set 指令可根據不同的需求來設定當前所使用 shell 的執行方式,同時也可以用來設定或顯示 shell 變數的值。當指定

C++STLsort用法介紹

自定義STL中sort的排序規則 前情提要: 0、要使用sort,首先需要包含標頭檔案< algorithm> 1、sort函式可以指定兩個引數,也可以指定三個引數。 (1)第一個是要排序

jspinclude的兩種用法

<%@ include file=” ”%> include指令 <jsp:include page=” ” flush=”true”/> include動作 主要有兩方面的不同: 1.執行時間上 <%@ include file=” ”

python的map()函式和reduce()函式的區別和用法介紹

咱們先從定義上來解釋一下這兩個函式的區別: ①從引數方面來講: map(func, *iterables)包含兩個引數,第一個是引數是一個函式,第二個是序列(列表或元組)。其中,函式(即map的第一個引

Spring和SpringBoot的@Component 和@ComponentScan註解用法介紹和注意事項

通過本文你將學到: Component Scan是什麼? 為什麼ComponentScan很重要? 專案中Spring Boot會對哪些包自動執行掃描(Component Scan)? 如何利用Spring Boot定義掃描範圍? 專案啟動時關於Compone

pythonFraction()方法的用法介紹

小編是想將字串的分數,轉換為浮點型的小數才接觸到這個方法的。 原始碼如下: class Fraction(numbers.Rational): """This class implements rational numbers. In the two-argument fo