Solving C library call incompatibilities with the dynamic linking loader

Posted by Emmanuel in software development on April 11, 2009 – 20:01
Post a comment

While testing the latest dynamically-linked binary version of SCAMP on various Linux platforms, I discovered that the executable would sometimes exit with the error message:

scamp: symbol lookup error: scamp: undefined symbol: c_plimage

This happens exclusively with older versions of the PLplot library, libplplotd, because of a change PLplot developers made to the API circa 2007. After this date, plimage() was replaced with c_plimage(). Although compatibility is maintained at compile time thanks to a

#define   plimage c_plimage

in the plplot.h include file, it is lost for dynamically-linked executables such as SCAMP. How then to make the program work with any version of the PLplot library? To my knowledge, there isn’t much that can be done at compile or link-time using the standard C library to solve this problem.

Fortunately, a POSIX-compliant solution exists. The dlopen(), dlclose(), and dlsym() functions allow respectively to open/close a dynamic library, and query symbol (function) pointers in it. The problem above can be solved by replacing calls to plimage() by (*myplimage)(), and setting the function pointer myplimage either to plimage or c_plimage depending on their availability. Here is a code example to replace a call to plimage(3.1416) (I simplified the arguments to plimage() for readability):

#include <dlfcn.h>

void (*myplimage)(double *dummy);
void *dl, *fptr;

dl = dlopen("", RTLD_LAZY);
if ((fptr = dlsym(dl, "plimage")))
  myplimage = fptr;
  myplimage =  dlsym(dl, "c_plimage");


RTLD_LAZY is one of the flags available to control binding rules; see the dlopen() man page for more details.

This entry was written by Emmanuel, filed under software development.
Bookmark the permalink or follow any comments here with the RSS feed for this post.
Post a comment or leave a trackback: Trackback URL.

Post a Comment

You must be logged in to post a comment.