CMake 安裝
CMake 下載
官方下載地址: https://cmake.org/download/

選擇自己系統(Platform)對應的版本並下載
這里我們選擇Windows win64-x64 Installer: Installer tool has changed. Uninstall CMake 3.4 or lower first!

CMake 安裝
安裝時根據自己系統的安全設置,可能會出現如下對話框,不用擔心,直接點擊 "運行(R)"



必須選擇同意,否則不能進入下一步

- 是否添加環境變量,這里我們選擇 "Add CMake to the system PATH for all users"
- 是否創建桌面快捷圖標,根據自身情況而定,這個只是創建桌面圖標使用方便,並不會對以后的使用造成實質上的影響
安裝路徑
這里選擇自己習慣存放程序的路徑,我們這里采取默認值

安裝最后確認
經過前面的操作終於把需要配置的都配置了,下面該程序自己干活了

進入安裝
真正開始安裝的階段,這一階段比較耗時,完全取決於電腦自身的配置高低,系統主要是解壓文件和寫磁盤

安裝完成
恭喜你,終於將CMake安裝完成了

確認CMake安裝
驗證CMake是否成功安裝,可以調出CMD窗口,輸入cmake
,瞧瞧系統會給你說什么,如果出現如下窗口,那么恭喜你沒有任何問題。

那么萬一出現的是如下內容呢

我們一般有如下處理步驟和處理方法:
- 1. 確認是新調出CMD窗口再進行的操作
- 2. 我們可以手動修改系統的環境變量指定CMake的bin目錄位置

確認如圖所示內容在Path中配置,如果沒有可以手動輸入並確定
- 3. 待2操作完成后可以再驗證,如果解決那么恭喜,如果問題仍存在,那么需要重啟系統(一般都能解決了,除非比較低的系統版本可能需要重啟)
mingw
mingw 下載
這里給出64系統使用的mingw, https://sourceforge.net/projects/mingw-w64/

這里其實是下載的一個安裝器,具體的安裝是通過運行這個安裝器來引導安裝的
mingw 安裝

mingw 安裝選項

這里需要做出對應的選擇,當然完全默認沒有任何問題,我們這里采用默認,繼續安裝
mingw 安裝位置

這里有坑,我們先入坑, 繼續安裝
mingw 安裝中

安裝器需要從網上下載所需要的文件,這一步耗時較長
mingw 安裝完成

環境變量設置
同CMake的一樣,mingw安裝完后自動了設置環境變量,你也可以通過運行其安裝目錄下的mingw-w64.bat
來進入運行環境

驗證mingw環境是否設置好,同樣新調出CMD窗口,輸入gcc
命令,出入如下信息則表示安裝沒有問題,否則請參照CMake配置環境變量的方式來解決。

CMake+mingw 實例
我們安裝完環境后來個實例運行下吧
- 編寫源碼文件
來個宇宙最著名的程序吧
1#include <stdio.h>
2int main()
3{
4 printf("hello\n");
5 return 0;
6}
- 編寫CMake文件
1cmake_minimum_required(VERSION 3.0)
2project(Hello)
3set(SOURCE main.cpp)
4add_executable(${PROJECT_NAME} ${SOURCE})
- 生成Make file
1mkdir build
2cd build
3cmake -G"Unix Makefiles" ../
很不幸,這一步會出問題
1CMake Error: CMake was unable to find a build program corresponding to "Unix Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
2CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
3CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
4-- Configuring incomplete, errors occurred!
5See also "D:/tmp/build/CMakeFiles/CMakeOutput.log".
意思就是不能生成Unix Makefiles,這是缺少make程序造成的,
解決方法就是找到mingw安裝目錄下mingw32-make.exe拷貝一份並重命名為make.exe

再運行cmake -G"Unix Makefiles" ../
1$ cmake -G"Unix Makefiles" ../
2-- The C compiler identification is GNU 7.2.0
3-- The CXX compiler identification is GNU 7.2.0
4-- Check for working C compiler: C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/gcc.exe
5-- Check for working C compiler: C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/gcc.exe -- works
6-- Detecting C compiler ABI info
7-- Detecting C compiler ABI info - done
8-- Detecting C compile features
9-- Detecting C compile features - done
10-- Check for working CXX compiler: C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/c++.exe
11-- Check for working CXX compiler: C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/c++.exe -- works
12-- Detecting CXX compiler ABI info
13-- Detecting CXX compiler ABI info - done
14-- Detecting CXX compile features
15-- Detecting CXX compile features - done
16-- Configuring done
17-- Generating done
18-- Build files have been written to: D:/tmp/build
這樣就對了
- 編譯
1make
什么,又有問題
1$ make
2/usr/bin/sh: -c: line 0: syntax error near unexpected token `('
3/usr/bin/sh: -c: line 0: `C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/make -f CMakeFiles/Makefile2 all'
4make: *** [Makefile:84: all] Error 1
還記得前面我們安裝mingw時說的坑嗎,現在我們需要填坑了,文件就是萬惡的C:/Program Files (x86)
,這也好辦,將mingw-w64
文件夾復制到一個正常的目錄吧,比如直接C:/mingw-w64
,然后需要修改環境變量

1$ make
2Scanning dependencies of target Hello
3[ 50%] Building CXX object CMakeFiles/Hello.dir/main.cpp.obj
4[100%] Linking CXX executable Hello.exe
5[100%] Built target Hello
- 運行
1$ ./Hello.exe
2hello
好了,終於成功了