本文教你如何在windows环境下使用CMake和mingw搭建同linux开发一样体验的c/c++开发环境
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
,瞧瞧系统会给你说什么,如果出现如下窗口,那么恭喜你没有任何问题。

那么万一出现的是如下内容呢

我们一般有如下处理步骤和处理方法:
- 确认是新调出CMD窗口再进行的操作
-
我们可以手动修改系统的环境变量指定CMake的bin目录位置
系统环境变量 Path
确认如图所示内容在Path中配置,如果没有可以手动输入并确定
-
- 待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 实例
我们安装完环境后来个实例运行下吧
- 编写源码文件
来个宇宙最著名的程序吧
#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时说的坑吗,现在我们需要填坑了,文件就是万恶的C:/Program Files (x86)
,这也好办,将mingw-w64
文件夹复制到一个正常的目录吧,比如直接C:/mingw-w64
,然后需要修改环境变量

$ 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
好了,终于成功了
网友评论