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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 13.2 交叉編譯hello world示例 **NOTE**:*此示例代碼可以在 https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-13/recipe-01 中找到,其中包含一個C++示例。該示例在CMake 3.5版(或更高版本)中是有效的,并且已經在GNU/Linux、macOS和Windows上進行過測試。* 這個示例中,我們將重用“Hello World”示例,并將代碼從Linux或macOS交叉編譯到Windows。換句話說,我們將在Linux或macOS上配置和編譯代碼,并生成Windows平臺的可執行文件 ## 準備工作 我們從`hello world`示例(`hello-world.cpp`)開始: ```c++ #include <iostream> #include <omp.h> #include <string> int main(int argc, char *argv[]) { std::cout << "number of available processors: " << omp_get_num_procs() << std::endl; std::cout << "number of threads: " << omp_get_max_threads() << std::endl; auto n = std::stol(argv[1]); std::cout << "we will form sum of numbers from 1 to " << n << std::endl; // start timer auto t0 = omp_get_wtime(); auto s = 0LL; #pragma omp parallel for reduction(+ : s) for (auto i = 1; i <= n; i++) { s += i; } // stop timer auto t1 = omp_get_wtime(); std::cout << "sum: " << s << std::endl; std::cout << "elapsed wall clock time: " << t1 - t0 << " seconds" << std::endl; return 0; } ``` 我們還將使用與前一個示例相同的`CMakeLists.txt`: ```cmake # set minimum cmake version cmake_minimum_required(VERSION 3.5 FATAL_ERROR) # project name and language project(recipe-01 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}) # define executable and its source file add_executable(hello-world hello-world.cpp) # we will print the system name in the code target_compile_definitions(hello-world PUBLIC "SYSTEM_NAME=\"${CMAKE_SYSTEM_NAME}\"" ) install( TARGETS hello-world DESTINATION ${CMAKE_INSTALL_BINDIR} ) ``` 為了交叉編譯源代碼,我們需要安裝一個C++交叉編譯器,也可以為C和Fortran安裝一個交叉編譯器。可以使用打包的MinGW編譯器,作為打包的交叉編譯器的替代方案。還可以使用MXE (M cross environment)從源代碼構建一套交叉編譯器:http://mxe.cc ## 具體實施 我們將按照以下步驟,在這個交叉編譯的“hello world”示例中創建三個文件: 1. 創建一個文件夾,其中包括`hello-world.cpp`和`CMakeLists.txt`。 2. 再創建一個`toolchain.cmake`文件,其內容為: ```cmake # the name of the target operating system set(CMAKE_SYSTEM_NAME Windows) # which compilers to use set(CMAKE_CXX_COMPILER i686-w64-mingw32-g++) # adjust the default behaviour of the find commands: # search headers and libraries in the target environment set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) # search programs in the host environment set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) ``` 3. 將`CMAKE_CXX_COMPILER`設置為對應的編譯器(路徑)。 4. 然后,通過將`CMAKE_TOOLCHAIN_FILE`指向工具鏈文件,從而配置代碼(本例中,使用了從源代碼構建的MXE編譯器): ```shell $ mkdir -p build $ cd build $ cmake -D CMAKE_TOOLCHAIN_FILE=toolchain.cmake .. -- The CXX compiler identification is GNU 5.4.0 -- Check for working CXX compiler: /home/user/mxe/usr/bin/i686-w64-mingw32.static-g++ -- Check for working CXX compiler: /home/user/mxe/usr/bin/i686-w64-mingw32.static-g++ -- 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-recipes/chapter-13/recipe-01/cxx-example/build ``` 5. 現在,構建可執行文件: ```shell $ cmake --build . Scanning dependencies of target hello-world [ 50%] Building CXX object CMakeFiles/hello-world.dir/hello-world.cpp.obj [100%] Linking CXX executable bin/hello-world.exe [100%] Built target hello-world ``` 6. 注意,我們已經在Linux上獲得`hello-world.exe`。將二進制文件復制到Windows上。 7. 在WIndows上可以看到如下的輸出: ```shell Hello from Windows ``` 8. 如你所見,這個二進制可以在Windows下工作。 ## 工作原理 由于與目標環境(Windows)不同的主機環境(在本例中是GNU/Linux或macOS)上配置和構建代碼,所以我們需要向CMake提供關于目標環境的信息,這些信息記錄在`toolchain.cmake`文件中( https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html#cross-compiling )。 首先,提供目標操作系統的名稱: ```cmake set(CMAKE_SYSTEM_NAME Windows) ``` 然后,指定編譯器: ```cmake set(CMAKE_C_COMPILER i686-w64-mingw32-gcc) set(CMAKE_CXX_COMPILER i686-w64-mingw32-g++) set(CMAKE_Fortran_COMPILER i686-w64-mingw32-gfortran) ``` 這個例子中,我們不需要檢測任何庫或頭文件。如果必要的話,我們將使用以下命令指定根路徑: ```cmake set(CMAKE_FIND_ROOT_PATH /path/to/target/environment) ``` 例如,提供MXE編譯器的安裝路徑。 最后,調整`find`命令的默認行為。我們指示CMake在目標環境中查找頭文件和庫文件: ```cmake set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) ``` 在主機環境中的搜索程序: ```cmake set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) ``` ## 更多信息 有關各種選項的更詳細討論,請參見: https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html#cross-compiling
                  <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>

                              哎呀哎呀视频在线观看