Windows 下使用 MinGW 和 CMake 进行开发

CMake 是个非常棒的项目管理工具,这已经是毋庸置疑的。

一些小工具需要在 win 下开发,所以今天探索使用 MinGW 和 CMake 在 win 下的搭配使用。简单做记录。

MinGW 使用 Qt 5.7.0 安装包中预装的版本。

CMake 直接从官网下载了安装包:https://cmake.org/download/

 

因为 MinGW 和 CMake 完全是无关的两个工具,所以,只需要安装后,把工具的路径加入到系统的 Path,就可以直接在 cmd.exe 中来使用。

因为 windows 版的 CMake 支持很多的编译器,而且其默认的一般是最新的 vs 工具,所以,需要指定 Makefile 的格式:

cmake –G”MinGW Makefiles” .. 

也可以使用 Cmake 提供的 UI 工具,但是你懂的。

尝试编译了一下 Cmake 的 Tutorial,如下:

复制代码
C:\Users\luo\cmake\build>cmake -G"MinGW Makefiles" ..
-- The C compiler identification is GNU 5.3.0
-- The CXX compiler identification is GNU 5.3.0
-- Check for working C compiler: C:/Qt/Qt5.7.0/Tools/mingw530_32/bin/gcc.exe
-- Check for working C compiler: C:/Qt/Qt5.7.0/Tools/mingw530_32/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:/Qt/Qt5.7.0/Tools/mingw530_32/bin/g++.exe
-- Check for working CXX compiler: C:/Qt/Qt5.7.0/Tools/mingw530_32/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: C:/Users/luo/cmake/build

C:\Users\luo\cmake\build>mingw32-make
Scanning dependencies of target MathFunctions
[ 25%] Building CXX object MathFunctions/CMakeFiles/MathFunctions.dir/mysqrt.cxx.obj
[ 50%] Linking CXX static library libMathFunctions.a
[ 50%] Built target MathFunctions
Scanning dependencies of target Tutorial
[ 75%] Building CXX object CMakeFiles/Tutorial.dir/tutorial.cxx.obj
[100%] Linking CXX executable Tutorial.exe
[100%] Built target Tutorial

C:\Users\luo\cmake\build>Tutorial.exe 25
The square root of 25 is 5

C:\Users\luo\cmake\build>Tutorial.exe
Tutorial.exe Version 1.0
Usage: Tutorial.exe number
复制代码

 

PART2

在 qmake 中尝试使用 cmake 来管理非 Qt 项目。

需要在 工具->选项->"构建和运行" 中,先添加 qmake 工具链,然后,在 “构建套件(kits)”中选择刚刚添加的 Cmake 工具链。其中,cmake generator 需要选择 codeblock-mingw Makefiles(有些版本好像要在新建工程的时候再选择 generator)。

打开 QtCreator 新建一个 Non-Qt 的项目,并使用刚才添加的 cmake 作为构建系统,拷贝我们之前写好的文件过来。。。剩下的都交给 qtcreator 就可以了。

 

PART3

因为新版本的 CMAKE 还支持 Ninja 作为构建工具,所以,还可以直接使用 “-G Ninja -DCMAKE_MAKE_PROGRAM=/path/to/ninja” 来生成 Ninja 的规则文件。

Ninja 的使用可以参考这篇:https://www.cnblogs.com/pied/p/9597593.html

感觉 windows 下使用 Ninja 要比 make 方便。

posted @   Biiigfish  阅读(12318)  评论(1编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
历史上的今天:
2013-03-26 曾国藩修身养性十三条
点击右上角即可分享
微信分享提示