Windows中像在Linux里一样使用CMake和make
1. 安装GCC环境
1.1 安装MinGW(Minimalist GNU for Windows)
首先下载MinGW,并安装。安装完成之后运行MinGW Installer。界面如下。勾选自己需要安装的包,然后点击Apply Changes执行。
其中除了一些必须的包之外,mingw32-make是执行make命令需要。
参考教程:https://zhuanlan.zhihu.com/p/66197013
1.2 添加环境变量
之后在系统环境变量的PATH中添加 MinGW\bin 这个位置。在终端中试着用一下gcc gdb等命令确定可行。
2. 安装CMake
在CMake官网上下载并安装CMake。安装时注意勾选将路径添加到环境变量选项。
参考教程:https://gclxry.com/use-cmake-on-windows/
3. 使用CMake编译
与linux下不同的是,使用cmake要加入参数 -G 选择使用的生成器,直接回车会列出下面的生成器。
1 Generators 2 Visual Studio 16 2019 = Generates Visual Studio 2019 project files. 3 Use -A option to specify architecture. 4 * Visual Studio 15 2017 [arch] = Generates Visual Studio 2017 project files. 5 Optional [arch] can be "Win64" or "ARM". 6 Visual Studio 14 2015 [arch] = Generates Visual Studio 2015 project files. 7 Optional [arch] can be "Win64" or "ARM". 8 Visual Studio 12 2013 [arch] = Generates Visual Studio 2013 project files. 9 Optional [arch] can be "Win64" or "ARM". 10 Visual Studio 11 2012 [arch] = Generates Visual Studio 2012 project files. 11 Optional [arch] can be "Win64" or "ARM". 12 Visual Studio 10 2010 [arch] = Generates Visual Studio 2010 project files. 13 Optional [arch] can be "Win64" or "IA64". 14 Visual Studio 9 2008 [arch] = Generates Visual Studio 2008 project files. 15 Optional [arch] can be "Win64" or "IA64". 16 Borland Makefiles = Generates Borland makefiles. 17 NMake Makefiles = Generates NMake makefiles. 18 NMake Makefiles JOM = Generates JOM makefiles. 19 MSYS Makefiles = Generates MSYS makefiles. 20 MinGW Makefiles = Generates a make file for use with 21 mingw32-make. 22 Unix Makefiles = Generates standard UNIX makefiles. 23 Green Hills MULTI = Generates Green Hills MULTI files 24 (experimental, work-in-progress). 25 Ninja = Generates build.ninja files. 26 Ninja Multi-Config = Generates build-<Config>.ninja files. 27 CodeBlocks - MinGW Makefiles = Generates CodeBlocks project files. 28 CodeBlocks - NMake Makefiles = Generates CodeBlocks project files. 29 CodeBlocks - NMake Makefiles JOM 30 CodeBlocks - Unix Makefiles = Generates CodeBlocks project files. 31 CodeLite - MinGW Makefiles = Generates CodeLite project files. 32 CodeLite - NMake Makefiles = Generates CodeLite project files. 33 CodeLite - Ninja = Generates CodeLite project files. 34 CodeLite - Unix Makefiles = Generates CodeLite project files. 35 Sublime Text 2 - MinGW Makefiles 36 = Generates Sublime Text 2 project files. 37 Sublime Text 2 - NMake Makefiles 38 = Generates Sublime Text 2 project files. 39 Sublime Text 2 - Ninja = Generates Sublime Text 2 project files. 40 Sublime Text 2 - Unix Makefiles 41 = Generates Sublime Text 2 project files. 42 Kate - MinGW Makefiles = Generates Kate project files. 43 Kate - NMake Makefiles = Generates Kate project files. 44 Kate - Ninja = Generates Kate project files. 45 Kate - Unix Makefiles = Generates Kate project files. 46 Eclipse CDT4 - NMake Makefiles 47 = Generates Eclipse CDT 4.0 project files. 48 Eclipse CDT4 - MinGW Makefiles 49 = Generates Eclipse CDT 4.0 project files. 50 Eclipse CDT4 - Ninja = Generates Eclipse CDT 4.0 project files. 51 Eclipse CDT4 - Unix Makefiles= Generates Eclipse CDT 4.0 project files.
这里我使用
cmake .. -G "MinGW Makefiles"
可以看到生成了Makefile文件。接着我们输入mingw32-make.exe就可以生成main.exe了。
这里mingw32-make.exe有些麻烦。我们可以把这个文件重命名成make.exe,就可以直接使用make了。
这里生产的文件是.exe文件,和Linux下的还有所不同。
-------------------------------------------------------------------------
花有重开日,人无再少年