内容纲要
一、环境:
- windows10 64位 + VSCODE
- 时间:2020年9月13日
- msys2-x86_64-20200903.exe
- GTK3
二、MSYS2环境搭建
1、安装MSYS2
下载地址:https://repo.msys2.org/distrib/x86_64/msys2-x86_64-20200903.exe
安装该软件时,使用默认选项,不修改文件夹位置。
安装最后一步勾选运行MSYS2


2、修改源信息
下一步需要同步数据库,同步前需要修改源信息,否则同步数据库会非常慢,后面的安装也无法进行下去。
找到C:\msys64\etc\pacman.d目录,

在该目录下有三个文件,修改为如下信息:
A、mirrorlist.mingw32
## ## 32-bit Mingw-w64 repository mirrorlist ## Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/i686/ ## Primary ## msys2.org Server = http://repo.msys2.org/mingw/i686/ Server = https://sourceforge.net/projects/msys2/files/REPOS/MINGW/i686/ Server = https://www2.futureware.at/~nickoe/msys2-mirror/mingw/i686/ Server = https://mirror.yandex.ru/mirrors/msys2/mingw/i686/ Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/i686/ Server = http://mirrors.ustc.edu.cn/msys2/mingw/i686/ Server = http://mirror.bit.edu.cn/msys2/mingw/i686/ Server = https://mirror.selfnet.de/msys2/mingw/i686/
B、mirrorlist.mingw64
## ## 64-bit Mingw-w64 repository mirrorlist ## Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/x86_64/ ## Primary ## msys2.org Server = http://repo.msys2.org/mingw/x86_64/ Server = https://sourceforge.net/projects/msys2/files/REPOS/MINGW/x86_64/ Server = https://www2.futureware.at/~nickoe/msys2-mirror/mingw/x86_64/ Server = https://mirror.yandex.ru/mirrors/msys2/mingw/x86_64/ Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/x86_64/ Server = http://mirrors.ustc.edu.cn/msys2/mingw/x86_64/ Server = http://mirror.bit.edu.cn/msys2/mingw/x86_64/ Server = https://mirror.selfnet.de/msys2/mingw/x86_64/
C、mirrorlist.msys
## ## MSYS2 repository mirrorlist ## Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/msys/$arch/ ## Primary ## msys2.org Server = http://repo.msys2.org/msys/$arch/ Server = https://sourceforge.net/projects/msys2/files/REPOS/MSYS2/$arch/ Server = https://www2.futureware.at/~nickoe/msys2-mirror/msys/$arch/ Server = https://mirror.yandex.ru/mirrors/msys2/msys/$arch/ Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/msys/$arch/ Server = http://mirrors.ustc.edu.cn/msys2/msys/$arch/ Server = http://mirror.bit.edu.cn/msys2/msys/$arch/ Server = https://mirror.selfnet.de/msys2/msys/$arch/
3、同步安装数据库
当出现完全同步时可认为同步成功
pacman -Syu

三、安装GTK+3及其依赖项
1、运行MSYS2软件
打开对应目前系统的MSYS2软件

2、输入命令安装GTK
pacman -S mingw-w64-x86_64-gtk3

3、安装Glade
pacman -S mingw-w64-x86_64-glade

4、环境测试
至此,GTK开发环境已经搭建完成了,我们来测试一下。
A、首先在home目录下新建test文件夹,用VSCODE打开,建立simple.c文件。复制GTK官网例程到simple内,

B、保存后在打开的MSYS2内执行命令,编译出可执行文件。
命令如下:
hi@DESKTOP-B25J5MG MINGW64 ~
$ cd test/ hi@DESKTOP-B25J5MG MINGW64 ~/test
$ gcc `pkg-config --cflags gtk+-3.0` -o simple simple.c `pkg-config --libs gtk+-3.0`

C、启动可执行文件
执行命令:
hi@DESKTOP-B25J5MG MINGW64 ~/test $ ./simple.exe

易错点总结:
1、使用了错误的MSYS2
在64位系统上如果安装的都是64位GTK,却用32位的MSYS2来编译,是会找不到GTK的。下面就是使用32位MSYS2的版本带来的错误提示。
hi@DESKTOP-B25J5MG MSYS ~ $ gcc `pkg-config --cflags gtk+-3.0` -o simple simple.c `pkg-config --libs gtk+-3.0` Package gtk+-3.0 was not found in the pkg-config search path. Perhaps you should add the directory containing `gtk+-3.0.pc' to the PKG_CONFIG_PATH environment variable No package 'gtk+-3.0' found Package gtk+-3.0 was not found in the pkg-config search path. Perhaps you should add the directory containing `gtk+-3.0.pc' to the PKG_CONFIG_PATH environment variable No package 'gtk+-3.0' found gcc: 错误:simple.c:No such file or directory gcc: 致命错误:没有输入文件 编译中断。