内容纲要

msys2 安装

安装完先升级

$ pacman -Syu

关闭后再执行一遍

$ pacman -Syu

搜索 gcc 相关安装包

$ pacman -Ss gcc
$ pacman -Ss gcc | grep mingw64

发现 gcc 9.2 在 mingw-w64-x86_64-toolchain 这个包,安装

$ pacman -S mingw-w64-x86_64-toolchain
$ pacman -S vim
$ pacman -S mingw-w64-x86_64-clang
$ exit
pacman -S --needed base-devel mingw-w64-i686-toolchain mingw-w64-x86_64-toolchain \
                    git subversion mercurial \
                    mingw-w64-i686-cmake mingw-w64-x86_64-cmake

安装 boost

# 搜索
$ pacman -Ss boost

# 安装
$ pacman -Sy mingw-w64-i686-boost mingw-w64-x86_64-boost

编译 64 位程序

启动 MSYS2 MinGW 64-bit

$ gcc --version
$ cd ~
$ vim fs_test.cpp
#include <iostream>
#include <filesystem>

int main() {
    for (auto &file : std::filesystem::recursive_directory_iterator("./")) {
        std::cout << file.path() << '\n';
    }
}

以上程序使用 C++ 17 Filesystem 递归遍历所有文件。

$ cd ~
$ ls
$ g++ -std=c++17 -Wall -Wextra -pedantic fs_test.cpp -o fs_test
$ ./fs_test

Code::Blocks IDE 安装

安装完,进入 Settings -> Compiler 菜单,修改编译选项

默认为

Compiler's installation directory: C:\MinGW
C compiler: mingw32-gcc.exe
C++ compiler: mingw32-g++.exe
Linker for dynamic libs: mingw32-g++.exe
Linker for static libs: ar.exe
Debugger: GDB/CDB debugger : Default
Resource compiler: windres.exe
Make program: mingw32-make.exe

修改为

Compiler's installation directory: C:\msys64\mingw64
C compiler: gcc.exe
C++ compiler: g++.exe
Linker for dynamic libs: g++.exe
Linker for static libs: ar.exe
Debugger: GDB/CDB debugger : Default
Resource compiler: windres.exe
Make program: mingw32-make.exe

默认情况, GCC 9 使用 C++ 14 标准编译 C++ 代码,对于 C 程序, 默认标准为 C11。

使用 C++ 17 标准,需要设置一个编译器标志。

以下已废弃,新版 codeblocks 可以设置 -std=c++17

New flag 对话框

Name: Have g++ follow the C++17 standard
Compiler flags: -std=c++17
Linker flags:
Category: General
Check against:
Check message:
Supersedes:
Exclusive: False

然后勾选编译选项: Have g++ follow the C++17 standard [-std=c++17]

使用 cmake

先查看 cmake 支持的 Generators

