日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

多C++标准版本指定

發(fā)布時間:2023/12/10 c/c++ 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 多C++标准版本指定 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本文主要介紹 CMake 中 include 指令的相關(guān)知識。

1 概述
引用 CMake 官網(wǎng)對于 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++標(biāo)準(zhǔn)版本指定
有時遇到這樣一種需求,在使用同一個外層 CMakeLists.txt 的前提下,每個源碼子目錄中要求使用的 C++ 標(biāo)準(zhǔn)版本不同,有的源碼要求使用 C++98 標(biāo)準(zhǔn)編譯、有的源碼要求使用 C++11 標(biāo)準(zhǔn)編譯,這時就可以使用 include 指令來滿足該需求。

2.1.1 項目代碼結(jié)構(gòu)及內(nèi)容
此處使用《CMake介紹及其用法示例》中的項目代碼結(jié)構(gòu),并在其基礎(chǔ)上做一些改動,改動后的項目代碼結(jié)構(gòu)如下:

相比于之前的項目代碼結(jié)構(gòu),這里新增了“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 內(nèi)容如下:

# 遍歷當(dāng)前路徑下的所有源文件,并將其添加到變量DIR_SRCS中
aux_source_directory(. DIR_SRCS)
?
# 添加名為cmake_test3的可執(zhí)行文件,該文件會由變量DIR_SRCS中的源文件構(gòu)建生成
add_executable(cmake_test3 ${DIR_SRCS})
源碼文件 main.cpp 內(nèi)容如下:

#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 項目構(gòu)建
對上述項目使用 CMake 進行構(gòu)建,過程信息如下:

通過上圖可以看到,項目構(gòu)建失敗了,因為在 cmake_dir3 中存在“to_string”函數(shù),該函數(shù)需要在 C++11 標(biāo)準(zhǔn)下進行編譯,而項目默認使用的是 C++98 標(biāo)準(zhǔn)。

2.1.3 解決方案
此時,就需要為 cmake_dir3 設(shè)置不同的 C++ 標(biāo)準(zhǔn)進行編譯了。具體步驟如下:

1. 在最外層的 CMakeList.txt 的同級目錄下,增加一個文件 set_cxx_norm.cmake,如下:

文件 set_cxx_norm.cmake 的內(nèi)容如下:

# 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. 然后,通過修改最外層的 CMakeLists.txt,使用include指令引入 set_cxx_norm.cmake 文件,這樣就可以在源碼目錄中設(shè)置想要使用的 C++ 標(biāo)準(zhǔn)了。CMakeList.txt 中新增的 include 指令如下:

3. 最后,修改 cmake_dir3 的 CMakeLists.txt 文件,新增“要使用C++11標(biāo)準(zhǔn)”的語句,如下:

# 使用C++11標(biāo)準(zhǔn)
set_cxx_norm(${CXX_NORM_CXX11})
完成上述修改后,再次進行項目構(gòu)建,結(jié)果如下:

通過上圖能夠知道,項目構(gòu)建成功了。此時,cmake_test1 和 cmake_test2 使用的是 C++98(默認)標(biāo)準(zhǔn);而 cmake_test3 使用的是 C++11 標(biāo)準(zhǔn)。

運行 cmake_test3 程序,運行結(jié)果如下:

上面的運行結(jié)果表明,cmake_test3 成功調(diào)用了 C++11 標(biāo)準(zhǔn)的“to_string”函數(shù),將整型轉(zhuǎn)換為字符串類型了。

總結(jié)

以上是生活随笔為你收集整理的多C++标准版本指定的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。