Windows下编译glib库的新方法

  • 时间:
  • 浏览:
  • 来源:互联网

这篇文章为什么说是新方法呢 ? 因为之前有一篇文章是通过修改 meson.build文件编译通过的, 不过上次编译dll文件,我在使用的时候会报无法定位函数的入口。这两天仔细看官网的介绍,说在windows下可以使用msys2来编译,就照着官网的说明在来一次吧, 看看这次编译出来的dll还会不会有问题。

MYSY2的官网为 https://www.msys2.org/

首页有Download the installer: msys2-x86_64-20210228.exe 下载地址:   https://repo.msys2.org/distrib/x86_64/msys2-x86_64-20210228.exe

下载之后安装很顺利,默认安装到 了C:\msys64目录,启动应用后进入控制台界面, 下来的操作就和linux shell控制台一样了。

安装好MSYS2之后,需要安装mingw,具体参考 https://www.gtk.org/docs/installations/windows/  上面的介绍

MSYS2会自带一个pacman的工具, 类似centOS中的yum,Ubuntu中的apt-get。

安装以下的一些基础工具:

pacman --needed --noconfirm -S meson git automake autoconf make gcc libtool unzip
pacman --needed --noconfirm -S intltool pcre-devel base-devel mingw-w64-x86_64-toolchain mingw-w64-x86_64-pcre
pacman --needed --noconfirm -S mingw-w64-x86_64-gtk3
pacman --needed --noconfirm -S mingw-w64-x86_64-glade
pacman --needed --noconfirm -S mingw-w64-x86_64-python3-gobject

安装完毕后解压glib到安装目录中的 “/home/用户名/glib” 目录,执行  meson  build命令

如果碰到下面的错误,请修改 meson.build 文件,就改两行就行。

subprojects/libffi/src/meson.build:71:2: ERROR: Problem encountered: Unsupported pair: system "cygwin", cpu family "x86_64"

if host_cpu_family.startswith('x86')    此行改为 if  true
  arch_subdir = 'x86'
  if host_system == 'windows'  此行改为 if ['cygwin', 'windows'].contains(host_system) 
    if size_t == 4
      TARGET = 'X86_WIN32'
      if is_msvc_like
        c_sources = ['ffiold-msvc.c']
        asm_sources = ['win32_msvc.S']
      else
        c_sources = ['ffi.c']
        asm_sources = ['sysv.S']
      endif
    else
      TARGET = 'X86_WIN64'
      c_sources = ['ffiw64.c']
      if is_msvc_like
        asm_sources = ['win64_intel.S']
      else
        asm_sources = ['win64.S']
      endif
    endif
  elif ['darwin', 'ios', 'linux', 'android'].contains(host_system)
    if size_t == 4
      if ['darwin', 'ios'].contains(host_system)
        TARGET = 'X86_DARWIN'
      else
        TARGET = 'X86'
      # FIXME: TARGET_X32 support
      endif
      c_sources = ['ffi.c']
      asm_sources = ['sysv.S']
    else
      TARGET = 'X86_64'
      c_sources = ['ffi64.c', 'ffiw64.c']
      asm_sources = ['unix64.S', 'win64.S']
    endif
  endif
elif host_cpu_family == 'aarch64'
  arch_subdir = 'aarch64'
  TARGET = 'AARCH64'
  c_sources = ['ffi.c']
  if is_msvc_like
    asm_sources = ['win64_armasm.S']
  else
    asm_sources = ['sysv.S']
  endif
elif host_cpu_family == 'arm'
  arch_subdir = 'arm'
  TARGET = 'ARM'
  c_sources = ['ffi.c']
  asm_sources = ['sysv.S']
endif

if TARGET == ''
  error('Unsupported pair: system "@0@", cpu family "@1@"'.format(host_system, host_cpu_family))
endif

 

ninja -C build 进行编译,编译zlib库的时候出个错。

[41/1055] Linking target subprojects/zlib-1.2.11/msys-z.dll
FAILED: subprojects/zlib-1.2.11/msys-z.dll
cc  -o subprojects/zlib-1.2.11/msys-z.dll subprojects/zlib-1.2.11/msys-z.dll.p/adler32.c.o subprojects/zlib-1.2.11/msys-z.dll.p/crc32.c.o subprojects/zlib-1.2.11/msys-z.dll.p/deflate.c.o subprojects/zlib-1.2.11/msys-z.dll.p/infback.c.o subprojects/zlib-1.2.11/msys-z.dll.p/inffast.c.o subprojects/zlib-1.2.11/msys-z.dll.p/inflate.c.o subprojects/zlib-1.2.11/msys-z.dll.p/inftrees.c.o subprojects/zlib-1.2.11/msys-z.dll.p/trees.c.o subprojects/zlib-1.2.11/msys-z.dll.p/zutil.c.o subprojects/zlib-1.2.11/msys-z.dll.p/compress.c.o subprojects/zlib-1.2.11/msys-z.dll.p/uncompr.c.o subprojects/zlib-1.2.11/msys-z.dll.p/gzclose.c.o subprojects/zlib-1.2.11/msys-z.dll.p/gzlib.c.o subprojects/zlib-1.2.11/msys-z.dll.p/gzread.c.o subprojects/zlib-1.2.11/msys-z.dll.p/gzwrite.c.o -Wl,--as-needed -Wl,--allow-shlib-undefined -shared ../subprojects/zlib-1.2.11/win32/zlib.def -Wl,--start-group -Wl,--out-implib=subprojects/zlib-1.2.11/libz.dll.a -Wl,--version-script,/home/tengl/glib-2.64.2/subprojects/zlib-1.2.11/zlib.map -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -Wl,--end-group
/usr/lib/gcc/x86_64-pc-msys/10.2.0/../../../../x86_64-pc-msys/bin/ld: subprojects/zlib-1.2.11/msys-z.dll.p/gzlib.c.o: in function `gz_open':
/home/tengl/glib-2.64.2/build/../subprojects/zlib-1.2.11/gzlib.c:243: undefined reference to `_wopen'
collect2: error: ld returned 1 exit status
[58/1055] Compiling C object glib/msys-glib-2.0-0.dll.p/gkeyfile.c.o
ninja: build stopped: subcommand failed.
文件gzguts.h中
#if defined(_WIN32) || defined(__CYGWIN__)
//#  define WIDECHAR      注释掉此定义
#endif


文件gzlib.c中
/* -- see zlib.h -- */
//#ifdef WIDECHAR            此行注释掉
gzFile ZEXPORT gzopen_w(path, mode)
    const wchar_t *path;
    const char *mode;
{
    return gz_open(path, -2, mode);
}
//#endif                    此行注释掉

然后重新编译就能通过。

ninja -C build install 执行安装

 

本文链接http://www.dzjqx.cn/news/show-617094.html