原先想寫個簡單的C++程式碼去測試Share Library在Runtime時的執行速度是否與沒用動態載入有差距,光想到要那一行g++的指令就令人頭疼,即時可能已經簡單了,但既然要測試我們就是需要一些彈性調整,那Makefile呢?真的,你上一次自己寫Makefile是何時?
這時候CMake就可以讓我們在最輕量的狀態下產生我們所需要的編譯腳本。這邊內文將會使用Ubuntu 18.04進行測試,直接從程式碼去看我們的結果,詳細撰寫CMake的方式可以去Google會得到更多,這篇想要用最直接的方式去說明我們如何使用CMake,最簡單的流程去感受標準化所帶來的好處。
Cross-platform free and open-source software。不只是Make連Windows也可以使用。
CMake is not a build system but rather it generates another system's build files. It supports directory hierarchies and applications that depend on multiple libraries. It is used in conjunction with native build environments such as Make, Qt Creator, Ninja, Android Studio, Apple's Xcode, and Microsoft Visual Studio. It has minimal dependencies, requiring only a C++ compiler on its own build system.
簡單的概念大概是這樣
過去可能要針對Windows寫Project file,Linux寫Makefile。現在透過CMake通通解決。它提供標準化介面讓開發者撰寫並引出後續平台所需的編譯環境,當然該安裝的還是不能少GCC、Visual Studio等等。至於詳細的網路有很多資源,我就不多贅述,接著可以開始動手實踐一下。
# CMakeLists.txt project(HelloWorld) cmake_minimum_required(VERSION 3.0) add_executable(hello_world main.cpp)
建立build
$ mkdir build
進入到build
$ cd build
產生Makefile
$ cmake ..
編譯
$ make
https://github.com/Shaing/cmake-practice
CMake可以依照你所給的command去產生不同的編譯環境所需要的檔案,因此可以透過這樣的build directory,舉個例子:你也可以自己設計變數在CMakelists.txt內,透過這樣的方式去改變編譯參數,只要切換directory就可以產生不同的編譯結果。
前面一個例子是產生單一的執行檔,那麼該如何產生share library呢?