<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 7.5 重新定義函數和宏 **NOTE**:*此示例代碼可以在 https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-7/recipe-05 中找到。該示例在CMake 3.5版(或更高版本)中是有效的,并且已經在GNU/Linux、macOS和Windows上進行過測試。* 我們已經提到模塊包含不應該用作函數調用,因為模塊可能被包含多次。本示例中,我們將編寫我們自己的“包含保護”機制,如果多次包含一個模塊,將觸發警告。內置的`include_guard`命令從3.10版開始可以使用,對于C/C++頭文件,它的行為就像`#pragma`一樣。對于當前版本的CMake,我們將演示如何重新定義函數和宏,并且展示如何檢查CMake版本,對于低于3.10的版本,我們將使用定制的“包含保護”機制。 ## 準備工作 這個例子中,我們將使用三個文件: ```shell . ├── cmake │ ├── custom.cmake │ └── include_guard.cmake └── CMakeLists.txt ``` `custom.cmake `模塊包含以下代碼: ```cmake include_guard(GLOBAL) message(STATUS "custom.cmake is included and processed") ``` 我們稍后會對` cmake/include_guard.cmake`進行討論。 ## 具體實施 我們對三個CMake文件的逐步分解: 1. 示例中,我們不會編譯任何代碼,因此我們的語言要求是`NONE`: ```cmake cmake_minimum_required(VERSION 3.5 FATAL_ERROR) project(recipe-05 LANGUAGES NONE) ``` 2. 定義一個`include_guard`宏,將其放在一個單獨的模塊中: ```cmake # (re)defines include_guard include(cmake/include_guard.cmake) ``` 3. ` cmake/include_guard.cmake`文件包含以下內容(稍后將詳細討論): ```cmake macro(include_guard) if (CMAKE_VERSION VERSION_LESS "3.10") # for CMake below 3.10 we define our # own include_guard(GLOBAL) message(STATUS "calling our custom include_guard") # if this macro is called the first time # we start with an empty list if(NOT DEFINED included_modules) set(included_modules) endif() if ("${CMAKE_CURRENT_LIST_FILE}" IN_LIST included_modules) message(WARNING "module ${CMAKE_CURRENT_LIST_FILE} processed more than once") endif() list(APPEND included_modules ${CMAKE_CURRENT_LIST_FILE}) else() # for CMake 3.10 or higher we augment # the built-in include_guard message(STATUS "calling the built-in include_guard") _include_guard(${ARGV}) endif() endmacro() ``` 4. 主CMakeLists.txt中,我們模擬了兩次包含自定義模塊的情況: ```cmake include(cmake/custom.cmake) include(cmake/custom.cmake) ``` 5. 最后,使用以下命令進行配置: ```shell $ mkdir -p build $ cd build $ cmake .. ``` 6. 使用CMake 3.10及更高版本的結果如下: ```shell -- calling the built-in include_guard -- custom.cmake is included and processed -- calling the built-in include_guard ``` 7. 使用CMake得到3.10以下的結果如下: ```shell - calling our custom include_guard -- custom.cmake is included and processed -- calling our custom include_guard CMake Warning at cmake/include_guard.cmake:7 (message): module /home/user/example/cmake/custom.cmake processed more than once Call Stack (most recent call first): cmake/custom.cmake:1 (include_guard) CMakeLists.txt:12 (include) ``` ## 工作原理 `include_guard`宏包含兩個分支,一個用于CMake低于3.10,另一個用于CMake高于3.10: ```cmake macro(include_guard) if (CMAKE_VERSION VERSION_LESS "3.10") # ... else() # ... endif() endmacro() ``` 如果CMake版本低于3.10,進入第一個分支,并且內置的`include_guard`不可用,所以我們自定義了一個: ```cmake message(STATUS "calling our custom include_guard") # if this macro is called the first time # we start with an empty list if(NOT DEFINED included_modules) set(included_modules) endif() if ("${CMAKE_CURRENT_LIST_FILE}" IN_LIST included_modules) message(WARNING "module ${CMAKE_CURRENT_LIST_FILE} processed more than once") endif() list(APPEND included_modules ${CMAKE_CURRENT_LIST_FILE}) ``` 如果第一次調用宏,則`included_modules`變量沒有定義,因此我們將其設置為空列表。然后檢查`${CMAKE_CURRENT_LIST_FILE}`是否是`included_modules`列表中的元素。如果是,則會發出警告;如果沒有,我們將`${CMAKE_CURRENT_LIST_FILE}`追加到這個列表。CMake輸出中,我們可以驗證自定義模塊的第二個包含確實會導致警告。 CMake 3.10及更高版本的情況有所不同;在這種情況下,存在一個內置的`include_guard`,我們用自己的宏接收到參數并調用它: ```cmake macro(include_guard) if (CMAKE_VERSION VERSION_LESS "3.10") # ... else() message(STATUS "calling the built-in include_guard") _include_guard(${ARGV}) endif() endmacro() ``` 這里,`_include_guard(${ARGV})`指向內置的`include_guard`。本例中,使用自定義消息(“調用內置的`include_guard`”)進行了擴展。這種模式為我們提供了一種機制,來重新定義自己的或內置的函數和宏,這對于調試或記錄日志來說非常有用。 **NOTE**:*這種模式可能很有用,但是應該謹慎使用,因為CMake不會對重新定義的宏或函數進行警告。*
                  <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>

                              哎呀哎呀视频在线观看