<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國際加速解決方案。 廣告
                # 7.8 使用target_sources避免全局變量 **NOTE**:*此示例代碼可以在 https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-7/recipe-08 中找到,其中有一個C++示例。該示例在CMake 3.5版(或更高版本)中是有效的,并且已經在GNU/Linux、macOS和Windows上進行過測試。* 本示例中,我們將討論前一個示例的另一種方法,并不使用`add_subdirectory`的情況下,使用`module include`組裝不同的CMakeLists.txt文件。這種方法的靈感來自https://crascit.com/2016/01/31/enhance-sours-file-handling-with-target_sources/ ,其允許我們使用`target_link_libraries`鏈接到當前目錄之外定義的目標。 ## 準備工作 將使用與前一個示例相同的源代碼。惟一的更改將出現在`CMakeLists.txt`文件中,我們將在下面的部分中討論這些更改。 ## 具體實施 1. 主`CMakeLists.txt`包含以下內容: ```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) include(GNUInstallDirs) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}) # defines targets and sources include(src/CMakeLists.txt) include(external/CMakeLists.txt) enable_testing() add_subdirectory(tests) ``` 2. 與前一個示例相比,`external/CMakeLists.txt`文件沒有變化。 3. `src/CMakeLists.txt `文件定義了兩個庫(automaton和evolution): ```cmake add_library(automaton "") add_library(evolution "") include(${CMAKE_CURRENT_LIST_DIR}/evolution/CMakeLists.txt) include(${CMAKE_CURRENT_LIST_DIR}/initial/CMakeLists.txt) include(${CMAKE_CURRENT_LIST_DIR}/io/CMakeLists.txt) include(${CMAKE_CURRENT_LIST_DIR}/parser/CMakeLists.txt) add_executable(automata "") target_sources(automata PRIVATE ${CMAKE_CURRENT_LIST_DIR}/main.cpp ) target_link_libraries(automata PRIVATE automaton conversion ) ``` 4. `src/evolution/CMakeLists.txt`文件包含以下內容: ```cmake target_sources(automaton PRIVATE ${CMAKE_CURRENT_LIST_DIR}/evolution.cpp PUBLIC ${CMAKE_CURRENT_LIST_DIR}/evolution.hpp ) target_include_directories(automaton PUBLIC ${CMAKE_CURRENT_LIST_DIR} ) target_sources(evolution PRIVATE ${CMAKE_CURRENT_LIST_DIR}/evolution.cpp PUBLIC ${CMAKE_CURRENT_LIST_DIR}/evolution.hpp ) target_include_directories(evolution PUBLIC ${CMAKE_CURRENT_LIST_DIR} ) ``` 5. 其余`CMakeLists.txt`文件和`src/initial/CMakeLists.txt`相同: ```cmake target_sources(automaton PRIVATE ${CMAKE_CURRENT_LIST_DIR}/initial.cpp PUBLIC ${CMAKE_CURRENT_LIST_DIR}/initial.hpp ) target_include_directories(automaton PUBLIC ${CMAKE_CURRENT_LIST_DIR} ) ``` 6. 配置、構建和測試的結果與前面的方法相同: ```shell $ mkdir -p build $ cd build $ cmake .. $ cmake --build build $ ctest Running tests... Start 1: test_evolution 1/1 Test #1: test_evolution ................... Passed 0.00 sec 100% tests passed, 0 tests failed out of 1 ``` ## 工作原理 與之前的示例不同,我們定義了三個庫: * conversion(在external定義) * automaton(包含除轉換之外的所有源) * evolution(在`src/evolution`中定義,并通過`cpp_test`鏈接) 本例中,通過使用`include()`引用`CMakeLists.txt`文件,我們在父范圍內,仍然能保持所有目標可用: ```cmake include(src/CMakeLists.txt) include(external/CMakeLists.txt) ``` 我們可以構建一個包含樹,記住當進入子目錄(`src/CMakeLists.txt`)時,我們需要使用相對于父范圍的路徑: ```cmake include(${CMAKE_CURRENT_LIST_DIR}/evolution/CMakeLists.txt) include(${CMAKE_CURRENT_LIST_DIR}/initial/CMakeLists.txt) include(${CMAKE_CURRENT_LIST_DIR}/io/CMakeLists.txt) include(${CMAKE_CURRENT_LIST_DIR}/parser/CMakeLists.txt) ``` 這樣,我們就可以定義并鏈接到通過`include()`語句訪問文件樹中任何位置的目標。但是,我們應該選擇在對維護人員和代碼貢獻者容易看到的地方,去定義它們。 ## 更多信息 我們可以再次使用CMake和Graphviz (http://www.graphviz.org/)生成這個項目的依賴關系圖: ```shell $ cd build $ cmake --graphviz=example.dot .. $ dot -T png example.dot -o example.png ``` 對于當前設置,我們得到如下依賴關系圖: ![](https://img.kancloud.cn/60/a9/60a9f8000785e5d2a1a4edf9fdeebe68_1651x411.png)
                  <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>

                              哎呀哎呀视频在线观看