CMake是一個跨平臺的安裝(編譯)工具,能夠用簡單的語句來描述全部平臺的安裝(編譯過程)。他可以輸出各類各樣的makefile或者project文件,能測試編譯器所支持的C++特性,相似UNIX下的automake。只是 CMake 的組態檔取名爲 CmakeLists.txt。Cmake 並不直接建構出最終的軟件,而是產生標準的建構檔(如 Unix 的 Makefile 或 Windows Visual C++ 的 projects/workspaces),而後再依通常的建構方式使用。這使得熟悉某個集成開發環境(IDE)的開發者能夠用標準的方式建構他的軟件,這種可使用各平臺的原生建構系統的能力是 CMake 和 SCons 等其餘相似系統的區別之處。linux
cmake應用c++
cmake使用除了應用程序外,就是編寫CMakeLists.txt文檔,以生成Makefile文件。app
1. 每一個目錄下都須要文件CmakeLists.txt文件,CmakeLists.txt的編寫需遵循cmake語法。ide
2. 最好在根目錄下,建立build文件夾,讓後進入build文件夾構建工程,這樣構建工程的中間文件及最後文件都在build中,直接發佈build文件便可。工具
3. 構建命令cd build; cmake ..; make;測試
注:cmake後的..是上層目錄意思。ui
4. 生成可執行程序,運行便可。spa
一個最簡單示例.net
//hello.c #include <stdio.h> int main() { printf("Hello World!\n"); return 0; } //CmakeLists.txt PROJECT(HELLO) SET(SRC_LIST hello.c) ADD_EXECUTABLE(hello ${SRC_LIST})
流程:命令行
~$pwd /home/yuxi/test/cmake/build ~$ls .. build CMakeLists.txt hello.c ~$cmake .. -- The C compiler identification is GNU -- The CXX compiler identification is GNU -- Check for working C compiler: /usr/bin/gcc -- Check for working C compiler: /usr/bin/gcc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- 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 -- Configuring done -- Generating done -- Build files have been written to: /home/yuxi/test/cmake/build ~$ls CMakeCache.txt CMakeFiles cmake_install.cmake Makefile ~$make Scanning dependencies of target hello [100%] Building C object CMakeFiles/hello.dir/hello.c.o Linking C executable hello [100%] Built target hello ~$ls CMakeCache.txt CMakeFiles cmake_install.cmake hello Makefile ~$./hello Hello World! ~$ls CMakeFiles/ CMakeCCompiler.cmake CMakeOutput.log Makefile2 cmake.check_cache CMakeSystem.cmake Makefile.cmake CMakeCXXCompiler.cmake CMakeTmp progress.marks CMakeDetermineCompilerABI_C.bin CompilerIdC TargetDirectories.txt CMakeDetermineCompilerABI_CXX.bin CompilerIdCXX CMakeDirectoryInformation.cmake hello.dir
Cmake debug和release設置
1. 經過命令行指定
cmake -DCMAKE_BUILD_TYPE=Release/Debug ..
2. 在CMakeLists.txt中設置(目前不起做用)
SET(CMAKE_BUILD_TYPE "Debug」) or SET(CMAKE_BUILD_TYPE "Release")
同時可在CMakeLists.txt中設置release和debug編譯選項
SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb") SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
參考:
1. cmake快速入門
2. CMake快速使用教程