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:Usersluocmakeuild>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:Usersluocmakeuild>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:Usersluocmakeuild>Tutorial.exe 25
The square root of 25 is 5

C:Usersluocmakeuild>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 方便。

原文地址:https://www.cnblogs.com/pied/p/8651650.html