Blame README.win32

Packit ae235b
Tor Lillqvist <tml@iki.fi>
Packit ae235b
Hans Breuer <hans@breuer.org>
Packit ae235b

Packit ae235b
Note that this document is not really maintained in a serious
Packit ae235b
fashion. Lots of information here might be misleading or outdated. You
Packit ae235b
have been warned.
Packit ae235b

Packit ae235b
The general parts, and the section about gcc and autoconfiscated
Packit ae235b
build, and about a Visual Studio build are by Tor Lillqvist.
Packit ae235b

Packit ae235b
General
Packit ae235b
=======
Packit ae235b

Packit ae235b
For prebuilt binaries (DLLs and EXEs) and developer packages (headers,
Packit ae235b
import libraries) of GLib, Pango, GTK+ etc for Windows, go to
Packit ae235b
http://www.gtk.org/download-windows.html . They are for "native"
Packit ae235b
Windows meaning they use the Win32 API and Microsoft C runtime library
Packit ae235b
only. No POSIX (Unix) emulation layer like Cygwin in involved.
Packit ae235b

Packit ae235b
To build GLib on Win32, you can use either gcc ("mingw") or the
Packit ae235b
Microsoft compiler and tools. For the latter, MSVC6 and later have
Packit ae235b
been used successfully. Also the Digital Mars C/C++ compiler has
Packit ae235b
reportedly been used.
Packit ae235b

Packit ae235b
You can also cross-compile GLib for Windows from Linux using the
Packit ae235b
cross-compiling mingw packages for your distro.
Packit ae235b

Packit ae235b
Note that to just *use* GLib on Windows, there is no need to build it
Packit ae235b
yourself.
Packit ae235b

Packit ae235b
On Windows setting up a correct build environment can be quite a task,
Packit ae235b
especially if you are used to just type "./configure; make" on Linux,
Packit ae235b
and expect things to work as smoothly on Windows.
Packit ae235b

Packit ae235b
The following preprocessor macros are to be used for conditional
Packit ae235b
compilation related to Win32 in GLib-using code:
Packit ae235b

Packit ae235b
- G_OS_WIN32 is defined when compiling for native Win32, without
Packit ae235b
  any POSIX emulation, other than to the extent provided by the
Packit ae235b
  bundled Microsoft C library (msvcr*.dll).
Packit ae235b

Packit ae235b
- G_WITH_CYGWIN is defined if compiling for the Cygwin
Packit ae235b
  environment. Note that G_OS_WIN32 is *not* defined in that case, as
Packit ae235b
  Cygwin is supposed to behave like Unix. G_OS_UNIX *is* defined by a GLib
Packit ae235b
  for Cygwin.
Packit ae235b

Packit ae235b
- G_PLATFORM_WIN32 is defined when either G_OS_WIN32 or G_WITH_CYGWIN
Packit ae235b
  is defined.
Packit ae235b

Packit ae235b
These macros are defined in glibconfig.h, and are thus available in
Packit ae235b
all source files that include <glib.h>.
Packit ae235b

Packit ae235b
Additionally, there are the compiler-specific macros:
Packit ae235b
- __GNUC__ is defined when using gcc
Packit ae235b
- _MSC_VER is defined when using the Microsoft compiler
Packit ae235b
- __DMC__ is defined when using the Digital Mars C/C++ compiler
Packit ae235b

Packit ae235b
G_OS_WIN32 implies using the Microsoft C runtime, normally
Packit ae235b
msvcrt.dll. GLib is not known to work with the older crtdll.dll
Packit ae235b
runtime, or the static Microsoft C runtime libraries libc.lib and
Packit ae235b
libcmt.lib. It apparently does work with the debugging version of
Packit ae235b
msvcrt.dll, msvcrtd.dll. If compiled with Microsoft compilers newer
Packit ae235b
than MSVC6, it also works with their compiler-specific runtimes, like
Packit ae235b
msvcr70.dll or msvcr80.dll. Please note that it's non totally clear if
Packit ae235b
you would be allowed by the license to distrubute a GLib linked to
Packit ae235b
msvcr70.dll or msvcr80.dll, as those are not part of the operating
Packit ae235b
system, but of the MSVC product. msvcrt.dll is part of Windows.
Packit ae235b

