本文教你如何在windows環(huán)境下使用CMake和mingw搭建同linux開發(fā)一樣體驗的c/c++開發(fā)環(huán)境
CMake 安裝
CMake 下載
官方下載地址: https://cmake.org/download/
選擇自己系統(tǒng)(Platform)對應(yīng)的版本并下載
這里我們選擇Windows win64-x64 Installer: Installer tool has changed. Uninstall CMake 3.4 or lower first!
CMake 安裝
安裝時根據(jù)自己系統(tǒng)的安全設(shè)置态贤,可能會出現(xiàn)如下對話框会傲,不用擔(dān)心,直接點擊 "運行(R)"
必須選擇同意抒和,否則不能進入下一步
- 是否添加環(huán)境變量,這里我們選擇 "Add CMake to the system PATH for all users"
- 是否創(chuàng)建桌面快捷圖標(biāo)管怠,根據(jù)自身情況而定袭祟,這個只是創(chuàng)建桌面圖標(biāo)使用方便,并不會對以后的使用造成實質(zhì)上的影響
安裝路徑
這里選擇自己習(xí)慣存放程序的路徑捂贿,我們這里采取默認值
安裝最后確認
經(jīng)過前面的操作終于把需要配置的都配置了纠修,下面該程序自己干活了
進入安裝
真正開始安裝的階段,這一階段比較耗時厂僧,完全取決于電腦自身的配置高低扣草,系統(tǒng)主要是解壓文件和寫磁盤
安裝完成
共享你,終于將CMake安裝完成了
確認CMake安裝
驗證CMake是否成功安裝,可以調(diào)出CMD窗口德召,輸入cmake
白魂,瞧瞧系統(tǒng)會給你說什么,如果出現(xiàn)如下窗口上岗,那么恭喜你沒有任何問題福荸。
那么萬一出現(xiàn)的是如下內(nèi)容呢
我們一般有如下處理步驟和處理方法:
- 確認是新調(diào)出CMD窗口再進行的操作
-
我們可以手動修改系統(tǒng)的環(huán)境變量指定CMake的bin目錄位置
確認如圖所示內(nèi)容在Path中配置,如果沒有可以手動輸入并確定
-
- 待2操作完成后可以再驗證肴掷,如果解決那么恭喜敬锐,如果問題仍存在,那么需要重啟系統(tǒng)(一般都能解決了呆瞻,除非比較低的系統(tǒng)版本可能需要重啟)
mingw
mingw 下載
這里給出64系統(tǒng)使用的mingw, https://sourceforge.net/projects/mingw-w64/
這里其實是下載的一個安裝器台夺,具體的安裝是通過運行這個安裝器來引導(dǎo)安裝的
mingw 安裝
mingw 安裝選項
這里需要做出對應(yīng)的選擇,當(dāng)然完全默認沒有任何問題痴脾,我們這里采用默認颤介,繼續(xù)安裝
mingw 安裝位置
這里有坑,我們先入坑, 繼續(xù)安裝
mingw 安裝中
安裝器需要從網(wǎng)上下載所需要的文件赞赖,這一步耗時較長
mingw 安裝完成
環(huán)境變量設(shè)置
同CMake的一樣滚朵,mingw安裝完后自動了設(shè)置環(huán)境變量,你也可以通過運行其安裝目錄下的mingw-w64.bat
來進入運行環(huán)境
驗證mingw環(huán)境是否設(shè)置好前域,同樣新調(diào)出CMD窗口辕近,輸入
gcc
命令,出入如下信息則表示安裝沒有問題匿垄,否則請參照CMake配置環(huán)境變量的方式來解決移宅。CMake+mingw 實例
我們安裝完環(huán)境后來個實例運行下吧
- 編寫源碼文件
來個宇宙最著名的程序吧
#include <stdio.h>
int main()
{
printf("hello\n");
return 0;
}
- 編寫CMake文件
cmake_minimum_required(VERSION 3.0)
project(Hello)
set(SOURCE main.cpp)
add_executable(${PROJECT_NAME} ${SOURCE})
- 生成Make file
mkdir build
cd build
cmake -G"Unix Makefiles" ../
很不幸,這一步會出問題
CMake 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.
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!
See also "D:/tmp/build/CMakeFiles/CMakeOutput.log".
意思就是不能生成Unix Makefiles椿疗,這是缺少make程序造成的漏峰,
解決方法就是找到mingw安裝目錄下mingw32-make.exe拷貝一份并重命名為make.exe
再運行
cmake -G"Unix Makefiles" ../
$ cmake -G"Unix Makefiles" ../
-- The C compiler identification is GNU 7.2.0
-- The CXX compiler identification is GNU 7.2.0
-- Check for working C compiler: C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/gcc.exe
-- 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
-- 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:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/c++.exe
-- 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
-- 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: D:/tmp/build
這樣就對了
- 編譯
make
什么,又有問題
$ make
/usr/bin/sh: -c: line 0: syntax error near unexpected token `('
/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'
make: *** [Makefile:84: all] Error 1
還記得前面我們安裝mingw時說的坑嗎变丧,現(xiàn)在我們需要填坑了芽狗,文件就是萬惡的C:/Program Files (x86)
,這也好辦痒蓬,將mingw-w64
文件夾復(fù)制到一個正常的目錄吧童擎,比如直接C:/mingw-w64
,然后需要修改環(huán)境變量
$ make
Scanning dependencies of target Hello
[ 50%] Building CXX object CMakeFiles/Hello.dir/main.cpp.obj
[100%] Linking CXX executable Hello.exe
[100%] Built target Hello
- 運行
$ ./Hello.exe
hello
好了攻晒,終于成功了