Coyote's Guide to IDL Programming

Calling C Programs from IDL

QUESTION: It's obvious you don't know squat about calling C programs from IDL. Who does?

ANSWER: Well, I know a little bit about it. But you're right, it's not much. :-)

But fortunately there are many, many people who know quite a lot about calling C (or FORTRAN, or any number of other languages) from IDL. Many of these people hang out on the IDL newsgroup, comp.lang.idl-pvwave. I always have good luck asking a question there.

Another excellent source of information (one might say the definitive source) is Ronn Kling's book Calling C from IDL: Using DLM's to Extend Your IDL Code. Ronn has put a ton of example code, explanation, and hard-earned expertise together is this definitive guide. You can order the book directly from Ronn. And be sure to visit his web page for other terrific software and information about IDL.

Here are some tips about calling external programs with Call_External that I have gathered from Mark Rivers, one of the experts on the IDL newsgroup. His notes have been lightly edited by me.

From: Mark Rivers (
Subject: Re: Problems with IDL call_external to C shared object
Newsgroups: comp.lang.idl-pvwave
Date: 12 OCT 2000

Here are some general tips for using Call_External.

Question: What is the effect of the /CDECL keyword to Call_External?

Answer: This controls the calling convention. If your C function is being called then you probably have this set correctly.

Question: Is it possible that the C program "forgets" something between the IDL Call_External calls?

Answer: Yes, it will forget anything which is not global or static.

Question: How can I return an array via Call_External or have I always to loop over calls returning scalars ?

Answer: Here is a simple example. It is C code which computes the Mandelbrot set, and is called from IDL. The argv[7] argument is a 2-D array.

   void mandelbrot(int argc, void *argv[])
    int nr = *(int *) argv[0];
    int ni = *(int *) argv[1];
    double rstart = *(double *) argv[2];
    double istart = *(double *) argv[3];
    double dr = *(double *) argv[4];
    double di = *(double *) argv[5];
    int max_iter = *(int *) argv[6];
    int *result =  argv[7];
   int i, j, count;
    double real, imag, rz, iz, sz2, rz2, iz2;
       for (i=0; i<ni; i++) {
         imag = istart + i*di;
         for (j=0; j<nr; j++) {
              real = rstart + j*dr;
              rz = 0.;
              iz = 0.;
              sz2 = 0.;

              count = 0;
              while ((count < max_iter) && (sz2 < 4.0)) {
                   rz2 = rz * rz;
                   iz2 = iz * iz;
                   iz = 2.0 * rz * iz + imag;
                   rz = rz2 - iz2 + real;
                   sz2 = rz2 + iz2;
              *result++ = count;

Here is the IDL code which calls the C code:

   function mandelbrot1, xcenter, ycenter, radius, size, max_iter, xout, yout
   if (n_elements(size) eq 0) then size=100
   if (n_elements(max_iter) eq 0) then max_iter=255
   dx = double(radius)*2/size
   xstart = double(xcenter - radius)
   xstop = double(xcenter + radius)
   ystart = double(ycenter - radius)
   ystop = double(ycenter + radius)
   result = lonarr(size, size)
   xout = xstart + findgen(size)*dx
   yout = ystart + findgen(size)*dx
   s = call_external('mandelbrot.dll', 'mandelbrot', $
                      long(size), $
                      long(size), $
                      double(xstart), $
                      double(ystart), $
                      double(dx), $
                      double(dx), $
                      long(max_iter), $
   return, result

Question: Can I pass IDL structures to my C program using Call_External?

Answer: [From 4 APRIL 1996] The last time I looked, the means by which structures are passed was intentionally not documented, presumably so that RSI would be free to change it in the future.

However, I know by experience that IDL presently passes structures just like you would expect (i.e. it passes the address of the start of the structure). All structure elements except strings are contained in the structure itself (i.e. the structure contains the value, not a pointer). Strings are different: the structure contains either the descriptor or the address of the descriptor (I forget).

I routinely pass structures to Call_External, but I do so at my own risk, since it is not guaranteed to be done the same way in future versions of IDL.

I have found that the structures will contain padding to keep the members aligned on natural boundaries. The C compiler will normally do this on the structures in your Call_External code as well, so it has not been a problem.

Web Coyote's Guide to IDL Programming