Packit ae235b
For people using Visual Studio 2005 or later:
Packit ae235b

Packit ae235b
If you are building GLib-based libraries or applications, or GLib itself
Packit ae235b
and you see a C4819 error (or warning, before C4819 is treated as an error
Packit ae235b
in msvc_recommended_pragmas.h), please be advised that this error/warning should
Packit ae235b
not be disregarded, as this likely means portions of the build is not being
Packit ae235b
done correctly, as this is an issue of Visual Studio running on CJK (East Asian)
Packit ae235b
locales.  This is an issue that also affects builds of other projects, such as
Packit ae235b
QT, Firefox, LibreOffice/OpenOffice, Pango and GTK+, along with many other projects.
Packit ae235b

Packit ae235b
To overcome this problem, please set your system's locale setting for non-Unicode to
Packit ae235b
English (United States), reboot, and restart the build, and the code should build
Packit ae235b
normally.  See also this GNOME Wiki page [1] that gives a bit further info on this.
Packit ae235b

Packit ae235b
Building software that use GLib or GTK+
Packit ae235b
=======================================
Packit ae235b

Packit ae235b
Building software that just *uses* GLib or GTK+ also require to have
Packit ae235b
the right compiler set up the right way. If you intend to use gcc,
Packit ae235b
follow the relevant instructions below in that case, too.
Packit ae235b

Packit ae235b
Tor uses gcc with the -mms-bitfields flag which means that in order to
Packit ae235b
use the prebuilt DLLs (especially of GTK+), if you compile your code
Packit ae235b
with gcc, you *must* also use that flag. This flag means that the
Packit ae235b
struct layout rules are identical to those used by MSVC. This is
Packit ae235b
essential if the same DLLs are to be usable both from gcc- and
Packit ae235b
MSVC-compiled code. Such compatibility is desirable.
Packit ae235b

Packit ae235b
When using the prebuilt GLib DLLs that use msvcrt.dll from code that
Packit ae235b
uses other C runtimes like for example msvcr70.dll, one should note
Packit ae235b
that one cannot use such GLib API that take or returns file
Packit ae235b
descriptors. On Windows, a file descriptor (the small integer as
Packit ae235b
returned by open() and handled by related functions, and included in
Packit ae235b
the FILE struct) is an index into a table local to the C runtime
Packit ae235b
DLL. A file descriptor in one C runtime DLL does not have the same
Packit ae235b
meaning in another C runtime DLL.
Packit ae235b

Packit ae235b
Building GLib
Packit ae235b
=============
Packit ae235b

Packit ae235b
Again, first decide whether you really want to do this.
Packit ae235b

Packit ae235b
Before building GLib you must also have a GNU gettext-runtime
Packit ae235b
developer package. Get prebuilt binaries of gettext-runtime from
Packit ae235b
http://www.gtk.org/download-windows.html .
Packit ae235b

Packit ae235b
Autoconfiscated build (with gcc)
Packit ae235b
================================
Packit ae235b

Packit ae235b
Tor uses gcc 3.4.5 and the rest of the mingw utilities, including MSYS
Packit ae235b
from www.mingw.org. Somewhat earlier or later versions of gcc
Packit ae235b
presumably also work fine.
Packit ae235b

Packit ae235b
Using Cygwin's gcc with the -mno-cygwin switch is not recommended. In
Packit ae235b
theory it should work, but Tor hasn't tested that lately. It can
Packit ae235b
easily lead to confusing situations where one mixes headers for Cygwin
Packit ae235b
from /usr/include with the headers for native software one really
Packit ae235b
should use. Ditto for libraries.
Packit ae235b

Packit ae235b
If you want to use mingw's gcc, install gcc, win32api, binutils and
Packit ae235b
MSYS from www.mingw.org.
Packit ae235b

Packit ae235b
Tor invokes configure using:
Packit ae235b

Packit ae235b
CC='gcc -mtune=pentium3 -mthreads' CPPFLAGS='-I/opt/gnu/include' \
Packit ae235b
	LDFLAGS='-L/opt/gnu/lib -Wl,--enable-auto-image-base' CFLAGS=-O2 \
Packit ae235b
	./configure --disable-gtk-doc --prefix=$TARGET
Packit ae235b

