[racket] How to compile C libraries that use Cairo?

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Sat Nov 16 16:16:40 EST 2013

Hi All,

I spent the day trying to get Asumu's bindings to librsvg
(a library that renders svg-files via Cairo) to run on OS X.

The problem seems to be the same that made me give
up getting libpoppler to work on OS X, so I was willing to
use some time on it.

The library works on Linux, but it crashes when I use it on OS X.

A stack trace from the (original) crash can be seen here (but see below first):
    https://github.com/takikawa/racket-rsvg/issues/2

Racket uses

  (define bitmap (make-bitmap width height))
  (define bitmap-handle (send bitmap get-handle))
  (rsvg_handle_render_cairo svg-handle (cairo_create bitmap-handle))

to create a cairo rendering context, and passes it along to librsvg.

Then librsvg call libcairo and a crash happens in cairo_get_matrix.

It took a while before I notices, but look at the address of cairo_get_matrix:
    0x0000000131691141
In the list of binary sections ones sees:

0x10e9c0000 - 0x10ea5cffa +libcairo.2.dylib (11003.2)
/Users/USER//libcairo.2.dylib
0x131682000 - 0x131731ff7 +libcairo.2.dylib (0) /opt/local/lib/libcairo.2.dylib

The conclusion is that librsvg is calling a libcairo different from
the Racket provided one.
Renaming /opt/local/libcairo.2.dylib confirms this hypothesis - after renaming
Racket can't load librsvg due to a missing dependency.

The library librsvg was installed with MacPorts which fetches the
source of librsvg
and the source of any of it depencies automatically, before it compiles.

My next attempt was to download the source of librsvg myself and use -L
to get it to use the Racket libraries. Some head scratching was caused
by the refusal of librsvg version 2.40 to compile. A comment in the
source revealed a /* Fixme portability */ comment where the C compiler
complained.
After fetching the sources for librsvg 2.36.4 I got a successful compile.
(I used the flags:   --disable-gtk-theme  --disable-gtk-doc
--disable-Bsymbolic  )

After copying the librsvg-2.2.dylib to the folder with Asumu's tests, I now get
this error:

Welcome to DrRacket, version 5.90.0.10--2013-11-10(53c6658e/d) [3m].
Language: racket/gui.
. . ffi-lib: couldn't open "librsvg-2.2.dylib"
(dlopen(librsvg-2.2.dylib, 6): Library not loaded:
/opt/local/lib/libcairo.2.dylib
  Referenced from: /usr/local/lib/librsvg-2.2.dylib
  Reason: Incompatible library version: librsvg-2.2.dylib requires
version 11203.0.0 or later, but libcairo.2.dylib provides version
11003.0.0)

This is progress. Racket has now loaded the correct version of librsvg
- and it even tries to use
the libcairo that Racket provides (version 1.10.3). Unfortunately the
include files I used for Cairo
were for 1.12.3 so I get the incompatible library version error.

At first I thought that were easy to fix - I just need to get the
include files for Cairo version 1.10.3
and recompile librsvg. But ... version 1.10.3 is not a release version
of Cairo. ( http://cairographics.org/releases/ )
And now I give up.

The question is thus: How can I use C libraries that uses Cairo in OS X ?

In particular I'd like to use libpoppler (and to a lesser degree librsvg).
Is it possible to include libpoppler in the native libraries
distributed with Racket?


--
Jens Axel Søgaard


Posted on the users mailing list.