We’ve created a solution for accessing 4GL global variables from C code in a form of a Python script (included in Lycia Compiler).
At runtime, as soon as control has been passed to the C code, Lycia copies the values of all registered global 4GL variables to the C side and vice versa. As soon as the 4GL side receives control back from the C code, the values of global variables are copied to the variables on the 4GL side. Therefore, you will have two copies of certain global variables: one on the C side and one on the 4GL side. Lycia will make sure that the values are valid at the moment control is passed to the C or 4GL side by registering all the defined 4GL global variables in C.
To use this solution, you will need:
Step 1. Create a .4gl file with C function call and .c file with 4GL function call. You will find an example in the snippets below:
global_fgl.4gl
GLOBALS
DEFINE var INT # Define global variable 'var'
END GLOBALS
MAIN
LET var = 8 # Initialize global variable 'var'
DISPLAY "(init) var = ", var # Display current value of global variable 'var'
CALL add_two() # Calling a C function that increments the global variable 'var' by two
DISPLAY "(main) var = ", var # Display current value of global variable 'var'
END MAIN
FUNCTION mult_two()
DISPLAY "(mult_two before) var = ", var # Display current value of global variable 'var'
LET var = var * 2
DISPLAY "(mult_two after) var = ", var # Display current value of global variable 'var'
END FUNCTION
global_c.c
#include <fglapi.h>
extern int4 var; // Define external global variable var
void add_two() {
var = var + 2; // Increment by two
fgl_call(mult_two, 0); // Calling the fgl function, which multiplies by two
var = var + 2; // Increment by two
}
global_c.c
#include <fglapi.h>
extern int4 var; // Define external global variable var
void add_two() {
var = var + 2; // Increment by two
fgl_call(mult_two, 0); // Calling the fgl function, which multiplies by two
var = var + 2; // Increment by two
}
Step 2. For generating C globals, you need to execute the Python script globals_c.py with a 4GL file that defines globals (or a list of such files, separated with a space):
python /opt/Querix/Lycia/bin/globals_c.py global_fgl.4gl
As a result, the script will generate .c file(s) with shared global variables. The file(s) will have the same name(s) as the .4gl file(s).
Step 3. Include generated file(s) — in our example, global_fgl.c — in C static library.