C/C++配置使用windows msys2中的gcc/g++编译器

介绍

比较常见的,可以在Windows上安装各种工具链来编译C和C ++应用程序,如果是专门为Windows开发的,那是推荐Microsoft 免费提供的Visual Studio Community,出色的IDE工具。

对于那些需要或喜欢跨平台GNU工具的人,可以使用MSYS2的gcc、make、cmake工具,接下来介绍如何使用MSYS2安装gcc。

在Windows中使用Code::Blocks - The IDE with all the features you need, having a consistent look, feel and operation across platforms.。

Qt Creator是mingw工具链附带的另一个免费IDE。One framework. One codebase. Any platform.

如果拥有一台Windows,并且只是想学习和使用GNU,建议通过Microsoft Store在Windows上安装Ubuntu或Suse。这将在Windows中提供一个Linux环境;或者可以在VirtualBox或VMWare Player虚拟机中安装像Ubuntu。

安装和使用msys2

从http://www.msys2.org/下载MSYS2 。下载.exe文件然后按照网站上的安装说明进行操作。安装后,导航到安装目录,然后运行msys2.exe。一般来说,默认位置为C:\ msys64。

MSYS2使用Arch Linux发行版使用的pacman软件包管理器。初次安装后,最好更新所有软件包。使用以下命令更新所有内容:

> pacman -Syu

如果更新太慢,可以配置msys2中pacman的镜像地址,三个文件mirrorlist.mingw32、mirrorlist.msys、mirrorlist.mingw64

# C:\msys64\etc\pacman.d\mirrorlist.mingw32
##
## 32-bit Mingw-w64 repository mirrorlist
##

## Primary
##中国科学技术大学开源软件镜像
Server = http://mirrors.ustc.edu.cn/msys2/mingw/i686
##北京理工大学镜像
Server = http://mirror.bit.edu.cn/msys2/mingw/i686
##日本北陆先端科学技术大学院大学 sourceforge 镜像
Server = http://jaist.dl.sourceforge.net/project/msys2/REPOS/MINGW/i686
##The UK Mirror Service Sourceforge mirror
Server = http://www.mirrorservice.org/sites/download.sourceforge.net/pub/sourceforge/m/ms/msys2/REPOS/MINGW/i686
## Primary
Server = ftp://148.251.42.38/MINGW/i686
## Sourceforge.net
Server = http://downloads.sourceforge.net/project/msys2/REPOS/MINGW/i686

## msys2.org
Server = http://repo.msys2.org/mingw/i686/
Server = https://sourceforge.net/projects/msys2/files/REPOS/MINGW/i686/
Server = http://www2.futureware.at/~nickoe/msys2-mirror/mingw/i686/
Server = https://mirror.yandex.ru/mirrors/msys2/mingw/i686/

# C:\msys64\etc\pacman.d\mirrorlist.msys
##
## MSYS2 repository mirrorlist
##

## Primary
##中国科学技术大学开源软件镜像
Server = http://mirrors.ustc.edu.cn/msys2/msys/$arch
##北京理工大学镜像
Server = http://mirror.bit.edu.cn/msys2/mingw/$arch
##日本北陆先端科学技术大学院大学 sourceforge 镜像
Server = http://jaist.dl.sourceforge.net/project/msys2/REPOS/MINGW/$arch
##The UK Mirror Service Sourceforge mirror
Server = http://www.mirrorservice.org/sites/download.sourceforge.net/pub/sourceforge/m/ms/msys2/REPOS/MSYS2/$arch
## Primary
Server = ftp://148.251.42.38/MSYS2/$arch
## Sourceforge.net
Server = http://downloads.sourceforge.net/project/msys2/REPOS/MSYS2/$arch

## msys2.org
Server = http://repo.msys2.org/msys/$arch/
Server = https://sourceforge.net/projects/msys2/files/REPOS/MSYS2/$arch/
Server = http://www2.futureware.at/~nickoe/msys2-mirror/msys/$arch/
Server = https://mirror.yandex.ru/mirrors/msys2/msys/$arch/
# C:\msys64\etc\pacman.d\mirrorlist.mingw64
##
## 64-bit Mingw-w64 repository mirrorlist
##

