<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 1.1 將單個源文件編譯為可執行文件 **NOTE**:*此示例代碼可以在 https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-01/recipe-01 中找到,包含C++、C和Fortran示例。該示例在CMake 3.5版(或更高版本)中是有效的,并且已經在GNU/Linux、macOS和Windows上進行過測試。* 本節示例中,我們將演示如何運行CMake配置和構建一個簡單的項目。該項目由單個源文件組成,用于生成可執行文件。我們將用C++討論這個項目,您在GitHub示例庫中可以找到C和Fortran的例子。 ## 準備工作 我們希望將以下源代碼編譯為單個可執行文件: ```c++ #include <cstdlib> #include <iostream> #include <string> std::string say_hello() { return std::string("Hello, CMake world!"); } int main() { std::cout << say_hello() << std::endl; return EXIT_SUCCESS; } ``` ## 具體實施 除了源文件之外,我們還需要向CMake提供項目配置描述。該描述使用CMake完成,完整的文檔可以在 https://cmake.org/cmake/help/latest/ 找到。我們把CMake指令放入一個名為`CMakeLists.txt`的文件中。 **NOTE**:*文件的名稱區分大小寫,必須命名為`CMakeLists.txt`,CMake才能夠解析。* 具體步驟如下: 1. 用編輯器打開一個文本文件,將這個文件命名為`CMakeLists.txt`。 2. 第一行,設置CMake所需的最低版本。如果使用的CMake版本低于該版本,則會發出致命錯誤: ```cmake cmake_minimum_required(VERSION 3.5 FATAL_ERROR) ``` 3. 第二行,聲明了項目的名稱(`recipe-01`)和支持的編程語言(CXX代表C++): ```cmake project(recipe-01 LANGUAGES CXX) ``` 4. 指示CMake創建一個新目標:可執行文件`hello-world`。這個可執行文件是通過編譯和鏈接源文件`hello-world.cpp`生成的。CMake將為編譯器使用默認設置,并自動選擇生成工具: ```cmake add_executable(hello-world hello-world.cpp) ``` 5. 將該文件與源文件`hello-world.cpp`放在相同的目錄中。記住,它只能被命名為`CMakeLists.txt`。 6. 現在,可以通過創建`build`目錄,在`build`目錄下來配置項目: ```shell $ mkdir -p build $ cd build $ cmake .. -- The CXX compiler identification is GNU 8.1.0 -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: /home/user/cmake-cookbook/chapter-01/recipe-01/cxx-example/build ``` 7. 如果一切順利,項目的配置已經在`build`目錄中生成。我們現在可以編譯可執行文件: ```shell $ cmake --build . Scanning dependencies of target hello-world [ 50%] Building CXX object CMakeFiles/hello-world.dir/hello-world.cpp.o [100%] Linking CXX executable hello-world [100%] Built target hello-world ``` ## 工作原理 示例中,我們使用了一個簡單的`CMakeLists.txt`來構建“Hello world”可執行文件: ```cmake cmake_minimum_required(VERSION 3.5 FATAL_ERROR) project(recipe-01 LANGUAGES CXX) add_executable(hello-world hello-world.cpp) ``` **NOTE**:*CMake語言不區分大小寫,但是參數區分大小寫。* **TIPS**:*CMake中,C++是默認的編程語言。不過,我們還是建議使用`LANGUAGES`選項在`project`命令中顯式地聲明項目的語言。* 要配置項目并生成構建器,我們必須通過命令行界面(CLI)運行CMake。CMake CLI提供了許多選項,`cmake -help`將輸出以顯示列出所有可用選項的完整幫助信息,我們將在書中對這些選項進行更多地了解。正如您將從`cmake -help`的輸出中顯示的內容,它們中的大多數選項會讓你您訪問CMake手冊,查看詳細信息。通過下列命令生成構建器: ```shell $ mkdir -p build $ cd build $ cmake .. ``` 這里,我們創建了一個目錄`build`(生成構建器的位置),進入`build`目錄,并通過指定`CMakeLists.txt`的位置(本例中位于父目錄中)來調用CMake。可以使用以下命令行來實現相同的效果: ```shell $ cmake -H. -Bbuild ``` 該命令是跨平臺的,使用了`-H`和`-B`為CLI選項。`-H`表示當前目錄中搜索根`CMakeLists.txt`文件。`-Bbuild`告訴CMake在一個名為`build`的目錄中生成所有的文件。 **NOTE**:*`cmake -H. -Bbuild`也屬于CMake標準使用方式: https://cmake.org/pipermail/cmake-developers/2018-January/030520.html 。不過,我們將在本書中使用傳統方法(創建一個構建目錄,進入其中,并通過將CMake指向`CMakeLists.txt`的位置來配置項目)。* 運行`cmake`命令會輸出一系列狀態消息,顯示配置信息: ```shell $ cmake .. -- The CXX compiler identification is GNU 8.1.0 -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: /home/user/cmake-cookbook/chapter-01/recipe-01/cxx-example/build ``` **NOTE**:*在與`CMakeLists.txt`相同的目錄中執行`cmake .`,原則上足以配置一個項目。然而,CMake會將所有生成的文件寫到項目的根目錄中。這將是一個源代碼內構建,通常是不推薦的,因為這會混合源代碼和項目的目錄樹。我們首選的是源外構建。* CMake是一個構建系統生成器。將描述構建系統(如:Unix Makefile、Ninja、Visual Studio等)應當如何操作才能編譯代碼。然后,CMake為所選的構建系統生成相應的指令。默認情況下,在GNU/Linux和macOS系統上,CMake使用Unix Makefile生成器。Windows上,Visual Studio是默認的生成器。在下一個示例中,我們將進一步研究生成器,并在第13章中重新討論生成器。 GNU/Linux上,CMake默認生成Unix Makefile來構建項目: * `Makefile`: `make`將運行指令來構建項目。 * `CMakefile`:包含臨時文件的目錄,CMake用于檢測操作系統、編譯器等。此外,根據所選的生成器,它還包含特定的文件。 * `cmake_install.cmake`:處理安裝規則的CMake腳本,在項目安裝時使用。 * `CMakeCache.txt`:如文件名所示,CMake緩存。CMake在重新運行配置時使用這個文件。 要構建示例項目,我們運行以下命令: ```shell $ cmake --build . ``` 最后,CMake不強制指定構建目錄執行名稱或位置,我們完全可以把它放在項目路徑之外。這樣做同樣有效: ```shell $ mkdir -p /tmp/someplace $ cd /tmp/someplace $ cmake /path/to/source $ cmake --build . ``` ## 更多信息 官方文檔 https://cmake.org/runningcmake/ 給出了運行CMake的簡要概述。由CMake生成的構建系統,即上面給出的示例中的Makefile,將包含為給定項目構建目標文件、可執行文件和庫的目標及規則。`hello-world`可執行文件是在當前示例中的唯一目標,運行以下命令: ```shell $ cmake --build . --target help The following are some of the valid targets for this Makefile: ... all (the default if no target is provided) ... clean ... depend ... rebuild_cache ... hello-world ... edit_cache ... hello-world.o ... hello-world.i ... hello-world.s ``` CMake生成的目標比構建可執行文件的目標要多。可以使用`cmake --build . --target <target-name>`語法,實現如下功能: * **all**(或Visual Studio generator中的ALL_BUILD)是默認目標,將在項目中構建所有目標。 * **clean**,刪除所有生成的文件。 * **rebuild_cache**,將調用CMake為源文件生成依賴(如果有的話)。 * **edit_cache**,這個目標允許直接編輯緩存。 對于更復雜的項目,通過測試階段和安裝規則,CMake將生成額外的目標: * **test**(或Visual Studio generator中的**RUN_TESTS**)將在CTest的幫助下運行測試套件。我們將在第4章中詳細討論測試和CTest。 * **install**,將執行項目安裝規則。我們將在第10章中討論安裝規則。 * **package**,此目標將調用CPack為項目生成可分發的包。打包和CPack將在第11章中討論。
                  <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>

                              哎呀哎呀视频在线观看