cmake --help
The following generators are available on this platform (* marks default):
* Visual Studio 16 2019        = Generates Visual Studio 2019 project files.
                                 Use -A option to specify architecture.
  Visual Studio 15 2017 [arch] = Generates Visual Studio 2017 project files.
                                 Optional [arch] can be "Win64" or "ARM".
  Visual Studio 14 2015 [arch] = Generates Visual Studio 2015 project files.
                                 Optional [arch] can be "Win64" or "ARM".
  Visual Studio 12 2013 [arch] = Generates Visual Studio 2013 project files.
                                 Optional [arch] can be "Win64" or "ARM".
  Visual Studio 11 2012 [arch] = Generates Visual Studio 2012 project files.
                                 Optional [arch] can be "Win64" or "ARM".
  Visual Studio 10 2010 [arch] = Generates Visual Studio 2010 project files.
                                 Optional [arch] can be "Win64" or "IA64".
  Visual Studio 9 2008 [arch]  = Generates Visual Studio 2008 project files.
                                 Optional [arch] can be "Win64" or "IA64".
  Borland Makefiles            = Generates Borland makefiles.
  NMake Makefiles              = Generates NMake makefiles.
  NMake Makefiles JOM          = Generates JOM makefiles.
  MSYS Makefiles               = Generates MSYS makefiles.
  MinGW Makefiles              = Generates a make file for use with
                                 mingw32-make.
  Unix Makefiles               = Generates standard UNIX makefiles.
  Green Hills MULTI            = Generates Green Hills MULTI files
                                 (experimental, work-in-progress).
  Ninja                        = Generates build.ninja files.
  Watcom WMake                 = Generates Watcom WMake makefiles.
  CodeBlocks - MinGW Makefiles = Generates CodeBlocks project files.
  CodeBlocks - NMake Makefiles = Generates CodeBlocks project files.
  CodeBlocks - NMake Makefiles JOM
                               = Generates CodeBlocks project files.
  CodeBlocks - Ninja           = Generates CodeBlocks project files.
  CodeBlocks - Unix Makefiles  = Generates CodeBlocks project files.
  CodeLite - MinGW Makefiles   = Generates CodeLite project files.
  CodeLite - NMake Makefiles   = Generates CodeLite project files.
  CodeLite - Ninja             = Generates CodeLite project files.
  CodeLite - Unix Makefiles    = Generates CodeLite project files.
  Sublime Text 2 - MinGW Makefiles
                               = Generates Sublime Text 2 project files.
  Sublime Text 2 - NMake Makefiles
                               = Generates Sublime Text 2 project files.
  Sublime Text 2 - Ninja       = Generates Sublime Text 2 project files.
  Sublime Text 2 - Unix Makefiles
                               = Generates Sublime Text 2 project files.
  Kate - MinGW Makefiles       = Generates Kate project files.
  Kate - NMake Makefiles       = Generates Kate project files.
  Kate - Ninja                 = Generates Kate project files.
  Kate - Unix Makefiles        = Generates Kate project files.
  Eclipse CDT4 - NMake Makefiles
                               = Generates Eclipse CDT 4.0 project files.
  Eclipse CDT4 - MinGW Makefiles
                               = Generates Eclipse CDT 4.0 project files.
  Eclipse CDT4 - Ninja         = Generates Eclipse CDT 4.0 project files.
  Eclipse CDT4 - Unix Makefiles= Generates Eclipse CDT 4.0 project files.

将 MinGW 添加到 PATH 路径

@echo off
set path=%~dp0;D:\nginxstack\cmake\bin;D:\downloads\mingw-17.1\MinGW\bin;%path%
md build  1>nul 2>nul
cd build
md Debug 1>nul 2>nul
md Debug2 1>nul 2>nul
md Debug3 1>nul 2>nul
md Release 1>nul 2>nul
cd /d "%~dp0"

::cd build\Debug
::echo 生成 Debug 版
::call cmake_cmd.bat ../.. -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Debug 
::cd /d "%~dp0"

cd build\Debug2
echo 生成 Debug2 版
call cmake_cmd.bat ../.. -G "CodeBlocks - MinGW Makefiles" -DCMAKE_BUILD_TYPE=Debug 
cd /d "%~dp0"

::cd build\Debug3
::echo 生成 Debug3 版
::call cmake_cmd.bat ../.. -G "CodeLite - MinGW Makefiles" -DCMAKE_BUILD_TYPE=Debug 
::cd /d "%~dp0"

::echo.
::
::cd build\Release
::echo 生成 Release 版
::call cmake_cmd.bat ../.. -G "Visual Studio 15" -DCMAKE_BUILD_TYPE=Release -T v141
::cd /d "%~dp0"
::

顶级 CMakeLists.txt 需要设置编译器

set(CMAKE_C_COMPILER "gcc")
set(CMAKE_CXX_COMPILER "g++")

CodeBlocks 可以根据 cmake 的 target 再生成 project 来管理调试项目。在 Build targets 选项下有一个按钮 Create project from target...,生成项目后,可以保存 workspace,下次直接打开 workspace。

参考

发表评论

电子邮件地址不会被公开。 必填项已用*标注