## Primary
##中国科学技术大学开源软件镜像
Server = http://mirrors.ustc.edu.cn/msys2/mingw/x86_64
##北京理工大学镜像
Server = http://mirror.bit.edu.cn/msys2/mingw/x86_64
##日本北陆先端科学技术大学院大学 sourceforge 镜像
Server = http://jaist.dl.sourceforge.net/project/msys2/REPOS/MINGW/x86_64
##The UK Mirror Service Sourceforge mirror
Server = http://www.mirrorservice.org/sites/download.sourceforge.net/pub/sourceforge/m/ms/msys2/REPOS/MINGW/x86_64
## Primary
Server = ftp://148.251.42.38/MINGW/x86_64
## Sourceforge.net
Server = http://downloads.sourceforge.net/project/msys2/REPOS/MINGW/x86_64

## msys2.org
Server = http://repo.msys2.org/mingw/x86_64/
Server = https://sourceforge.net/projects/msys2/files/REPOS/MINGW/x86_64/
Server = http://www2.futureware.at/~nickoe/msys2-mirror/mingw/x86_64/
Server = https://mirror.yandex.ru/mirrors/msys2/mingw/x86_64/

C/C++配置使用windows msys2中的gcc/g++编译器_第1张图片

使用MSYS2安装gcc

在MSYS2 bash shell中,再次使用pacman安装 build toolchain 和 compilers。运行以下命令以安装mingw-w64-x86_64-toolchain。

# Install make, autoconf, etc to C:\msys64\usr\bin
> pacman -S base-devel gcc vim cmake

C/C++配置使用windows msys2中的gcc/g++编译器_第2张图片

注意,这里直接加上了vim和cmake。安装完这些软件包之后,在MSYS2 shell中将可以使用gcc和其他工具。如果要在命令提示符下或在MSYS2 Shell之外使用它们,则可以在C:\ msys64 \ usr \ bin目录中找到它们。

> pacman -S base-devel gcc vim cmake
:: There are 56 members in group base-devel:
:: Repository msys
   1) asciidoc  2) autoconf  3) autoconf2.13  4) autogen  5) automake-wrapper
   6) automake1.10  7) automake1.11  8) automake1.12  9) automake1.13
   10) automake1.14  11) automake1.15  12) automake1.16  13) automake1.6
   14) automake1.7  15) automake1.8  16) automake1.9  17) bison  18) diffstat
   19) diffutils  20) dos2unix  21) file  22) flex  23) gawk  24) gdb  25) gettext
   26) gettext-devel  27) gperf  28) grep  29) groff  30) help2man  31) intltool
   32) lemon  33) libtool  34) libunrar  35) libunrar-devel  36) m4  37) make
   38) man-db  39) pacman  40) pactoys-git  41) patch  42) patchutils  43) perl
   44) pkg-config  45) pkgfile  46) quilt  47) rcs  48) scons  49) sed  50) swig
   51) texinfo  52) texinfo-tex  53) ttyrec  54) unrar  55) wget  56) xmlto

安装完成之后

> gcc
gcc: fatal error: no input files
compilation terminated.

> g++
g++: fatal error: no input files
compilation terminated.

> gdb
GNU gdb (GDB) 8.2.1
Copyright (C) 2018 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.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-pc-msys".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb)

将可执行文件添加到Windows PATH

此外,C:\msys64\usr\bin目录中还有许多其他exe,如果将bin目录添加到PATH时,注意看一下。

如果要访问Windows命令提示符中的所有内容,则将bin目录添加到Windows PATH环境变量中。请记住,这会将很多可执行文件添加到您的路径中,这可能会与其他应用程序发生冲突。

一般来说是这两个路径:

C:\msys64\mingw64\bin
C:\msys64\usr\bin

MSYS vs Mingw64

有些东西仅在msys/repo中可用,例如vim和git,并且仅在\usr\bin\中可用。诸如gcc之类的东西在msys \,mingw32\和mingw64\repos中可用,并且有可能安装在\usr\bin\和\mingw64\bin\中。

在这里,必须注意如何设置PATH环境变量。

如果已经添加\usr\bin\使用vim或git的话,再将\mingw64\bin\也添加到PATH中,则可能会与某些内容发生冲突。

如果只希望build toolchain和compiler,请直接使用mingw64软件包,仅将\mingw64\bin目录添加到PATH中。

另外,mingw64通常具有更多可用于安装的库,这些库在一般的msys存储库中不可用(例如SDL,exif,freeglut),具体可以看pacman的用法。

你可能感兴趣的:(C/C++配置使用windows msys2中的gcc/g++编译器)