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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 1.10 使用控制流 **NOTE**:*此示例代碼可以在 https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-01/recipe-10 中找到,有一個C++示例。該示例在CMake 3.5版(或更高版本)中是有效的,并且已經在GNU/Linux、macOS和Windows上進行過測試。* 本章前面的示例中,已經使用過`if-else-endif`。CMake還提供了創建循環的語言工具:`foreach endforeach`和`while-endwhile`。兩者都可以與`break`結合使用,以便盡早從循環中跳出。本示例將展示如何使用`foreach`,來循環源文件列表。我們將應用這樣的循環,在引入新目標的前提下,來為一組源文件進行優化降級。 ## 準備工作 將重用第8節中的幾何示例,目標是通過將一些源代碼匯集到一個列表中,從而微調編譯器的優化。 ## 具體實施 下面是`CMakeLists.txt`中要的詳細步驟: 1. 與示例8中一樣,指定了CMake的最低版本、項目名稱和語言,并聲明了幾何庫目標: ```cmake cmake_minimum_required(VERSION 3.5 FATAL_ERROR) project(recipe-10 LANGUAGES CXX) add_library(geometry STATIC geometry_circle.cpp geometry_circle.hpp geometry_polygon.cpp geometry_polygon.hpp geometry_rhombus.cpp geometry_rhombus.hpp geometry_square.cpp geometry_square.hpp ) ``` 2. 使用`-O3`編譯器優化級別編譯庫,對目標設置一個私有編譯器選項: ```cmake target_compile_options(geometry PRIVATE -O3 ) ``` 3. 然后,生成一個源文件列表,以較低的優化選項進行編譯: ```cmake list( APPEND sources_with_lower_optimization geometry_circle.cpp geometry_rhombus.cpp ) ``` 4. 循環這些源文件,將它們的優化級別調到`-O2`。使用它們的源文件屬性完成: ```cmake message(STATUS "Setting source properties using IN LISTS syntax:") foreach(_source IN LISTS sources_with_lower_optimization) set_source_files_properties(${_source} PROPERTIES COMPILE_FLAGS -O2) message(STATUS "Appending -O2 flag for ${_source}") endforeach() ``` 5. 為了確保設置屬性,再次循環并在打印每個源文件的`COMPILE_FLAGS`屬性: ```cmake message(STATUS "Querying sources properties using plain syntax:") foreach(_source ${sources_with_lower_optimization}) get_source_file_property(_flags ${_source} COMPILE_FLAGS) message(STATUS "Source ${_source} has the following extra COMPILE_FLAGS: ${_flags}") endforeach() ``` 6. 最后,添加`compute-areas`可執行目標,并將`geometry`庫連接上去: ```cmake add_executable(compute-areas compute-areas.cpp) target_link_libraries(compute-areas geometry) ``` 7. 驗證在配置步驟中正確設置了標志: ```shell $ mkdir -p build $ cd build $ cmake .. ... -- Setting source properties using IN LISTS syntax: -- Appending -O2 flag for geometry_circle.cpp -- Appending -O2 flag for geometry_rhombus.cpp -- Querying sources properties using plain syntax: -- Source geometry_circle.cpp has the following extra COMPILE_FLAGS: -O2 -- Source geometry_rhombus.cpp has the following extra COMPILE_FLAGS: -O2 ``` 8. 最后,還使用`VERBOSE=1`檢查構建步驟。將看到`-O2`標志添加在`-O3`標志之后,但是最后一個優化級別標志(在本例中是`-O2`)不同: ```shell $ cmake --build . -- VERBOSE=1 ``` ## 工作原理 `foreach-endforeach`語法可用于在變量列表上,表示重復特定任務。本示例中,使用它來操作、設置和獲取項目中特定文件的編譯器標志。CMake代碼片段中引入了另外兩個新命令: * `set_source_files_properties(file PROPERTIES property value) `,它將屬性設置為給定文件的傳遞值。與目標非常相似,文件在CMake中也有屬性,允許對構建系統進行非常細粒度的控制。源文件的可用屬性列表可以在這里找到: https://cmake.org/cmake/help/v3.5/manual/cmake-properties.7.html#source-file-properties 。 * `get_source_file_property(VAR file property)`,檢索給定文件所需屬性的值,并將其存儲在CMake`VAR`變量中。 **NOTE**:*CMake中,列表是用分號分隔的字符串組。列表可以由`list`或`set`命令創建。例如,`set(var a b c d e)`和`list(APPEND a b c d e)`都創建了列表`a;b;c;d;e`。* **TIPS**:*為了對一組文件降低優化,將它們收集到一個單獨的目標(庫)中,并為這個目標顯式地設置優化級別,而不是附加一個標志,這樣可能會更簡潔,不過在本示例中,我們的重點是`foreach-endforeach`。* ## 更多信息 `foreach()`的四種使用方式: * `foreach(loop_var arg1 arg2 ...) `: 其中提供循環變量和顯式項列表。當為`sources_with_lower_optimization`中的項打印編譯器標志集時,使用此表單。注意,如果項目列表位于變量中,則必須顯式展開它;也就是說,`${sources_with_lower_optimization}`必須作為參數傳遞。 * 通過指定一個范圍,可以對整數進行循環,例如:`foreach(loop_var range total)`或`foreach(loop_var range start stop [step])`。 * 對列表值變量的循環,例如:`foreach(loop_var IN LISTS [list1[...]])` 。參數解釋為列表,其內容就會自動展開。 * 對變量的循環,例如:` foreach(loop_var IN ITEMS [item1 [...]])`。參數的內容沒有展開。
                  <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>

                              哎呀哎呀视频在线观看