New method of compiling glib library under Windows

Why is this article said to be a new method? Because there was a previous article that was compiled by modifying the meson.build file, but the last time I compiled the dll file, when I used it, it reported that the entry of the function could not be located. In the past two days, I carefully read the introduction on the official website, saying that msys2 can be used to compile under windows. Just follow the instructions on the official website and see if there will be any problems with the compiled dll this time.

The official website of MYSY2 is https://www.msys2.org/

The home page has Download the installer: msys2-x86_64-20210228.exe download address:   https://repo.msys2.org/distrib/x86_64/msys2-x86_64-20210228.exe

After downloading, the installation went smoothly. By default, it was installed in the C:\msys64 directory. After starting the application, you enter the console interface, and the next operation is the same as the Linux shell console.

After installing MSYS2, you need to install mingw, please refer to   the introduction on https://www.gtk.org/docs/installations/windows/

MSYS2 will come with a pacman tool, similar to yum in centOS and apt-get in Ubuntu.

Install the following basic tools:

pacman --needed --noconfirm -S meson git diffutils 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

After the installation is complete, unzip glib to the "/home/username/glib" directory in the installation directory, and execute the meson build command

If you encounter the following errors, please modify the meson.build file and change all if host_system =='windows' to if host_system =='windows' or host_system =='cygwin' or to if ['cygwin','windows '].contains(host_system)

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

 

Compile with ninja -C build, and an error occurred when compiling the zlib library.

[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                    此行注释掉

Then recompile it will pass.

ninja -C build install executes the installation

Installing subprojects/libffi/src/msys-ffi-7.dll       to /usr/local/bin
Installing subprojects/libffi/src/libffi.dll.a         to /usr/local/lib
Installing subprojects/zlib-1.2.11/msys-z.dll          to /usr/local/bin
Installing subprojects/zlib-1.2.11/libz.dll.a          to /usr/local/lib
Installing glib/msys-glib-2.0-0.dll                    to /usr/local/bin
Installing glib/libglib-2.0.dll.a                      to /usr/local/lib
Installing gobject/msys-gobject-2.0-0.dll              to /usr/local/bin
Installing gobject/libgobject-2.0.dll.a                to /usr/local/lib
Installing gthread/msys-gthread-2.0-0.dll              to /usr/local/bin
Installing gthread/libgthread-2.0.dll.a                to /usr/local/lib
Installing gmodule/msys-gmodule-2.0-0.dll              to /usr/local/bin
Installing gmodule/libgmodule-2.0.dll.a                to /usr/local/lib
Installing gio/msys-gio-2.0-0.dll                      to /usr/local/bin
Installing gio/libgio-2.0.dll.a                        to /usr/local/lib

Installing glib/gtester.exe                            to /usr/local/bin
Installing gobject/gobject-query.exe                   to /usr/local/bin
Installing gio/gio.exe                                 to /usr/local/bin
Installing gio/gresource.exe                           to /usr/local/bin
Installing gio/gio-querymodules.exe                    to /usr/local/bin
Installing gio/glib-compile-schemas.exe                to /usr/local/bin
Installing gio/glib-compile-resources.exe              to /usr/local/bin
Installing gio/gsettings.exe                           to /usr/local/bin
Installing gio/gdbus.exe                               to /usr/local/bin
Installing gio/gapplication.exe                        to /usr/local/bin

Installing gobject/glib-enumtypes.h                    to /usr/local/include/glib-2.0/gobject
Installing gio/gioenumtypes.h                          to /usr/local/include/glib-2.0/gio

My method was compiled and passed, but it did not happen when other windows-related implementations could not find the problem when I used it.

The following method is also available on the official website. However, due to proxy issues, the tools you rely on cannot be downloaded successfully. Wait for a different network environment and try again.

  • gvsbuild

    This method provides scripts to build the GTK stack from source and outputs libraries and tools that can be consumed by Visual Studio or Meson based projects.

We assume that you are using Windows 7 or later. For older versions of Windows, you will need to do a custom build of older versions of GLib and GTK.

Guess you like

Origin blog.csdn.net/langeldep/article/details/115034571