libuv之msys2环境下编译

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

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的差异。

PowerShell 7 环境已加载 (版本: 7.5.2) PowerShell 7 环境已加载 (版本: 7.5.2) PS C:\Users\Administrator\Desktop> <# >> Libuv 完整修复脚本 >> #> PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> $ErrorActionPreference = 'Stop' PS C:\Users\Administrator\Desktop> Set-StrictMode -Version 3 PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> try { >> $workingDir = "E:\PyTorch_Build\pytorch" >> $libuvBase = Join-Path $workingDir "third_party\libuv" >> $libDestDir = Join-Path $libuvBase "lib" >> >> # 1. 确保lib目录存在 >> New-Item -Path $libDestDir -ItemType Directory -Force -ErrorAction Stop | Out-Null >> >> # 2. 检查是否已有库文件 >> $existingLibs = Get-ChildItem -Path $libDestDir -Filter "uv*.lib" -File >> if ($existingLibs) { >> Write-Host "✅ 已找到库文件: $($existingLibs.FullName -join ', ')" -ForegroundColor Green >> $actualLibFile = $existingLibs[0].Name >> } >> else { >> Write-Host "未找到库文件,尝试从官方源下载..." -ForegroundColor Yellow >> >> # 3. 使用正确的下载URL >> $version = "1.51.0" >> $downloadUrl = "https://github.com/libuv/libuv/archive/refs/tags/v$version.zip" >> $tempZip = Join-Path $env:TEMP "libuv_source_$version.zip" >> >> # 下载源代码 >> Write-Host "正在下载Libuv源代码 (v$version)..." -ForegroundColor Cyan >> Invoke-WebRequest -Uri $downloadUrl -OutFile $tempZip -UseBasicParsing >> >> # 创建临时目录并解压 >> $tempExtract = Join-Path $env:TEMP "libuv_extract_$(Get-date -Format 'yyyyMMddHHmmss')" >> New-Item -ItemType Directory -Path $tempExtract -Force | Out-Null >> Expand-Archive -Path $tempZip -DestinationPath $tempExtract -Force >> >> # 查找并复制库文件(尝试构建目录) >> $buildDir = Join-Path $tempExtract "libuv-$version\out\build\x64-Release" >> $libFiles = Get-ChildItem -Path $buildDir -Recurse -Filter "uv*.lib" | >> Select-Object -First 1 >> >> if (-not $libFiles) { >> # 如果构建目录不存在,尝试手动构建 >> Write-Host "编译目录未找到,尝试构建Libuv..." -ForegroundColor Yellow >> $sourceDir = Join-Path $tempExtract "libuv-$version" >> $buildPath = Join-Path $sourceDir "build" >> New-Item -Path $buildPath -ItemType Directory -Force | Out-Null >> Set-Location $buildPath >> >> # 运行CMake生成构建文件 >> cmake .. -G "Visual Studio 17 2022" -A x64 >> cmake --build . --config Release >> >> $libFiles = Get-ChildItem -Path "$buildPath\Release" -Filter "uv*.lib" | >> Select-Object -First 1 >> } >> >> if ($libFiles) { >> Copy-Item -Path $libFiles.FullName -Destination $libDestDir -Force >> $actualLibFile = $libFiles.Name >> Write-Host "✅ 成功复制库文件: $actualLibFile" -ForegroundColor Green >> } >> else { >> throw "无法找到或生成库文件" >> } >> >> # 清理临时文件 >> Remove-Item $tempZip -Force -ErrorAction SilentlyContinue >> Remove-Item $tempExtract -Recurse -Force -ErrorAction SilentlyContinue >> } >> >> # 4. 更新CMake配置文件 >> $cmakeConfigPath = Join-Path $workingDir "cmake\Modules\FindLibuv.cmake" >> >> # 获取相对路径 >> $relativePath = $libuvBase.Replace($workingDir, "").TrimStart('\').Replace('\', '/') >> >> # 创建配置文件内容 >> $libuvConfig = @" >> # Libuv手动配置 >> set(LIBUV_FOUND ON) >> set(LIBUV_INCLUDE_DIRS "\${CMAKE_CURRENT_SOURCE_DIR}/$relativePath/include") >> set(LIBUV_LIBRARIES >> "\${CMAKE_CURRENT_SOURCE_DIR}/$relativePath/lib/$actualLibFile" >> ) >> "@ >> >> # 确保目录存在 >> $cmakeDir = Split-Path $cmakeConfigPath -Parent >> New-Item -Path $cmakeDir -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null >> >> Set-Content -Path $cmakeConfigPath -Value $libuvConfig -Encoding UTF8 >> Write-Host "✅ 更新CMake配置文件: $cmakeConfigPath" -ForegroundColor Green >> >> # 5. 最终验证 >> Write-Host "`n验证安装结果:" -ForegroundColor Cyan >> >> # 检查关键文件是否存在 >> $requiredFiles = @( >> Join-Path $libuvBase "include\uv.h", >> Join-Path $libDestDir $actualLibFile, >> $cmakeConfigPath >> ) >> >> $validationResult = $requiredFiles | ForEach-Object { >> if (Test-Path $_) { >> "✅ 存在: $_" >> } >> else { >> "❌ 缺失: $_" >> } >> } >> >> $validationResult | ForEach-Object { Write-Host $_ } >> >> if ($validationResult -contains "❌") { >> throw "部分文件缺失,请检查修复过程" >> } >> else { >> Write-Host "`n✅ Libuv 安装修复完成!" -ForegroundColor Green >> } >> } >> catch { >> Write-Host "`n❌ 修复失败: $_" -ForegroundColor Red >> Write-Host "错误详细信息: $($_.Exception.Message)" -ForegroundColor Yellow >> } ✅ 已找到库文件: E:\PyTorch_Build\pytorch\third_party\libuv\lib\uv.lib ❌ 修复失败: The variable '$CMAKE_CURRENT_SOURCE_DIR' cannot be retrieved because it has not been set. 错误详细信息: The variable '$CMAKE_CURRENT_SOURCE_DIR' cannot be retrieved because it has not been set. PS C:\Users\Administrator\Desktop> "PowerShell 7 环境已加载 (版本: 7.5.2) PowerShell 7 环境已加载 (版本: 7.5.2) PS C:\Users\Administrator\Desktop> # 切换到Libuv目录 PS C:\Users\Administrator\Desktop> Set-Location "E:\PyTorch_Build\pytorch\third_party\libuv" PS E:\PyTorch_Build\pytorch\third_party\libuv> PS E:\PyTorch_Build\pytorch\third_party\libuv> # 创建构建目录 PS E:\PyTorch_Build\pytorch\third_party\libuv> New-Item -Path "build" -ItemType Directory -Force Directory: E:\PyTorch_Build\pytorch\third_party\libuv Mode LastWriteTime Length Name ---- ------------- ------ ---- da--- 2025/9/1 0:27 build PS E:\PyTorch_Build\pytorch\third_party\libuv> Set-Location "build" PS E:\PyTorch_Build\pytorch\third_party\libuv\build> PS E:\PyTorch_Build\pytorch\third_party\libuv\build> # 运行CMake生成构建文件 PS E:\PyTorch_Build\pytorch\third_party\libuv\build> cmake .. -G "Visual Studio 17 2022" -A x64 -- summary of build options: Install prefix: C:/Program Files/libuv Target system: Windows Compiler: C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.44.35207/bin/Hostx64/x64/cl.exe (MSVC) CFLAGS: /DWIN32 /D_WINDOWS -- Configuring done (0.1s) -- Generating done (0.2s) -- Build files have been written to: E:/PyTorch_Build/pytorch/third_party/libuv/build PS E:\PyTorch_Build\pytorch\third_party\libuv\build> PS E:\PyTorch_Build\pytorch\third_party\libuv\build> # 构建库文件 PS E:\PyTorch_Build\pytorch\third_party\libuv\build> cmake --build . --config Release 适用于 .NET Framework MSBuild 版本 17.14.19+164abd434 1>Checking Build System Building Custom Rule E:/PyTorch_Build/pytorch/third_party/libuv/CMakeLists.txt uv.vcxproj -> E:\PyTorch_Build\pytorch\third_party\libuv\build\Release\uv.dll Building Custom Rule E:/PyTorch_Build/pytorch/third_party/libuv/CMakeLists.txt uv_a.vcxproj -> E:\PyTorch_Build\pytorch\third_party\libuv\build\Release\libuv.lib Building Custom Rule E:/PyTorch_Build/pytorch/third_party/libuv/CMakeLists.txt uv_run_benchmarks_a.vcxproj -> E:\PyTorch_Build\pytorch\third_party\libuv\build\Release\uv_run_benchmarks_a.exe Building Custom Rule E:/PyTorch_Build/pytorch/third_party/libuv/CMakeLists.txt uv_run_tests.vcxproj -> E:\PyTorch_Build\pytorch\third_party\libuv\build\Release\uv_run_tests.exe Building Custom Rule E:/PyTorch_Build/pytorch/third_party/libuv/CMakeLists.txt uv_run_tests_a.vcxproj -> E:\PyTorch_Build\pytorch\third_party\libuv\build\Release\uv_run_tests_a.exe Building Custom Rule E:/PyTorch_Build/pytorch/third_party/libuv/CMakeLists.txt PS E:\PyTorch_Build\pytorch\third_party\libuv\build> PS E:\PyTorch_Build\pytorch\third_party\libuv\build> # 复制生成的库文件 PS E:\PyTorch_Build\pytorch\third_party\libuv\build> Copy-Item "Release\uv.lib" "..\lib\" -Force PS E:\PyTorch_Build\pytorch\third_party\libuv\build> # 更新CMake配置文件 PS E:\PyTorch_Build\pytorch\third_party\libuv\build> $cmakeConfigPath = "E:\PyTorch_Build\pytorch\cmake\Modules\FindLibuv.cmake" PS E:\PyTorch_Build\pytorch\third_party\libuv\build> $newContent = @' >> set(LIBUV_FOUND ON) >> set(LIBUV_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/third_party/libuv/include") >> set(LIBUV_LIBRARIES >> "${CMAKE_CURRENT_SOURCE_DIR}/third_party/libuv/lib/uv.lib" >> ) >> '@ PS E:\PyTorch_Build\pytorch\third_party\libuv\build> PS E:\PyTorch_Build\pytorch\third_party\libuv\build> Set-Content -Path $cmakeConfigPath -Value $newContent -Encoding UTF8 PS E:\PyTorch_Build\pytorch\third_party\libuv\build> Write-Host "✅ CMake配置文件已更新" -ForegroundColor Green ✅ CMake配置文件已更新 PS E:\PyTorch_Build\pytorch\third_party\libuv\build> # 最终验证脚本 PS E:\PyTorch_Build\pytorch\third_party\libuv\build> $workingDir = "E:\PyTorch_Build\pytorch" PS E:\PyTorch_Build\pytorch\third_party\libuv\build> $libuvBase = Join-Path $workingDir "third_party\libuv" PS E:\PyTorch_Build\pytorch\third_party\libuv\build> PS E:\PyTorch_Build\pytorch\third_party\libuv\build> # 验证包含文件 PS E:\PyTorch_Build\pytorch\third_party\libuv\build> $headerStatus = if (Test-Path "$libuvBase\include\uv.h") {"✅ 存在"} else {"❌ 缺失"} PS E:\PyTorch_Build\pytorch\third_party\libuv\build> Write-Host "uv.h: $headerStatus" -ForegroundColor $(if ($headerStatus -eq "✅ 存在") {"Green"} else {"Red"}) uv.h: ✅ 存在 PS E:\PyTorch_Build\pytorch\third_party\libuv\build> PS E:\PyTorch_Build\pytorch\third_party\libuv\build> # 验证库文件 PS E:\PyTorch_Build\pytorch\third_party\libuv\build> $libFiles = Get-ChildItem -Path "$libuvBase\lib" -Filter "uv*.lib" -File PS E:\PyTorch_Build\pytorch\third_party\libuv\build> $libStatus = if ($libFiles) {"✅ 找到 $($libFiles.Count) 个库文件"} else {"❌ 缺失库文件"} PropertyNotFoundException: The property 'Count' cannot be found on this object. Verify that the property exists. PS E:\PyTorch_Build\pytorch\third_party\libuv\build> Write-Host "库文件: $libStatus" -ForegroundColor $(if ($libFiles) {"Green"} else {"Red"}) 库文件: ✅ 找到 个库文件 PS E:\PyTorch_Build\pytorch\third_party\libuv\build> if ($libFiles) { >> Write-Host "库文件名: $($libFiles.Name)" -ForegroundColor Cyan >> } 库文件名: uv.lib PS E:\PyTorch_Build\pytorch\third_party\libuv\build> PS E:\PyTorch_Build\pytorch\third_party\libuv\build> # 验证CMake配置 PS E:\PyTorch_Build\pytorch\third_party\libuv\build> $cmakeConfigPath = Join-Path $workingDir "cmake\Modules\FindLibuv.cmake" PS E:\PyTorch_Build\pytorch\third_party\libuv\build> $cmakeStatus = if (Test-Path $cmakeConfigPath) {"✅ 存在"} else {"❌ 缺失"} PS E:\PyTorch_Build\pytorch\third_party\libuv\build> Write-Host "CMake配置: $cmakeStatus" -ForegroundColor $(if ($cmakeStatus -eq "✅ 存在") {"Green"} else {"Red"}) CMake配置: ✅ 存在 PS E:\PyTorch_Build\pytorch\third_party\libuv\build> PS E:\PyTorch_Build\pytorch\third_party\libuv\build> # 显示配置内容 PS E:\PyTorch_Build\pytorch\third_party\libuv\build> if (Test-Path $cmakeConfigPath) { >> Write-Host "`nCMake配置内容:" -ForegroundColor Cyan >> Get-Content $cmakeConfigPath >> } CMake配置内容: set(LIBUV_FOUND ON) set(LIBUV_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/third_party/libuv/include") set(LIBUV_LIBRARIES "${CMAKE_CURRENT_SOURCE_DIR}/third_party/libuv/lib/uv.lib" ) PS E:\PyTorch_Build\pytorch\third_party\libuv\build> # 查看可用生成器 PS E:\PyTorch_Build\pytorch\third_party\libuv\build> cmake -G CMake Error: No generator specified for -G Generators * Visual Studio 17 2022 = Generates Visual Studio 2022 project files. Use -A option to specify architecture. Visual Studio 16 2019 = Generates Visual Studio 2019 project files. Use -A option to specify architecture. Visual Studio 15 2017 = Generates Visual Studio 2017 project files. Use -A option to specify architecture. Visual Studio 14 2015 = Generates Visual Studio 2015 project files. Use -A option to specify architecture. 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. Green Hills MULTI = Generates Green Hills MULTI files (experimental, work-in-progress). Unix Makefiles = Generates standard UNIX makefiles. Ninja = Generates build.ninja files. Ninja Multi-Config = Generates build-<Config>.ninja files. Watcom WMake = Generates Watcom WMake makefiles. CodeBlocks - MinGW Makefiles = Generates CodeBlocks project files (deprecated). CodeBlocks - NMake Makefiles = Generates CodeBlocks project files (deprecated). CodeBlocks - NMake Makefiles JOM = Generates CodeBlocks project files (deprecated). CodeBlocks - Ninja = Generates CodeBlocks project files (deprecated). CodeBlocks - Unix Makefiles = Generates CodeBlocks project files (deprecated). CodeLite - MinGW Makefiles = Generates CodeLite project files (deprecated). CodeLite - NMake Makefiles = Generates CodeLite project files (deprecated). CodeLite - Ninja = Generates CodeLite project files (deprecated). CodeLite - Unix Makefiles = Generates CodeLite project files (deprecated). Eclipse CDT4 - NMake Makefiles = Generates Eclipse CDT 4.0 project files (deprecated). Eclipse CDT4 - MinGW Makefiles = Generates Eclipse CDT 4.0 project files (deprecated). Eclipse CDT4 - Ninja = Generates Eclipse CDT 4.0 project files (deprecated). Eclipse CDT4 - Unix Makefiles= Generates Eclipse CDT 4.0 project files (deprecated). Kate - MinGW Makefiles = Generates Kate project files (deprecated). Kate - NMake Makefiles = Generates Kate project files (deprecated). Kate - Ninja = Generates Kate project files (deprecated). Kate - Ninja Multi-Config = Generates Kate project files (deprecated). Kate - Unix Makefiles = Generates Kate project files (deprecated). Sublime Text 2 - MinGW Makefiles = Generates Sublime Text 2 project files (deprecated). Sublime Text 2 - NMake Makefiles = Generates Sublime Text 2 project files (deprecated). Sublime Text 2 - Ninja = Generates Sublime Text 2 project files (deprecated). Sublime Text 2 - Unix Makefiles = Generates Sublime Text 2 project files (deprecated). PS E:\PyTorch_Build\pytorch\third_party\libuv\build> PS E:\PyTorch_Build\pytorch\third_party\libuv\build> # 使用正确的生成器,例如: PS E:\PyTorch_Build\pytorch\third_party\libuv\build> cmake .. -G "Visual Studio 16 2019" -A x64 CMake Error: Error: generator : Visual Studio 16 2019 Does not match the generator used previously: Visual Studio 17 2022 Either remove the CMakeCache.txt file and CMakeFiles directory or choose a different binary directory. PS E:\PyTorch_Build\pytorch\third_party\libuv\build> "
09-02
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JXES智能生态系统

如文章对你有用,请作者喝个咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值