【CMake】Win10 + CMake + MinGW 简单使用

1、安装CMake

CMake是一个跨平台的自动化建构系统,它使用一个名为 CMakeLists.txt 的文件来描述构建过程。CMake可以运行在Windows和Linux环境下,根据配置的编译器不同,生成符合所在操作系统的 Makefile 文件,在Windows下可以生成VS的工程文件系统。
将CMake/bin 目录加入到系统环境变量Path中
在这里插入图片描述
在cmd输入 cmake --verison 可查看cmake是否成功,如成功将显示版本

C:\Users\wt>cmake --version
cmake version 3.13.0-rc3
CMake suite maintained and supported by Kitware (kitware.com/cmake).
C:\Users\wt>
2、安装MinGW

MinGW是一个编译环境,包含了C++代码编译过程中需要的三方库、头文件……用于完成C++源码的编译、链接,类似于早期Windows下的Turbo C。现在的VisualStudio环境集成了编译、连接的功能,所以实际的编译、连接过程对开发者是不可见的,不同的是,VS使用了微软的编译器、连接器。在安装、配置MinGW后,我们就可以在命令行中通过g++命令编译、链接生成可执行文件。
将 MinGW\bin 目录加入到系统环境变量Path中
在这里插入图片描述
在cmd输入 mingw-make --verison 可查MinGW是否成功,如成功将显示版本

C:\Users\wt>mingw32-make --version
GNU Make 3.82.90
Built for i686-pc-mingw32
Copyright (C) 1988-2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

C:\Users\wt>
3、创建源程序

创建一个hello.cpp的源程序,代码如下

#include <iostream>
using namespace std;
 
int main(){
	cout << "hello cmake!!!" <<  endl;
    
	return 0;
}
4、创建CMakeLists.txt文件

创建一个 CMakeLists.txt 文件,内容如下

# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)

# 项目信息
project(hello)

# 指定生成目标,编译hello.cpp源程序,生成hello.exe的可执行程序
add_executable(hello hello.cpp)

创建完后,目录文件如下
在这里插入图片描述

5、生成 Makefile

移动到源码所在目录,在cmd中执行命令 cmake -G “MinGW Makefiles” .

F:\CMake\demo-01>cmake -G "MinGW Makefiles" .
-- The C compiler identification is GNU 6.3.0
-- The CXX compiler identification is GNU 6.3.0
-- Check for working C compiler: C:/MinGW/bin/gcc.exe
-- Check for working C compiler: C:/MinGW/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:/MinGW/bin/g++.exe
-- Check for working CXX compiler: C:/MinGW/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: F:/CMake/demo-01

注意,该命令最后是一个点 . 表示当前目录,“MinGW Makefiles”表示指定makefile类型为 MinGW,如果使用vs的话,应指定为“NMake Makefiles”。完成后文件目录为,可以看到已经生成了一个 Makefile 文件。
在这里插入图片描述

6、编译

执行 mingw32-make 命令,对应于 linux 系统的 make 命令

F:\CMake\demo-01>mingw32-make
Scanning dependencies of target hello
[ 50%] Building CXX object CMakeFiles/hello.dir/hello.cpp.obj
[100%] Linking CXX executable hello.exe
[100%] Built target hello

执行后,文件目录如下
在这里插入图片描述
可以看到,已经生成了可执行文件 hello.exe

7、运行

在cmd继续输入 hello.exe 命令

F:\CMake\demo-01>hello.exe
hello word cmake!!!

可以看到,程序得到了正确的输出。

鸣谢:
windows下使用cmake+mingw配置makefile(一)

  • 17
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值