Merge branch 'g-clear-pointer-no-side-effects' into 'master'
[glib.git] / README.win32
blob1683d54b9683f232c586dd3109b0f7d2efa0145e
1 Tor Lillqvist <tml@iki.fi>
2 Hans Breuer <hans@breuer.org>
4 Note that this document is not really maintained in a serious
5 fashion. Lots of information here might be misleading or outdated. You
6 have been warned.
8 The general parts, and the section about gcc and autoconfiscated
9 build, and about a Visual Studio build are by Tor Lillqvist.
11 General
12 =======
14 For prebuilt binaries (DLLs and EXEs) and developer packages (headers,
15 import libraries) of GLib, Pango, GTK+ etc for Windows, go to
16 http://www.gtk.org/download-windows.html . They are for "native"
17 Windows meaning they use the Win32 API and Microsoft C runtime library
18 only. No POSIX (Unix) emulation layer like Cygwin in involved.
20 To build GLib on Win32, you can use either gcc ("mingw") or the
21 Microsoft compiler and tools. For the latter, MSVC6 and later have
22 been used successfully. Also the Digital Mars C/C++ compiler has
23 reportedly been used.
25 You can also cross-compile GLib for Windows from Linux using the
26 cross-compiling mingw packages for your distro.
28 Note that to just *use* GLib on Windows, there is no need to build it
29 yourself.
31 On Windows setting up a correct build environment can be quite a task,
32 especially if you are used to just type "./configure; make" on Linux,
33 and expect things to work as smoothly on Windows.
35 The following preprocessor macros are to be used for conditional
36 compilation related to Win32 in GLib-using code:
38 - G_OS_WIN32 is defined when compiling for native Win32, without
39   any POSIX emulation, other than to the extent provided by the
40   bundled Microsoft C library (msvcr*.dll).
42 - G_WITH_CYGWIN is defined if compiling for the Cygwin
43   environment. Note that G_OS_WIN32 is *not* defined in that case, as
44   Cygwin is supposed to behave like Unix. G_OS_UNIX *is* defined by a GLib
45   for Cygwin.
47 - G_PLATFORM_WIN32 is defined when either G_OS_WIN32 or G_WITH_CYGWIN
48   is defined.
50 These macros are defined in glibconfig.h, and are thus available in
51 all source files that include <glib.h>.
53 Additionally, there are the compiler-specific macros:
54 - __GNUC__ is defined when using gcc
55 - _MSC_VER is defined when using the Microsoft compiler
56 - __DMC__ is defined when using the Digital Mars C/C++ compiler
58 G_OS_WIN32 implies using the Microsoft C runtime, normally
59 msvcrt.dll. GLib is not known to work with the older crtdll.dll
60 runtime, or the static Microsoft C runtime libraries libc.lib and
61 libcmt.lib. It apparently does work with the debugging version of
62 msvcrt.dll, msvcrtd.dll. If compiled with Microsoft compilers newer
63 than MSVC6, it also works with their compiler-specific runtimes, like
64 msvcr70.dll or msvcr80.dll. Please note that it's non totally clear if
65 you would be allowed by the license to distrubute a GLib linked to
66 msvcr70.dll or msvcr80.dll, as those are not part of the operating
67 system, but of the MSVC product. msvcrt.dll is part of Windows.
69 For people using Visual Studio 2005 or later:
71 If you are building GLib-based libraries or applications, or GLib itself
72 and you see a C4819 error (or warning, before C4819 is treated as an error
73 in msvc_recommended_pragmas.h), please be advised that this error/warning should
74 not be disregarded, as this likely means portions of the build is not being
75 done correctly, as this is an issue of Visual Studio running on CJK (East Asian)
76 locales.  This is an issue that also affects builds of other projects, such as
77 QT, Firefox, LibreOffice/OpenOffice, Pango and GTK+, along with many other projects.
79 To overcome this problem, please set your system's locale setting for non-Unicode to
80 English (United States), reboot, and restart the build, and the code should build
81 normally.  See also this GNOME Wiki page [1] that gives a bit further info on this.
83 In Visual Studio 2015 and later, the /utf-8 option is provided, which is set by the
84 latest Meson releases when building GLib, and can be used in other project files
85 that uses GLib to avoid the need of setting your system's locale setting for
86 non-Unicode and the subsequent requirement to restart the system.
88 Building software that use GLib or GTK+
89 =======================================
91 Building software that just *uses* GLib or GTK+ also require to have
92 the right compiler set up the right way. If you intend to use gcc,
93 follow the relevant instructions below in that case, too.
95 Tor uses gcc with the -mms-bitfields flag which means that in order to
96 use the prebuilt DLLs (especially of GTK+), if you compile your code
97 with gcc, you *must* also use that flag. This flag means that the
98 struct layout rules are identical to those used by MSVC. This is
99 essential if the same DLLs are to be usable both from gcc- and
100 MSVC-compiled code. Such compatibility is desirable.
102 When using the prebuilt GLib DLLs that use msvcrt.dll from code that
103 uses other C runtimes like for example msvcr70.dll, one should note
104 that one cannot use such GLib API that take or returns file
105 descriptors. On Windows, a file descriptor (the small integer as
106 returned by open() and handled by related functions, and included in
107 the FILE struct) is an index into a table local to the C runtime
108 DLL. A file descriptor in one C runtime DLL does not have the same
109 meaning in another C runtime DLL.
111 Building GLib
112 =============
114 Again, first decide whether you really want to do this.
116 Before building GLib you must also have a GNU gettext-runtime
117 developer package. Get prebuilt binaries of gettext-runtime from
118 http://www.gtk.org/download-windows.html .
120 Autoconfiscated build (with gcc)
121 ================================
123 Tor uses gcc 3.4.5 and the rest of the mingw utilities, including MSYS
124 from www.mingw.org. Somewhat earlier or later versions of gcc
125 presumably also work fine.
127 Using Cygwin's gcc with the -mno-cygwin switch is not recommended. In
128 theory it should work, but Tor hasn't tested that lately. It can
129 easily lead to confusing situations where one mixes headers for Cygwin
130 from /usr/include with the headers for native software one really
131 should use. Ditto for libraries.
133 If you want to use mingw's gcc, install gcc, win32api, binutils and
134 MSYS from www.mingw.org.
136 Tor invokes configure using:
138 CC='gcc -mtune=pentium3 -mthreads' CPPFLAGS='-I/opt/gnu/include' \
139         LDFLAGS='-L/opt/gnu/lib -Wl,--enable-auto-image-base' CFLAGS=-O2 \
140         ./configure --disable-gtk-doc --prefix=$TARGET
142 The /opt/gnu mentioned contains the header files for GNU and (import)
143 libraries for GNU libintl. The build scripts used to produce the
144 prebuilt binaries are included in the "dev" packages.
146 Please note that the ./configure mechanism should not blindly be used
147 to build a GLib to be distributed to other developers because it
148 produces a compiler-dependent glibconfig.h. For instance, the typedef
149 for gint64 is long long with gcc, but __int64 with MSVC.
151 Except for this and a few other minor issues, there shouldn't be any
152 reason to distribute separate GLib headers and DLLs for gcc and MSVC6
153 users, as the compilers generate code that uses the same C runtime
154 library.
156 The DLL generated by either compiler is binary compatible with the
157 other one. Thus one either has to manually edit glibconfig.h
158 afterwards.
160 For MSVC7 and later (Visual C++ .NET 2003, Visual C++ 2005, Visual C++
161 2008 etc) it is preferred to use specific builds of GLib DLLs that use
162 the same C runtime as the code that uses GLib.
164 For GLib, the DLL that uses msvcrt.dll is called libglib-2.0-0.dll,
165 and the import libraries libglib-2.0.dll.a and glib-2.0.lib. Note that
166 the "2.0" is part of the "basename" of the library, it is not
167 something that libtool has added. The -0 suffix is added by libtool
168 and is the value of "LT_CURRENT - LT_AGE". The 0 should *not* be
169 thought to be part of the version number of GLib. The LT_CURRENT -
170 LT_AGE value will on purpose be kept as zero as long as binary
171 compatibility is maintained. For the gory details, see configure.ac
172 and libtool documentation.
174 Building with Visual Studio
175 ===========================
177 Meson is now the supported method of building GLib using Visual Studio.
179 Note that you will need a libintl implementation, zlib, and libFFI, and
180 optionally PCRE1, which should preferably be built with the same compiler
181 that is now being used to build GLib.  Ensure that their headers, .lib's
182 and DLLs can be found in the paths specified by the INCLUDE, LIB and PATH
183 envvars.  The Meson build process will pull in a copy of the ZLib and the
184 libFFI sources if they cannot be found, and will build an in-source copy
185 of PCRE1 if PCRE1 cannt be found.
187 One can also refer to the following page for building the dependencies:
189 https://wiki.gnome.org/Projects/GTK%2B/Win32/MSVCCompilationOfGTKStack
191 You will also need the following items:
192 -Python 3.6.x, you need the 32-bit version if you are building GLib
193  as a 32-bit/x86 build, or the amd64/x64 version for building 64-bit/x86-64
194  builds.  You will then need to install or update Meson by using pip.
195 -The Ninja build tool, required for Visual Studio 2008, 2012 and 2013 builds,
196  and optional for 2010, 2015 and 2017 builds, where Visual Studio projects
197  can be generated instead of the Ninja build files.
198 -GIT for Windows is highly recommended, in the case where some required
199  dependencies are not found, and Meson makes use of GIT to download
200  the sources to build in the build process.
202 To do a build using Meson, do the following:
204 -Open a Visual Studio (or SDK) command prompt that matches the Visual Studio
205  version and build platform (Win32/x86, x64, etc.) that will be used in all
206  the following steps.
208 -Create an empty directory/folder for the build.  It needs to be in the same
209  drive as where your GLib sources are located (i.e. $(GLIB_SRCDIR)).  cd into
210  that directory/folder.
212 -Setup your PATH envvar:
214    set PATH=%PATH%;$(PYTHON_INSTALL_DIR);$(NINJA_DIR)
216  where PYTHON_INSTALL_DIR is where Python 3.6.x+ is installed to, and NINJA_DIR
217  is where your ninja executable can be found.  The NINJA_DIR can be omitted if one
218  passes --backend=vs to the Meson configuration line, for Visual Studio 2010, 2015
219  and 2017 builds.
221 -Configure the build using Meson:
223     python $(PYTHON_INSTALL_DIR)\scripts\meson.py $(GLIB_SRCDIR) --buildtype=$(build_configuration) --prefix=$(INSTALL_PREFIX) [--backend=vs]
225  Please see the Meson docs for an explanation for --buildtype, the path passed for
226  --prefix need not to be on the same drive as where the build is carried out, but
227  it is recommended to use forward slashes for this path.  The --backend=vs can be
228  used if the Visual Studio project generator is preferred over using Ninja, for
229  Visual Studio 2010, 2015 and 2017 builds.
231 -Build, test and install the build:
232  Run ninja (and ninja test and ninja install) or open the generated Visual Studio
233  projects to compile, test and install the build.
235 Note that if building the sources with Visual Studio 2008, note the following
236 additional items:
238 -You need to run the following lines from your build directory, to embed the manifests
239  that are generated during the build, assuming the built binaries are installed
240  to $(PREFIX), after a successful build/installation:
242 for /r %f in (*.dll.manifest) do if exist $(PREFIX)\bin\%~nf mt /manifest %f $(PREFIX)\bin\%~nf;2
243 for /r %f in (*.exe.manifest) do if exist $(PREFIX)\bin\%~nf mt /manifest %f $(PREFIX)\bin\%~nf;1
245 -If building for amd64/x86_64/x64, sometimes the compilation of sources may seem to hang, which
246  is caused by an optimization issue in the 2008 x64 compiler.  You need to use Task Manager to
247  remove all running instances of cl.exe, which will cause the build process to terminate.  Update
248  the build flags of the sources that hang on compilation by changing its "/O2" flag to "/O1"
249  in build.ninja, and  retry the build, where things should continue to build normally.  At the
250  time of writing, this is needed for compiling glib/gtestutils.c,  gio/gsettings.c and
251  gio/gsettingsschema.c