回覆列表
  • 1 # 使用者3136867717565

      CMake是一個跨平臺的安裝(編譯)工具,可以用簡單的語句來描述所有平臺的安裝(編譯過程)。他能夠輸出各種各樣的makefile或者project檔案,能測試編譯器所支援的C++特性。只是 CMake 的組態檔取名為 CmakeLists.txt。Cmake 並不直接建構出最終的軟體,而是產生標準的建構檔(如 linux 的 Makefile 或 Windows Visual C++ 的 projects/workspaces),然後再依一般的建構方式使用。

      在 linux 平臺下使用 CMake 生成 Makefile 並編譯的流程如下:

      編寫 CmakeLists.txt。

      執行命令 “cmake PATH” 或者 “ccmake PATH” 生成 Makefile ( PATH 是 CMakeLists.txt 所在的目錄 )。

      使用 make 命令進行編譯

      工程例項:

      一. 編寫各層CMakeLists.txt

      主目錄的主程式main.cpp

      #include "hello.h"

      extern Hello hello;

      int main()

      {

      hello.Print();

      return 0;

      }

      主目錄的CMakeLists.txt

      # to the root binary directory of the project as ${MAIN_BINARY_DIR}.

      project (MAIN)

      #version support

      cmake_minimum_required(VERSION 2.8)

      # Recurse into the "Hello" and "Demo" subdirectories. This does not actually

      # cause another cmake executable to run. The same process will walk through

      # the project"s entire directory structure.

      add_subdirectory (Hello)

      add_subdirectory (Demo)

      # Make sure the compiler can find include files from our Hello library.

      include_directories (${MAIN_SOURCE_DIR}/Hello)

      # Make sure the linker can find the Hello Demo library once it is built.

      link_directories (${HELLO_BINARY_DIR}/Hello)

      link_directories (${HELLO_BINARY_DIR}/Demo)

      #define the source coedes of current directory as DIR_SRCS

      AUX_SOURCE_DIRECTORY(. DIR_SRCS)

      # Add executable called "MAIN" that is built from the source files

      add_executable (Main ${DIR_SRCS})

      # Link the executable to the Hello Demo library.

      target_link_libraries (Main Hello Demo)

      定義專案名project(MAIN),使得當前目錄可以用${MAIN_SOURCE_DIR},由於有2個子目錄,所以需要add_subdirectory它們。由於主程式會使用到其他庫,因而也需要指定連線庫所在目錄。

      主目錄下的作用是利用add_executable將當前目錄下的原始檔編譯成Main程式,然後透過target_link_libraries連結Hello和Demo庫。由於主程式檔案使用了hello.h檔案,所以要include_directories該目錄。

      ---------------------------------------------------------------------------------------------------

      子目錄Demo的子程式demo.c

      #include "hello.h"

      Hello hello;

      子目錄Demo的CMakeLists.txt

      # Make sure the compiler can find include files from our Hello library.

      include_directories (${MAIN_SOURCE_DIR}/Hello)

      #define the source coedes of current directory as DIR_DEMO_SRCS

      AUX_SOURCE_DIRECTORY(. DIR_DEMO_SRCS)

      # Add library called "Demo" that is built from the source files

      add_library (Demo ${DIR_DEMO_SRCS})

      Demo目錄下的CMakeLists主要作用是利用add_library將當前目錄原始碼編譯成Demo庫,由於該庫使用到hello.h檔案,所以要include_directories該目錄。

      ---------------------------------------------------------------------------------------------------

      子目錄Hello的子程式hello.h

      #ifndef _hello_h

      #define _hello_h

      class Hello

      {

      public:

      void Print();

      };

      #endif

      子目錄Hello的子程式hello.c

      #include "hello.h"

      #include

      void Hello::Print()

      {

      printf("Hello, World!\n");

      }

      子目錄Hello的CMakeLists.txt

      #define the source coedes of current directory as DIR_HELLO_SRCS

      AUX_SOURCE_DIRECTORY(. DIR_HELLO_SRCS)

      # Add library called "hello" that is built from the source files

      add_library (Hello ${DIR_HELLO_SRCS})

      Hello目錄下的CMakeLists主要作用是利用add_library將當前目錄原始碼編譯成Hello庫。

      ---------------------------------------------------------------------------------------------------

      二. 執行cmake命令

      至此我們完成了專案中所有 CMakeLists.txt 檔案的編寫,進入目錄 step2 中依次執行命令

      #cmake .

      預設當前目錄,生產makefile

      #make

      最後編譯程式

  • 中秋節和大豐收的關聯?
  • rust裡的直升機怎麼開?