發表文章

目前顯示的是 6月, 2017的文章

CMake 編寫

在linux底下除了make(makefile), 就是使用cmake(CMakeLists.txt)來編譯程式或套件(packages). 這邊用範例的方式來記錄用法 Example 1: 簡易的hello world專案 Example 2: 多層階層的專案(hierarchical CMakeLists.txt) Example 3: 建立靜態與動態程式庫(static and dynamic libraries) Example 4: 使用外部函式庫 Example 1: 簡易的hello world專案 [Top] 簡易的hello world範例, 編譯一個執行檔, 以下為project底下的檔案列表 . ├── CMakeLists.txt ├── build └── hello _ world.c CMakeLists.txt裡面填寫所要編譯相關規則. build是一個空目錄, 進去此目錄後, 下達cmake相關指令後, 把編譯的檔案放置此處 以下為CMakeLists.txt內容 1 2 3 4 5 # recommend to add these two lines cmake_minimum_required ( VERSION 2.8.9 ) project ( hello_world ) add_executable ( hello_world hello_world.c ) 行2是規定所需要的最低cmake版本. 行3是專案名稱. 行5則是編譯一個名稱為hello_world執行檔,其source檔案為hello_world.c #編譯 $ mkdir build ; cd build; cmake ..; make #執行程式 $ ./hello_world hello world! Example 2: 多層階層的專案(hierarchical CMakeLists.txt) [Top] 階層的CMakeLists.txt範例 . ├── CMakeLists.txt ├── src1 │   ├── CMakeLists.txt │   └── foo.cpp └── src2 ├── CMakeList