Skip to content
Advertisement

Ruby, ffi, and a custom C library

I’m trying to create a custom C library I can call from Ruby using FFI. My library will wrap certain libtiff functions with my own code, and the resulting higher-level functions will be called from within Ruby via the FFI interface.

I’ve got test code working in C, that compiles successfully to a .so file. I’ve got that same code plumbed into Ruby via FFI, and can successfully call simple functions from ruby code.

However, when I include libtiff in my C library code and define a function that uses some of the libtiff routines, the ruby code throws errors similar to the following at runtime.

JavaScript

My guess is this has something to do with the way the compiler/linker is doing things. I’m very rusty with C… haven’t touched it in 15+ years, and I’m not sure exactly what to do to resolve the problem. I’m using, the Code::Blocks IDE, and have my code in a dynamic library project. As long as I don’t include libtiff, everything works as expected. Once I do… though the code compiles, I get symbol lookup errors for anything in libtiff.so.

Here’s my test library module code that generates the above error:

JavaScript

Here are the compiler commands

JavaScript

AddInt() called from ruby via FFI, works just fine. ConvertTIFF() does not. Any help is much appreciated.

UPDATE 1:

Here’s the ldd output of my custom library:

JavaScript

UPDATE 2:

Here’s the ldd output now that things are working:

JavaScript

Advertisement

Answer

The Problem

/usr/bin/ruby: symbol lookup error: /<path_to_lib>/libtiffconverter.so: undefined symbol: TIFFOpen

The function TIFFOpen() used by your ConvertTIFF() is not found. This function belongs to the LibTIFF library. You need to link your project against this library.

The Solution: Linking against LibTIFF

Add the option -ltiff to your g++ line above, i.e.:

JavaScript

You can check whether your just generated libtiffconverter.so actually depends on the LibTIFF library (it should) this way:

JavaScript

The following is an example output of the command above:

JavaScript

You may also need to add the -L option to the g++ line above followed by the directory where your libtiff.so is located. In order to obtain its location you can do the following:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement