CMake 是個非常棒的項目管理工具,這已經是毋庸置疑的。
一些小工具需要在 win 下開發,所以今天探索使用 MinGW 和 CMake 在 win 下的搭配使用。簡單做記錄。
MinGW 使用 Qt 5.7.0 安裝包中預裝的版本。
CMake 直接從官網下載了安裝包:https://cmake.org/download/
因為 MinGW 和 CMake 完全是無關的兩個工具,所以,只需要安裝后,把工具的路徑加入到系統的 Path,就可以直接在 cmd.exe 中來使用。
因為 windows 版的 CMake 支持很多的編譯器,而且其默認的一般是最新的 vs 工具,所以,需要指定 Makefile 的格式:
cmake –G”MinGW Makefiles” ..
也可以使用 Cmake 提供的 UI 工具,但是你懂的。
嘗試編譯了一下 Cmake 的 Tutorial,如下:
C:\Users\luo\cmake\build>cmake -G"MinGW Makefiles" .. -- The C compiler identification is GNU 5.3.0 -- The CXX compiler identification is GNU 5.3.0 -- Check for working C compiler: C:/Qt/Qt5.7.0/Tools/mingw530_32/bin/gcc.exe -- Check for working C compiler: C:/Qt/Qt5.7.0/Tools/mingw530_32/bin/gcc.exe -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: C:/Qt/Qt5.7.0/Tools/mingw530_32/bin/g++.exe -- Check for working CXX compiler: C:/Qt/Qt5.7.0/Tools/mingw530_32/bin/g++.exe -- 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: C:/Users/luo/cmake/build C:\Users\luo\cmake\build>mingw32-make Scanning dependencies of target MathFunctions [ 25%] Building CXX object MathFunctions/CMakeFiles/MathFunctions.dir/mysqrt.cxx.obj [ 50%] Linking CXX static library libMathFunctions.a [ 50%] Built target MathFunctions Scanning dependencies of target Tutorial [ 75%] Building CXX object CMakeFiles/Tutorial.dir/tutorial.cxx.obj [100%] Linking CXX executable Tutorial.exe [100%] Built target Tutorial C:\Users\luo\cmake\build>Tutorial.exe 25 The square root of 25 is 5 C:\Users\luo\cmake\build>Tutorial.exe Tutorial.exe Version 1.0 Usage: Tutorial.exe number
PART2
在 qmake 中嘗試使用 cmake 來管理非 Qt 項目。
需要在 工具->選項->"構建和運行" 中,先添加 qmake 工具鏈,然后,在 “構建套件(kits)”中選擇剛剛添加的 Cmake 工具鏈。其中,cmake generator 需要選擇 codeblock-mingw Makefiles(有些版本好像要在新建工程的時候再選擇 generator)。
打開 QtCreator 新建一個 Non-Qt 的項目,並使用剛才添加的 cmake 作為構建系統,拷貝我們之前寫好的文件過來。。。剩下的都交給 qtcreator 就可以了。
PART3
因為新版本的 CMAKE 還支持 Ninja 作為構建工具,所以,還可以直接使用 “-G Ninja -DCMAKE_MAKE_PROGRAM=/path/to/ninja” 來生成 Ninja 的規則文件。
Ninja 的使用可以參考這篇:https://www.cnblogs.com/pied/p/9597593.html
感覺 windows 下使用 Ninja 要比 make 方便。