1.MSYS2是MSYS的升级版
http://www.msys2.org/
MSYS2 is a software distro and building platform for Windows
At its core is an independent rewrite of MSYS, based on modern Cygwin (POSIX compatibility layer) and MinGW-w64 with the aim of better interoperability with native Windows software. It provides a bash shell, Autotools, revision control systems and the like for building native Windows applications using MinGW-w64 toolchains.
It features a package management system to provide easy installation of packages, Pacman. It brings many powerful features such as dependency resolution and simple complete system upgrades, as well as straight-forward package building.
2.安装注意
1)对硬盘分区要求,系统要求
Installation restrictions:
MSYS2 can't be installed on FAT* partitions.
Current MSYS2 can't be installed on Windows XP anymore.
2)支持32和64位
Download and run the installer - "x86_64" for 64-bit, "i686" for 32-bit Windows
3)安装目录命名要求
Enter Installation Folder (ASCII, no accents, spaces nor symlinks, short path)
3.Update the package database and core system packages with: 升级核心包
pacman -Syu
$ pacman -Syu
:: 正在同步软件包数据库...
mingw32 329.0 KiB 172K/s 00:02 [#####################] 100%
mingw32.sig 96.0 B 0.00B/s 00:00 [#####################] 100%
mingw64 328.8 KiB 22.3K/s 00:15 [#####################] 100%
mingw64.sig 96.0 B 0.00B/s 00:00 [#####################] 100%
msys 142.3 KiB 90.6K/s 00:02 [#####################] 100%
msys.sig 96.0 B 0.00B/s 00:00 [#####################] 100%
:: Starting core system upgrade...
警告:terminate other MSYS2 programs before proceeding
正在解决依赖关系...
正在查找软件包冲突...
软件包 (5) bash-4.4.012-1 filesystem-2017.02-4 mintty-1~2.7.3-1
msys2-runtime-2.7.0-1 pacman-5.0.1-2
下载大小: 10.38 MiB
全部安装大小: 50.37 MiB
净更新大小: -1.24 MiB
:: 进行安装吗? [Y/n] Y
:: 正在获取软件包......
msys2-runtime-2.7.0... 114.1 KiB 13.1K/s 02:57 [---------------------] 4%
Note:
1) msys2的bash shell有点类似putty,选中屏幕内容即为copy到clipboard
2)有时输入pacman -Syu更新到最后失败,这个一般与国内网络有关系,但msys2更新时不止一个源,所以实际可能通过另一个源已经更新成功,下面是更新时的Log:
libxml2-2.9.2-3-x86_64 160.3 KiB 169K/s 00:01 [#####################] 100%
错误:无法从 repo.msys2.org : Operation too slow. Less than 1 bytes/sec transfer //从repo.msys2.org源下载失败 red the last 10 seconds 获取文件 'bsdcpio-3.2.2-2-x86_64.pkg.tar.xz'
bsdcpio-3.2.2-2-x86_64 62.4 KiB 146K/s 00:00 [#####################] 100% //是尝试其他源下载成功的
错误:无法从 repo.msys2.org : Operation too slow. Less than 1 bytes/sec transfer red the last 10 seconds 获取文件 'bsdtar-3.2.2-2-x86_64.pkg.tar.xz'
bsdtar-3.2.2-2-x86_64 331.5 KiB 205K/s 00:02 [#####################] 100%
libcrypt-1.4-1-x86_64 16.1 KiB 0.00B/s 00:00 [#####################] 100%
msys2三个源配置文件如下(mingw64, mingw32和msys2):
Administrator@PC-20170308HYGG MSYS ~
$ cat /etc/pacman.d/mirrorlist.m*
##
## 32-bit Mingw-w64 repository mirrorlist
##
## Primary
## msys2.org
Server = http://repo.msys2.org/mingw/i686
Server = http://downloads.sourceforge.net/project/msys2/REPOS/MINGW/i686
Server = http://www2.futureware.at/~nickoe/msys2-mirror/i686/
##
## 64-bit Mingw-w64 repository mirrorlist
##
## Primary
## msys2.org
Server = http://repo.msys2.org/mingw/x86_64
Server = http://downloads.sourceforge.net/project/msys2/REPOS/MINGW/x86_64
Server = http://www2.futureware.at/~nickoe/msys2-mirror/x86_64/
##
## MSYS2 repository mirrorlist
##
## Primary
## msys2.org
Server = http://repo.msys2.org/msys/$arch
Server = http://downloads.sourceforge.net/project/msys2/REPOS/MSYS2/$arch
Server = http://www2.futureware.at/~nickoe/msys2-mirror/msys/$arch/
可以看到有三个源供选择,当然也可以换成国内源,比如中科大,只需要把链接加到这里面来就可以了,中科大对应三个分别如下:
Server = http://mirrors.ustc.edu.cn/msys2/mingw/i686/
Server = http://mirrors.ustc.edu.cn/msys2/mingw/x86_64/
Server = http://mirrors.ustc.edu.cn/msys2/msys/$arch/
所有下载的包都存放在D:\msys64\var\cache\pacman\pkg,所以如果某个包无法在线下载安装,可以事先从网络上下载下来,然后放到这个目录下,这样Update时就不会再去下载了,直接安装这个目录下面的。
这点类似Openwrt下面的dl目录。
4.If needed, close MSYS2, run it again from Start menu. Update the rest with:升级其他包
pacman -Su
执行完上面3update后,这一步执行提示没有事可做。
5.pacman软件管理工具
pacman -Q查看已安装的软件包
pacman -S -g查看软件组
pacman -Q -g base-devel查看软件组包含的软件
pacman -Q -l vim查询软件包的内容
pacman -Q -s nettle查询软件所在的包
安装软件包
pacman -S <package_names|package_groups>
example:pacman -S vim
搜索软件包
很多时候,我们不知道要按照的软件的准确名称,这时就要先查询软件包的名称。
pacman -Ss <name_pattern>
example:pacman -Ss vim
列出所有的软件组
pacman -Q --groups
附:
Pacman是Arch Linux 的包管理器。它将一个简单的二进制包格式和易用的构建系统结合了起来。不管软件包是来自官方的 Arch 库还是用户自己创建,Pacman 都能方便得管理。
更新系统
在 Archlinux 中,使用一条命令即可对整个系统进行更新:
pacman -Syu
如果你已经使用pacman -Sy将本地的包数据库与远程的仓库进行了同步,也可以只执行:pacman -Su
安装包
pacman -S 包名:例如,执行 pacman -S firefox 将安装 Firefox。你也可以同时安装多个包,
只需以空格分隔包名即可。
pacman -Sy 包名:与上面命令不同的是,该命令将在同步包数据库后再执行安装。
pacman -Sv 包名:在显示一些操作信息后执行安装。
pacman -U:安装本地包,其扩展名为 pkg.tar.gz。
pacman -U http://www.example.com/repo/example.pkg.tar.xz 安装一个远程包(不在 pacman 配置的源里面)
删除包
pacman -R 包名:该命令将只删除包,保留其全部已经安装的依赖关系
pacman -Rs 包名:在删除包的同时,删除其所有没有被其他已安装软件包使用的依赖关系
pacman -Rsc 包名:在删除包的同时,删除所有依赖这个软件包的程序
pacman -Rd 包名:在删除包时不检查依赖。
搜索包
pacman -Ss 关键字:在仓库中搜索含关键字的包。
pacman -Qs 关键字: 搜索已安装的包。
pacman -Qi 包名:查看有关包的详尽信息。
pacman -Ql 包名:列出该包的文件。
其他用法
pacman -Sw 包名:只下载包,不安装。
pacman -Sc:清理未安装的包文件,包文件位于 /var/cache/pacman/pkg/ 目录。
pacman -Scc:清理所有的缓存文件。
6.安装mingw64工具链 (通过安装软件组方式安装,即对应组下面会有许多关联软件包供选择安装)
pacman -S mingw-w64-x86_64-toolchain //64bits
pacman -S mingw-w64-i686-toolchain //32bits
其他
pacman -S base-devel
6.安装mingw64工具链 (通过安装软件组方式安装,即对应组下面会有许多关联软件包供选择安装)
pacman -S mingw-w64-x86_64-toolchain //64bits
pacman -S mingw-w64-i686-toolchain //32bits
其他
pacman -S base-devel
7.msys2 packages
https://github.com/Alexpux/MSYS2-packages
查看可用的安装包
pacman -Sl | grep gcc
可以看到以下三个,分别对应mingw32,mingw64与msys三个编译环境
mingw32 mingw-w64-i686-gcc 4.9.1-3
mingw64 mingw-64-x86_64-gcc 4.9.1-3
msys gcc 4.8.2-2
8.编译libuv
直接在msys2下编译是编不过的,主要原因是msys2对libuv支持不够,比如某些头文件找不到,库找不到之类的,所以编译libuv同样需要在Mingw环境下编译,所以上面我们已经安装好mingw32与64这两个不同版本,通过常安装mingw64就够了,它既可编译32位的,也编译64位的。
msys2有三个不同的shell,所以安装完Msys2后,会在开始菜单下面看到三个shell快捷方式,我们选MSYS2 MinGW 64-bit就可以(另外两个shell是MSYS2 MSYS和MSYS2 MinGW 32-bit):
$ sh autogen.sh
+ libtoolize --copy
libtoolize: putting auxiliary files in '.'.
libtoolize: copying file './ltmain.sh'
libtoolize: putting macros in AC_CONFIG_MACRO_DIRS, 'm4'.
libtoolize: copying file 'm4/libtool.m4'
libtoolize: copying file 'm4/ltoptions.m4'
libtoolize: copying file 'm4/ltsugar.m4'
libtoolize: copying file 'm4/ltversion.m4'
libtoolize: copying file 'm4/lt~obsolete.m4'
+ aclocal -I m4
+ autoconf
+ automake --add-missing --copy
configure.ac:37: installing './ar-lib'
configure.ac:25: installing './compile'
configure.ac:22: installing './config.guess'
configure.ac:22: installing './config.sub'
configure.ac:21: installing './install-sh'
configure.ac:21: installing './missing'
Makefile.am: installing './depcomp'
Administrator@PC-20170308HYGG MINGW64 ~/libuv-v1.10.2
$ ./configure
configure: loading site script /mingw64/etc/config.site
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking build system type... x86_64-w64-mingw32
checking host system type... x86_64-w64-mingw32
......
checking for socket in -lsocket... no
checking for special C compiler options needed for large files... no
checking for _FILE_OFFSET_BITS value needed for large files... 64
checking sys/ahafs_evProds.h usability... no
checking sys/ahafs_evProds.h presence... no
checking for sys/ahafs_evProds.h... no
checking for pkg-config... yes
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating libuv.pc
config.status: creating Makefile
config.status: executing depfiles commands
config.status: executing libtool commands
Administrator@PC-20170308HYGG MINGW64 ~/libuv-v1.10.2
$ make
CC src/libuv_la-fs-poll.lo
In file included from src/win/internal.h:29:0,
from src/fs-poll.c:247:
src/win/winapi.h:4148:6: warning: ISO C90 doesn't support unnamed structs/unions [-Wpedantic]
//注意:libuv automake产生的makefile中指定的C标准有点旧,可以在./configure后,手动修改下makefile,将gnu89用gnu11替换掉(gnu89与gnu90都属于ISO C90,而gnu11是ISO C11)
};
^
src/win/winapi.h:4156:4: warning: ISO C90 doesn't support unnamed structs/unions [-Wpedantic]
};
^
CC src/libuv_la-inet.lo
........
^
src/win/winapi.h:4156:4: warning: ISO C90 doesn't support unnamed structs/unions [-Wpedantic]
};
^
CCLD libuv.la
第一次编译会报下面宏未定义:
src/win/error.c: In function 'uv_translate_sys_error':
src/win/error.c:74:10: error: 'ERROR_ELEVATION_REQUIRED' undeclared (first use in this function)
case ERROR_ELEVATION_REQUIRED: return UV_EACCES; (这个ms的宏找找不到,应该是mingw64与libuv下ms的header file版本不一致,应该说libuv的比较新,ms网上找到case 0x800702E4: return UV_EACCES; // jack added)
^~~~~~~~~~~~~~~~~~~~~~~~
src/win/error.c:74:10: note: each undeclared identifier is reported only once for each function it appears in
make: *** [Makefile:1801:src/win/libuv_la-error.lo] 错误 1
如果输入mingw32-make编译,以下是编译libuv过程(这是为windows环境编译的):
$ sh autogen.sh //log与前面一样
Administrator@PC-20170308HYGG MINGW64 ~/libuv-v1.10.2
$ ./configure --prefix=/home/Administrator/libuv-v1.10.2/jack //这里用--prefix参数指定了安装目录,其他log都与前面类似
configure: loading site script /mingw64/etc/config.site
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking build system type... x86_64-w64-mingw32
checking host system type... x86_64-w64-mingw32
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.exe
Administrator@PC-20170308HYGG MINGW64 ~/libuv-v1.10.2
$ mingw32-make //编译结果都与上面一样
Administrator@PC-20170308HYGG MINGW64 ~/libuv-v1.10.2
$ make install
make[1]: Entering directory 'D:/msys64/home/Administrator/libuv-v1.10.2'
/usr/bin/mkdir -p '/home/Administrator/libuv-v1.10.2/jack/lib'
D:/msys64/usr/bin/sh.exe ./libtool --mode=install /usr/bin/install -c libuv.la '/home/Administrator/libuv-v1.10.2/jack/lib'
libtool: install: /usr/bin/install -c .libs/libuv.dll.a /home/Administrator/libuv-v1.10.2/jack/lib/libuv.dll.a
libtool: install: base_file=`basename libuv.la`
libtool: install: dlpath=`/bin/sh 2>&1 -c '. .libs/'libuv.la'i; echo libuv-1.dll'`
libtool: install: dldir=/home/Administrator/libuv-v1.10.2/jack/lib/`dirname ../bin/libuv-1.dll`
libtool: install: test -d /home/Administrator/libuv-v1.10.2/jack/lib/../bin || mkdir -p /home/Administrator/libuv-v1.10.2/jack/lib/../bin
libtool: install: /usr/bin/install -c .libs/libuv-1.dll /home/Administrator/libuv-v1.10.2/jack/lib/../bin/libuv-1.dll
libtool: install: chmod a+x /home/Administrator/libuv-v1.10.2/jack/lib/../bin/libuv-1.dll
libtool: install: if test -n '' && test -n 'strip --strip-unneeded'; then eval 'strip --strip-unneeded /home/Administrator/libuv-v1.10.2/jack/lib/../bin/libuv-1.dll' || exit 0; fi
libtool: install: /usr/bin/install -c .libs/libuv.lai /home/Administrator/libuv-v1.10.2/jack/lib/libuv.la
libtool: install: /usr/bin/install -c .libs/libuv.a /home/Administrator/libuv-v1.10.2/jack/lib/libuv.a
libtool: install: chmod 644 /home/Administrator/libuv-v1.10.2/jack/lib/libuv.a
libtool: install: ranlib /home/Administrator/libuv-v1.10.2/jack/lib/libuv.a
/usr/bin/mkdir -p '/home/Administrator/libuv-v1.10.2/jack/include'
/usr/bin/install -c -m 644 include/uv.h include/uv-errno.h include/uv-threadpool.h include/uv-version.h include/uv-win.h include/tree.h '/home/Administrator/libuv-v1.10.2/jack/include'
/usr/bin/mkdir -p '/home/Administrator/libuv-v1.10.2/jack/lib/pkgconfig'
/usr/bin/install -c -m 644 libuv.pc '/home/Administrator/libuv-v1.10.2/jack/lib/pkgconfig'
make[1]: Leaving directory 'D:/msys64/home/Administrator/libuv-v1.10.2'
// libuv.a --- linux的static lib
libuv.la --- libtool编译出来的库文件
libuv-1.dll --- windows 库
libuv.dll.a --- mingw使用的staic lib
.dll.a文件的最初用意其实是MinGW下的DLL文件的imp-lib,即与VC下DLL文件附带了一个引入库.lib类似。在VC下编程,当要使用DLL文件时,
在开发时必须要有.lib文件才能链接通过,.dll.a文件就是这样的作用。
总结:这两种编译方式(make或mingw32-make)都是一样的。
以上编译会遇到ISO C90对匿名结构体不支持的警告,我们可以在Make前先修改makefile,把gnu89改为gnu11 (C11标准)。
另外,libuv下有个Makefile.mingw,直接用此makefile编译:
make makefile.mingw //通过这方法编译没有ISO C90的警告,主要是makefile里没有开启这样的警告
另外编译exe应用程序时,需要很多windows的库:
-ladvapi32,
-liphlpapi,
-lpsapi,
-lshell32,
-luser32,
-luserenv,
-lws2_32
$ gcc -g s1.c -o s1 /home/Administrator/libuv-v1.10.2/libuv.a -I /home/Administrator/libuv-v1.10.2/jack/include/ -ladvapi32 -liphlpapi -lpsapi -luser32 -luserenv -lws2_32
遇到问题:无论用上面的方法,还是用这里的makefile.mingw编译出来的库,socket server端小程序都无法printf从客户端接收到的数据,客户端正常,解决方法如下:
1)服务器端打印客户端发来的数据代码如下
原先:
printf("%s\n", buf->base);
printf("%ld\n", nread);
此方法在debug方式编译,用gdb查看,发现有内容输出来,并且输出的内容显示也有点怪怪
修改后:
printf("%s\n", buf->base); //printf是输出到屏幕上,在Linux系统下,也就是stdout
printf("%ld\n", nread);
fflush(NULL); //NULL表示对所有打开的文件数据更新。fflush主要的功能是将缓冲区里的数据写回到相应的文件中去,所以这里也可只能stdout更新fflush(stdout)
或者用如下方式指定输出到stderr:
fprintf(stderr,"read:%s\n", buf->base);
fprintf(stderr,"read len:%ld(%ld)\n", nread, buf->len);
fflush(stderr);
2)为什么服务器端不能立即将缓存中的数据写回到文件,而客户端不存在这个问题,通过对服务器端debug,只要加入uv_listen这个API,就会出现无法输出问题。不仅是这一个libuv API会有问题,其他也会出现打印信息异常。
另外,这个问题在linux下面不存在,而我现在是在mingw64下遇到的,ming64是一个linux写的程序在windows上运行的builder.
所以现在推断,应该是mingw64编译出来的windows exe,没有把stdout都fflush,而linux系统就会fflush。当然,如果用mingw64编译一个简单的hello world输出,是没有问题的,
所以应该还涉及到事件循环,线程方面linux与windows的差异。

本文介绍了如何在MSYS2环境下编译libuv,包括MSYS2的安装注意事项、包管理器pacman的使用、mingw64工具链的安装,以及libuv的编译过程和遇到的问题。在编译libuv时,由于MSYS2对libuv的支持不足,需要在Mingw环境下进行。编译过程中可能会遇到ISO C90不支持匿名结构体的警告,可通过修改makefile将C标准更改为C11。最后,文章提到了在Windows环境下编译libuv的exe应用时,需要链接特定的Windows库,并解决printf无法输出客户端数据的问题。
2107

被折叠的 条评论
为什么被折叠?



