windows+CMake+mingw 搭建c c++開發環境
CMake 安裝
CMake 下載
官方下載地址: https://cmake.org/download/前端

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

CMake 安裝
安裝時根據本身系統的安全設置,可能會出現以下對話框,不用擔憂,直接點擊 "運行(R)"nginx



必須選擇贊成,不然不能進入下一步c++

- 是否添加環境變量,這裏咱們選擇 "Add CMake to the system PATH for all users"
- 是否建立桌面快捷圖標,根據自身狀況而定,這個只是建立桌面圖標使用方便,並不會對之後的使用形成實質上的影響
安裝路徑
這裏選擇本身習慣存放程序的路徑,咱們這裏採起默認值git

安裝最後確認
通過前面的操做終於把須要配置的都配置了,下面該程序本身幹活了程序員

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

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

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

那麼萬一出現的是以下內容呢

咱們通常有以下處理步驟和處理方法:
- 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
好了,終於成功了