<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 3.8 檢測Boost庫 **NOTE**:*此示例代碼可以在 https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-03/recipe-08 中找到,包含一個C++的示例。該示例在CMake 3.5版(或更高版本)中是有效的,并且已經在GNU/Linux、macOS和Windows上進行過測試。* Boost是一組C++通用庫。這些庫提供了許多功能,這些功能在現代C++項目中不可或缺,但是還不能通過C++標準使用這些功能。例如,Boost為元編程、處理可選參數和文件系統操作等提供了相應的組件。這些庫中有許多特性后來被C++11、C++14和C++17標準所采用,但是對于保持與舊編譯器兼容性的代碼庫來說,許多Boost組件仍然是首選。 本示例將向您展示如何檢測和鏈接Boost庫的一些組件。 ## 準備工作 我們將編譯的源碼是Boost提供的文件系統庫與文件系統交互的示例。這個庫可以跨平臺使用,并將操作系統和文件系統之間的差異抽象為一致的API。下面的代碼(`path-info.cpp`)將接受一個路徑作為參數,并將其組件的報告打印到屏幕上: ```c++ #include <iostream> #include <boost/filesystem.hpp> using namespace std; using namespace boost::filesystem; const char *say_what(bool b) { return b ? "true" : "false"; } int main(int argc, char *argv[]) { if (argc < 2) { cout << "Usage: path_info path-element [path-element...]\n" "Composes a path via operator/= from one or more path-element arguments\n" "Example: path_info foo/bar baz\n" #ifdef BOOST_POSIX_API " would report info about the composed path foo/bar/baz\n"; #else // BOOST_WINDOWS_API " would report info about the composed path foo/bar\\baz\n"; #endif return 1; } path p; for (; argc > 1; --argc, ++argv) p /= argv[1]; // compose path p from the command line arguments cout << "\ncomposed path:\n"; cout << " operator<<()---------: " << p << "\n"; cout << " make_preferred()-----: " << p.make_preferred() << "\n"; cout << "\nelements:\n"; for (auto element : p) cout << " " << element << '\n'; cout << "\nobservers, native format:" << endl; #ifdef BOOST_POSIX_API cout << " native()-------------: " << p.native() << endl; cout << " c_str()--------------: " << p.c_str() << endl; #else // BOOST_WINDOWS_API wcout << L" native()-------------: " << p.native() << endl; wcout << L" c_str()--------------: " << p.c_str() << endl; #endif cout << " string()-------------: " << p.string() << endl; wcout << L" wstring()------------: " << p.wstring() << endl; cout << "\nobservers, generic format:\n"; cout << " generic_string()-----: " << p.generic_string() << endl; wcout << L" generic_wstring()----: " << p.generic_wstring() << endl; cout << "\ndecomposition:\n"; cout << " root_name()----------: " << p.root_name() << '\n'; cout << " root_directory()-----: " << p.root_directory() << '\n'; cout << " root_path()----------: " << p.root_path() << '\n'; cout << " relative_path()------: " << p.relative_path() << '\n'; cout << " parent_path()--------: " << p.parent_path() << '\n'; cout << " filename()-----------: " << p.filename() << '\n'; cout << " stem()---------------: " << p.stem() << '\n'; cout << " extension()----------: " << p.extension() << '\n'; cout << "\nquery:\n"; cout << " empty()--------------: " << say_what(p.empty()) << '\n'; cout << " is_absolute()--------: " << say_what(p.is_absolute()) << '\n'; cout << " has_root_name()------: " << say_what(p.has_root_name()) << '\n'; cout << " has_root_directory()-: " << say_what(p.has_root_directory()) << '\n'; cout << " has_root_path()------: " << say_what(p.has_root_path()) << '\n'; cout << " has_relative_path()--: " << say_what(p.has_relative_path()) << '\n'; cout << " has_parent_path()----: " << say_what(p.has_parent_path()) << '\n'; cout << " has_filename()-------: " << say_what(p.has_filename()) << '\n'; cout << " has_stem()-----------: " << say_what(p.has_stem()) << '\n'; cout << " has_extension()------: " << say_what(p.has_extension()) << '\n'; return 0; } ``` ## 具體實施 Boost由許多不同的庫組成,這些庫可以獨立使用。CMake可將這個庫集合,表示為組件的集合。`FindBoost.cmake`模塊不僅可以搜索庫集合的完整安裝,還可以搜索集合中的特定組件及其依賴項(如果有的話)。我們將逐步建立相應的`CMakeLists.txt`: 1. 首先,聲明CMake最低版本、項目名稱、語言,并使用C++11標準: ```cmake cmake_minimum_required(VERSION 3.5 FATAL_ERROR) project(recipe-08 LANGUAGES CXX) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_STANDARD_REQUIRED ON) ``` 2. 然后,使用`find_package`搜索Boost。若需要對Boost強制性依賴,需要一個參數。這個例子中,只需要文件系統組件,所以將它作為參數傳遞給`find_package`: ```cmake find_package(Boost 1.54 REQUIRED COMPONENTS filesystem) ``` 3. 添加可執行目標,編譯源文件: ```cmake add_executable(path-info path-info.cpp) ``` 4. 最后,將目標鏈接到Boost庫組件。由于依賴項聲明為`PUBLIC`,依賴于Boost的目標將自動獲取依賴項: ```cmake target_link_libraries(path-info PUBLIC Boost::filesystem ) ``` ## 工作原理 `FindBoost.cmake `是本示例中所使用的CMake模塊,其會在標準系統安裝目錄中找到Boost庫。由于我們鏈接的是`Boost::filesystem`,CMake將自動設置包含目錄并調整編譯和鏈接標志。如果Boost庫安裝在非標準位置,可以在配置時使用`BOOST_ROOT`變量傳遞Boost安裝的根目錄,以便讓CMake搜索非標準路徑: ```shell $ cmake -D BOOST_ROOT=/custom/boost ``` 或者,可以同時傳遞包含頭文件的`BOOST_INCLUDEDIR`變量和庫目錄的`BOOST_LIBRARYDIR`變量: ```shell $ cmake -D BOOST_INCLUDEDIR=/custom/boost/include -DBOOST_LIBRARYDIR=/custom/boost/lib ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看