<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 6.1 配置時生成源碼 **NOTE**:*此示例代碼可以在 https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-6/recipe-01 中找到,其中包含一個Fortran/C例子。該示例在CMake 3.10版(或更高版本)中是有效的,并且已經在GNU/Linux、macOS和Windows(使用MSYS Makefiles)上進行過測試。* 代碼生成在配置時發生,例如:CMake可以檢測操作系統和可用庫;基于這些信息,我們可以定制構建的源代碼。本節和下面的章節中,我們將演示如何生成一個簡單源文件,該文件定義了一個函數,用于報告構建系統配置。 ## 準備工作 此示例的代碼使用Fortran和C語言編寫,第9章將討論混合語言編程。主程序是一個簡單的Fortran可執行程序,它調用一個C函數`print_info()`,該函數將打印配置信息。值得注意的是,在使用Fortran 2003時,編譯器將處理命名問題(對于C函數的接口聲明),如示例所示。我們將使用的`example.f90`作為源文件: ```fortran program hello_world implicit none interface subroutine print_info() bind(c, name="print_info") end subroutine end interface call print_info() end program ``` C函數`print_info()`在模板文件`print_info.c.in`中定義。在配置時,以`@`開頭和結尾的變量將被替換為實際值: ```c++ #include <stdio.h> #include <unistd.h> void print_info(void) { printf("\n"); printf("Configuration and build information\n"); printf("-----------------------------------\n"); printf("\n"); printf("Who compiled | %s\n", "@_user_name@"); printf("Compilation hostname | %s\n", "@_host_name@"); printf("Fully qualified domain name | %s\n", "@_fqdn@"); printf("Operating system | %s\n", "@_os_name@, @_os_release@, @_os_version@"); printf("Platform | %s\n", "@_os_platform@"); printf("Processor info | %s\n", "@_processor_name@, @_processor_description@"); printf("CMake version | %s\n", "@CMAKE_VERSION@"); printf("CMake generator | %s\n", "@CMAKE_GENERATOR@"); printf("Configuration time | %s\n", "@_configuration_time@"); printf("Fortran compiler | %s\n", "@CMAKE_Fortran_COMPILER@"); printf("C compiler | %s\n", "@CMAKE_C_COMPILER@"); printf("\n"); fflush(stdout); } ``` ## 具體實施 在CMakeLists.txt中,我們首先必須對選項進行配置,并用它們的值替換`print_info.c.in`中相應的占位符。然后,將Fortran和C源代碼編譯成一個可執行文件: 1. 聲明了一個Fortran-C混合項目: ```cmake cmake_minimum_required(VERSION 3.10 FATAL_ERROR) project(recipe-01 LANGUAGES Fortran C) ``` 2. 使用`execute_process`為項目獲取當且使用者的信息: ```cmake execute_process( COMMAND whoami TIMEOUT 1 OUTPUT_VARIABLE _user_name OUTPUT_STRIP_TRAILING_WHITESPACE ) ``` 3. 使用`cmake_host_system_information()`函數(已經在第2章第5節遇到過),可以查詢很多系統信息: ```cmake # host name information cmake_host_system_information(RESULT _host_name QUERY HOSTNAME) cmake_host_system_information(RESULT _fqdn QUERY FQDN) # processor information cmake_host_system_information(RESULT _processor_name QUERY PROCESSOR_NAME) cmake_host_system_information(RESULT _processor_description QUERY PROCESSOR_DESCRIPTION) # os information cmake_host_system_information(RESULT _os_name QUERY OS_NAME) cmake_host_system_information(RESULT _os_release QUERY OS_RELEASE) cmake_host_system_information(RESULT _os_version QUERY OS_VERSION) cmake_host_system_information(RESULT _os_platform QUERY OS_PLATFORM) ``` 4. 捕獲配置時的時間戳,并通過使用字符串操作函數: ```cmake string(TIMESTAMP _configuration_time "%Y-%m-%d %H:%M:%S [UTC]" UTC) ``` 5. 現在,準備好配置模板文件`print_info.c.in`。通過CMake的`configure_file`函數生成代碼。注意,這里只要求以`@`開頭和結尾的字符串被替換: ```cmake configure_file(print_info.c.in print_info.c @ONLY) ``` 6. 最后,我們添加一個可執行目標,并定義目標源: ```cmake add_executable(example "") target_sources(example PRIVATE example.f90 ${CMAKE_CURRENT_BINARY_DIR}/print_info.c ) ``` 7. 下面是一個輸出示例: ```shell $ mkdir -p build $ cd build $ cmake .. $ cmake --build . $ ./example Configuration and build information ----------------------------------- Who compiled | somebody Compilation hostname | laptop Fully qualified domain name | laptop Operating system | Linux, 4.16.13-1-ARCH, #1 SMP PREEMPT Thu May 31 23:29:29 UTC 2018 Platform | x86_64 Processor info | Unknown P6 family, 2 core Intel(R) Core(TM) i5-5200U CPU @ 2.20GHz CMake version | 3.11.3 CMake generator | Unix Makefiles Configuration time | 2018-06-25 15:38:03 [UTC] Fortran compiler | /usr/bin/f95 C compiler | /usr/bin/cc ``` ## 工作原理 `configure_file`命令可以復制文件,并用變量值替換它們的內容。示例中,使用`configure_file`修改模板文件的內容,并將其復制到一個位置,然后將其編譯到可執行文件中。如何調用`configure_file`: ```cmake configure_file(print_info.c.in print_info.c @ONLY) ``` 第一個參數是模板的名稱為` print_info.c.in `。CMake假設輸入文件的目錄,與項目的根目錄相對;也就是說,在`${CMAKE_CURRENT_SOURCE_DIR}/print_info.c.in`。我們選擇`print_info.c`,作為第二個參數是配置文件的名稱。假設輸出文件位于相對于項目構建目錄的位置:`${CMAKE_CURRENT_BINARY_DIR}/print_info.c`。 輸入和輸出文件作為參數時,CMake不僅將配置`@VAR@`變量,還將配置`${VAR}`變量。如果`${VAR}`是語法的一部分,并且不應該修改(例如在shell腳本中),那么就很不方便。為了在引導CMake,應該將選項`@ONLY`傳遞給`configure_file`的調用,如前所述。 ## 更多信息 注意,用值替換占位符時,CMake中的變量名應該與將要配置的文件中使用的變量名完全相同,并放在`@`之間。可以在調用`configure_file`時定義的任何CMake變量。我們的示例中,這包括所有內置的CMake變量,如`CMAKE_VERSION`或`CMAKE_GENERATOR`。此外,每當修改模板文件時,重新生成代碼將觸發生成系統的重新生成。這樣,配置的文件將始終保持最新。 **TIPS**:*通過使用`CMake --help-variable-list`,可以從CMake手冊中獲得完整的內部CMake變量列表。* **NOTE**:*`file(GENERATE…)`為提供了一個有趣的替代`configure_file`,這是因為`file`允許將生成器表達式作為配置文件的一部分進行計算。但是,每次運行CMake時,`file(GENERATE…)`都會更新輸出文件,這將強制重新構建依賴于該輸出的所有目標。詳細可參見https://crascit.com/2017/04/18/generated-sources-in-cmake-build 。*
                  <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>

                              哎呀哎呀视频在线观看