| Extending the language / C-Extensions | |
C-Extensions functions can be called from the program in the same way that you call a BDL function.
int function-name( int )
Here function-name must be written in lowercase letters. The fglcomp compiler converts all BDL functions names (following a CALL keyword) to lowercase.
The C function must be declared in the usrFunctions array in the C interface file.
In the next code example, the C-Extension module mycext.c defines the c_fct() function:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "f2c/fglExt.h"
int c_fct( int n );
UsrFunction usrFunctions[]={
{"c_fct",c_fct,2,2},
{0,0,0,0}
};
int c_fct( int n )
{
int rc;
float price;
char name[31];
if (n != 2) exit(1);
popflo(&price);
popvchar(name, sizeof(name));
printf(">> [%s] price:%f\n", name, price);
pushint( strlen(name) );
price = price * 2;
pushflo( &price );
return 0;
}
The C-Extension library is imported by the BDL module with IMPORT:
IMPORT mycext
MAIN
DEFINE len INT, price2 FLOAT
CALL c_fct("Hand gloves", 120.50)
RETURNING len, price2
DISPLAY "len = ", len
DISPLAY "price2 = ", price2
END MAIN
$ gcc -I $FGLDIR/include -shared -fPIC -o mycext.so mycext.c $ fglcomp myprog.4gl $ fglrun myprog.42m >> [Hand gloves] price:120.500000 len = 11 price2 = 241.0