|
External Development Guide: Adding System Routines |
|
Thanks to the definitive text on the C language (Kernighan and Ritchie, The C Programming Language, Prentice Hall, NJ, Second Edition, 1988), the "Hello World" program is often used as an example of a trivial program. Our version of this program is a system function that returns a scalar string containing the text "Hello World!":
#include <stdio.h>
#include "idl_export.h"
IDL_VPTR hello_world(int argc, IDL_VPTR argv[])
{
return(IDL_StrToSTRING("Hello World!"));
}
This is about as simple as an IDL system routine can be. The function IDL_StrToSTRING(), returns a temporary variable which contains a scalar string. Since this is exactly what is wanted, hello_world() simply returns the variable.
After compiling this function into a sharable object (named, for example, hello_exe), we can link it into IDL with the following LINKIMAGE call:
LINKIMAGE, 'HELLO_WORLD', 'hello_exe', 1, 'hello_world', $ MAX_ARGS=0, MIN_ARGS=0
We can now issue the IDL command:
PRINT, HELLO_WORLD()
In response, IDL writes to the screen:
Hello World!
IDL Online Help (March 06, 2007)