Simple C-Extension example

This example shows how to create a C-Extension library on Linux™ using gcc.

The command line options to compile and link shared libraries can change depending on the operating system and compiler/linker used.

The "split.c" file

#include <string.h>
#include "f2c/fglExt.h"

int fgl_split( int in_num );
int fgl_split( int in_num )
{
       char c1[101];
       char c2[101];
       char z[201];
       char *ptr_in;
       char *ptr_out;
       popvchar(z, 200); /* Getting input parameter */
       strcpy(c1, "");
       strcpy(c2, "");
       ptr_out = c1;
       ptr_in = z;
       while (*ptr_in != ' ' && *ptr_in != '\0')
       {
               *ptr_out = *ptr_in;
               ptr_out++;
               ptr_in++;
       }
       *ptr_out=0;
       ptr_in++;
       ptr_out = c2;
       while (*ptr_in != '\0')
       {
               *ptr_out = *ptr_in;
               ptr_out++;
               ptr_in++;
       }
       *ptr_out=0;
       pushvchar(c1, 100); /* Returning the first output parameter */
       pushvchar(c2, 100); /* Returning the second output parameter */
       return 2; /* Returning the number of output parameters (MANDATORY) */
}

The "splitext.c" C interface file

#include "f2c/fglExt.h"

int fgl_split(int);

UsrFunction usrFunctions[]={
  { "fgl_split", fgl_split, 1, 2 },
  { 0,0,0,0 }
};

Compile the C Module and the interface file

gcc -c -I $FGLDIR/include -fPIC split.c
gcc -c -I $FGLDIR/include -fPIC splitext.c

Create the shared library

gcc -shared -o libsplit.so split.o splitext.o -L$FGLDIR/lib -lfgl

The program "split.4gl"

IMPORT libsplit
MAIN
      DEFINE str1, str2 VARCHAR(100)
      CALL fgl_split("Hello World") RETURNING str1, str2
      DISPLAY "1: ", str1
      DISPLAY "2: ", str2
END MAIN

Compile the .4gl module

fglcomp split.4gl

Run the program without the -e option

fglrun split