Native build of Gtk+ 3.0 with MS tools using prerequisites from OBS

Contents

About

This how-to is a result of a mailing list conversation http://mail.gnome.org/archives/gtk-list/2011-March/msg00111.html .

Rationale

Though cross-compilation have its benefits and employs well-established frameworks, sometimes it is necessary to use native builds.

  • Some may prefer MS Visual Studio IDE to develop/debug native part of GTK+ and/or other applications that can benefit from new features before official GTK(MM) release
  • Project may involve a bunch of MS specific code (like DirectShow)

  • C++ projects (like GTKMM) can't be portable between compiler suites as most likely name mangling is different such Gtk::Window::set_manage() would translate to __ZTv0_n32_N3Gtk6Window10set_manageEv for gcc vs ?set_manage@Window@Gtk@@$4PPPPPPPM@A@AEXXZ for MSVC++2008

Described approach doesn't require native building of all GTK+ dependencies.

Idea

For old plain C difference between naming is minor. In most cases it is just an underscore. Threfore it is possible to recover proper import libraries for MSVC++ and use them.

Unfortunately most of the OBS libraries & binaries are linked against msvcrt.dll instead of msvcrt90.dll or alike. Therefore end application should be also linked against msvcrt.dll. Otherwise simple calls like g_fopen() & fgets() will fail.

How to

What you'll need

Python 3 , download-mingw-rpm.py , a2lib.py, 7-Zip, MinGW (choose complete install for nm.exe, patch.exe, libmsvcrt.a), glibconfig_h.diff, MS VC++ 2008 Express (or alike), really optionally you may want mc-like FAR File Manager (even if you don't like mc, it makes copy/pasting to console and editing easier) ...

Download

Let's assume that C:\obs\ is our working directory. For now on all commands are executed from that directory (either in plain console or in FAR) if not stated otherwise. Download python scripts, diff, and brand new gtk+ into working directory and retrieve necessary packages. By pulling gtk3-devel we get all necessary prerequisites, though actual gtk3 from OBS is not used. If gtk3-demo.exe from OBS is working for you, you may skip building step:-) a2lib.py that prepare import libraries may need to be edited in case paths to MSVC, MinGW, etc. are different.

C:\obs>7z x gtk+-3.0.7.tar.bz2
C:\obs>7z x gtk+-3.0.7.tar

Copy C:\MinGW\lib\libmsvcrt.a to C:\obs\usr\i686-w64-mingw32\sys-root\mingw\lib\ as it will be converted to libmsvcrt.lib by a2lib.py.

C:\obs>C:\Python32\python.exe download-mingw-rpm.py gtk3-devel
C:\obs>C:\Python32\python.exe a2lib.py

You may want to change line 48 in main.c of gtk-demo project to point to correct examples directory from

result = g_strconcat (result, "\\share\\gtk-2.0\\demo", NULL);

to

result = g_strconcat (result, "\\share\\gtk-3.0\\demo", NULL);

glibconfig.h from OBS is not MSVC++-friendly. Gnome ftp though contain good one. If you don't have one at hand, you may apply a patch to C:/obs/usr/i686-w64-mingw32/sys-root/mingw/lib/glib-2.0/include/glibconfig.h with

C:\obs>C:\MinGW\msys\1.0\bin\patch.exe -p2 < glibconfig_h.diff 

If you are lucky to have MS VC++ 2008 (Express) proceed to the next subsection, otherwise skip it and read from #Using CMake.

Build in MS VC++ 2008 using IDE

Open C:\obs\gtk+-3.0.7\build\win32\vs9\gtk+.sln using MS VC++ 2008 Express IDE.

Quite a few properties needs to be changed.

  1. Go to Property Manager tab -> gdk -> Debug | Win32 -> gtk+props

  2. Go to User Macros and change GlibEtcInstallRoot to C:\obs\usr\i686-w64-mingw32\sys-root\mingw

  3. Remove msvc_recommended_pragmas.h (used when the entire Gtk+ stack is being built) from C/C++ Advanced -> Force Includes

  4. Add cairo-gobject.lib into Linker -> Input -> Additional Depencencies

  5. Right click on gtk+props -> Save gtk+props

Add libmsvcrt.lib (generate by a2lib.py from libmsvcrt.a) to Linker Input for gtk-demo project or any other end project.

Now it should be possible to build all Gtk+ 3.0 projects with F7.

Using CMake

There are template CMake files attached that can be used to generate project files for various MS VC++ versions as well as for old plain nmake.

Install recent CMake. Unpack cmake.7z such that CMakeLists.txt files appear in corresponding subfolders of gtk+-3.0.x . Run something like Start->All Programs->MS VC++->Command prompt. Type in commands from one the following subsubsections (You can use <TAB> for path completion).

Note that file naming is somewhat different from provided solution for MS VC 2008, but rather mimics the one from gtkmm2.4 bundle. Names (and versions) of dependencies are hard-coded in CMakeLists.txt files. While this works for now it would be nice to employ find_library mechanism.

Using old plain nmake

To get zip file with binaries, import libraries, header files, and demo

cd "\obs\gtk+-3.0.7"
mkdir vs100
cd vs100
"\Program Files\CMake 2.8\bin\cmake.exe" -D CMAKE_BUILD_TYPE=RELEASE ..
nmake package

Creating IDE solution and project files

cd "\obs\gtk+-3.0.7"
mkdir vs100
cd vs100
"\Program Files\CMake 2.8\bin\cmake.exe" -G "Visual Studio 10" ..

Open solution, adjust build type and press <F7> or use batch build.

Note

While you may start developing using Gtk+ 3.0 with MS VC++, some features may not work properly. Font change through settings probably won't work. This is caused by currently improper built dependencies (at least pango) @OBS. Fontconfig can successfully list installed TTF fonts, however pango-view attempts to communicate with X server instead of using Cairo or Win32 API backend.

Projects/GTK/Win32/NativeBuildWithOBS (last edited 2018-12-05 15:46:06 by EmmanueleBassi)