Packit ae235b
The /opt/gnu mentioned contains the header files for GNU and (import)
Packit ae235b
libraries for GNU libintl. The build scripts used to produce the
Packit ae235b
prebuilt binaries are included in the "dev" packages.
Packit ae235b

Packit ae235b
Please note that the ./configure mechanism should not blindly be used
Packit ae235b
to build a GLib to be distributed to other developers because it
Packit ae235b
produces a compiler-dependent glibconfig.h. For instance, the typedef
Packit ae235b
for gint64 is long long with gcc, but __int64 with MSVC.
Packit ae235b

Packit ae235b
Except for this and a few other minor issues, there shouldn't be any
Packit ae235b
reason to distribute separate GLib headers and DLLs for gcc and MSVC6
Packit ae235b
users, as the compilers generate code that uses the same C runtime
Packit ae235b
library.
Packit ae235b

Packit ae235b
The DLL generated by either compiler is binary compatible with the
Packit ae235b
other one. Thus one either has to manually edit glibconfig.h
Packit ae235b
afterwards, or use the supplied glibconfig.h.win32 which has been
Packit ae235b
produced by running configure twice, once using gcc and once using
Packit ae235b
MSVC, and merging the resulting files with diff -D.
Packit ae235b

Packit ae235b
For MSVC7 and later (Visual C++ .NET 2003, Visual C++ 2005, Visual C++
Packit ae235b
2008 etc) it is preferred to use specific builds of GLib DLLs that use
Packit ae235b
the same C runtime as the code that uses GLib. Such DLLs should be
Packit ae235b
named differently than the ones that use msvcrt.dll.
Packit ae235b

Packit ae235b
For GLib, the DLL that uses msvcrt.dll is called libglib-2.0-0.dll,
Packit ae235b
and the import libraries libglib-2.0.dll.a and glib-2.0.lib. Note that
Packit ae235b
the "2.0" is part of the "basename" of the library, it is not
Packit ae235b
something that libtool has added. The -0 suffix is added by libtool
Packit ae235b
and is the value of "LT_CURRENT - LT_AGE". The 0 should *not* be
Packit ae235b
thought to be part of the version number of GLib. The LT_CURRENT -
Packit ae235b
LT_AGE value will on purpose be kept as zero as long as binary
Packit ae235b
compatibility is maintained. For the gory details, see configure.ac
Packit ae235b
and libtool documentation.
Packit ae235b

Packit ae235b
Building with Visual Studio
Packit ae235b
===========================
Packit ae235b

Packit ae235b
A more detailed outline of building GLib with its dependencies can
Packit ae235b
now be found on the GNOME wiki:
Packit ae235b

Packit ae235b
https://wiki.gnome.org/Projects/GTK%2B/Win32/MSVCCompilationOfGTKStack
Packit ae235b

Packit ae235b
Please do not build GLib in paths that contain spaces in them, as
Packit ae235b
this may cause problems during compilation and during usage of the
Packit ae235b
library.
Packit ae235b

Packit ae235b
In an unpacked tarball, you will find in build\win32\vs9 (VS 2008) and
Packit ae235b
build\win32\vs10 (VS 2010) a solution file that can be used to build
Packit ae235b
the GLib DLLs and some auxiliary programs under VS 2008 and VS 2010
Packit ae235b
(Express Edition will suffice with the needed dependencies) respectively.
Packit ae235b
Read the README.txt file in those folders for more
Packit ae235b
information. Note that you will need a libintl implementation, zlib, and 
Packit ae235b
libFFI.
Packit ae235b

Packit ae235b
If you are building from a GIT checkout, you will first need to use some
Packit ae235b
Unix-like environment or run win32/setup.py, 
Packit ae235b
which will expand the VS 2008/2010 project files, the DLL resouce files and
Packit ae235b
other miscellanious files required for the build.  Run win32/setup.py
Packit ae235b
as follows:
Packit ae235b

Packit ae235b
$python win32/setup.py --perl path_to_your_perl.exe
Packit ae235b

Packit ae235b
for more usage on this script, run
Packit ae235b
$python win32/setup.py -h/--help
Packit ae235b

Packit ae235b
[1]: https://wiki.gnome.org/Projects/GTK%2B/Win32/MSVCCompilationOfGTKStack under "Preparations"