Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile Cairo for Visual C++ 2008 (Express edition)

Most precompiled Windows binaries are made with the MSYS+gcc toolchain. It uses MSVCRT runtime, which is incompatible with Visual C++ 2005/2008.

So, how to go about and compile Cairo 1.6.4 (or later) for Visual C++ only. Including dependencies (png,zlib,pixman).

like image 725
akauppi Avatar asked Sep 17 '08 17:09

akauppi


1 Answers

These steps can build the latest cairo on 2015-11-15 for Visual Studio 2015 community. The debug build is DLL, linking to the DLL version of CRT. The release build is static library, linking to the static link version of CRT and requiring no DLLs.

Install GnuWin

The build scripts require GNU command line tools. The following steps are tested with GnuWin from Chocolatey. MSYS might also work.

Download

zlib128.zip, lpng1619.zip, cairo-1.14.4.tar.xz, pixman-0.32.8.tar.gz

Extract

Extract these archives and rename the directories:

. (my_cairo_build_root)
├─cairo
├─libpng
├─pixman
└─zlib

zlib

Do not build. The build script uses MSVCRT that clashes with Visual Studio 2015. Use the generated lib from libpng build.

libpng

Edit libpng\projects\vstudio\zlib.props:

  • in <ZLibSrcDir> remove the version number: ..\..\..\..\zlib
  • in <WindowsSDKDesktopARMSupport> change true to false

Open libpng\projects\vstudio\vstudio.sln in Visual Studio and confirm the upgrade. Use the default Debug configuration, and right click project libpng to build. Switch to Release Library configuration and right click project libpng to build.

pixman

Edit pixman\Makefile.win32.common:

  • Replace CFG_CFLAGS = -MD -O2 with CFG_CFLAGS = -MT -O2 (linking to the static link version of CRT in release build)
  • Replace @mkdir with @"mkdir" (there are cmd's builtin mkdir and GnuWin's mkdir, the quotes force the latter to be used)

Run Visual Studio x86 Native Command Prompt from start menu:

cd /d my_cairo_build_root
cd pixman\pixman
make -f Makefile.win32
make -f Makefile.win32 CFG=debug

cairo

Edit cairo\build\Makefile.win32.common:

  • Replace CFG_CFLAGS = -MD -O2 with CFG_CFLAGS = -MT -O2
  • Replace CAIRO_LIBS += $(LIBPNG_PATH)/libpng.lib with CAIRO_LIBS += $(LIBPNG_PATH)/lib/$(CFG)/libpng16.lib. Now, copy the directory libpng\projects\vstudio\Debug into (created) libpng\lib\ and rename it to debug. Copy the directory libpng\projects\vstudio\Release Library into libpng\lib\ and rename it to release.
  • Replace CAIRO_LIBS += $(ZLIB_PATH)/zdll.lib with CAIRO_LIBS += $(LIBPNG_PATH)/lib/$(CFG)/zlib.lib
  • There are two @mkdir -p $(CFG)/`dirname $<` lines. Replace both of them with:

    @"mkdir" -p $(CFG)/$<
    @"rmdir" $(CFG)/$<
    

Edit cairo\build\Makefile.win32.features-h:

  • Replace all @echo with @"echo"

There is an unusable link.exe in GnuWin. Rename C:\GnuWin\bin\link.exe to link_.exe to avoid clash.

Run Visual Studio x86 Native Command Prompt from start menu:

cd /d my_cairo_build_root
cd cairo
make -f Makefile.win32 CFG=debug
make -f Makefile.win32 CFG=release

The last two command will show "Built successfully!" but return error. Ignore them.

Rename back C:\GnuWin\bin\link.exe.

Configure Visual Studio

Create a directory include and copy the following headers in:

  • cairo\cairo-version.h (not cairo\src\cairo-version.h)
  • cairo\src\*.h, excluding cairo\src\cairo-version.h

Add that directory to include path in Visual Studio.

Add cairo\src\$(Configuration) and libpng\lib\$(Configuration) to library path. $(Configuration) will automatically expand to Debug or Release when building.

Put cairo\src\debug\cairo.dll and libpng\lib\debug\libpng16.dll to one of Windows' PATH.

Before #include <cairo.h>, setup the link options:

#ifndef NDEBUG
#   pragma comment(lib, "cairo")
#else
#define CAIRO_WIN32_STATIC_BUILD
#   pragma comment(lib, "cairo-static")
#   pragma comment(lib, "libpng16")
#   pragma comment(lib, "zlib")
#endif
like image 81
jingyu9575 Avatar answered Sep 19 '22 20:09

jingyu9575