Calling C Code from IDL Modules

David Fanning has some excellent advice and useful info about calling C from IDL.


(From David Fanning):

  • All arrays which need to be passed between IDL and C must be allocated in IDL. This includes both arrays being passed from IDL to C and from C back to IDL. Sometimes this requires an initial call to the C code to return the array sizes which IDL will allocate, if the array sizes are not known to IDL beforehand.
  • Don't deallocate any arrays which were passed from IDL.
  • Don't pass strings, rather pass byte arrays. It is much simpler. Convert strings to byte arrays in IDL before or after the Call_External call.
  • Convert all output variables to the data type which C is expecting in the Call_External call.



See the attached files mandelbrot.c and for examples of a C file called by an IDL routine.

First, compile the mandelbrot C routine as a shared library:

  • % gcc -fPIC -o mandelbrot.o -c mandelbrot.c
  • % ld -shared -o mandelbrot.dll mandelbrot.o

Second, run the mandelbrot routine from IDL:

  • % idl
  • IDL> result = mandelbrot(2.2,3.4,6.3,5.5,4,2.2,2.1)

The result can be printed as an array of mandelbrot values.

-- ElizabethAuden - 11 May 2005

Topic attachments
I Attachment History Action Size Date Who Comment
C source code filec mandelbrot.c r1 manage 0.9 K 2005-05-14 - 22:12 ElizabethAuden Mandelbrot C program
Unknown file formatpro r1 manage 0.8 K 2005-05-14 - 22:11 ElizabethAuden Mandelbrot IDL program
Edit | Attach | Watch | Print version | History: r3 < r2 < r1 | Backlinks | Raw View | More topic actions...
Topic revision: r2 - 2005-05-14 - ElizabethAuden
  • Edit
  • Attach
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback