Sharing global variables
While not recommended, you can share global variables declared in your program with a C module.
In order to share the global variables declared in your program, you must:
- Generate the .c and .h interface files by
using
fglcomp -G
with the module defining the global variables:GLOBALS DEFINE g_name CHAR(100) END GLOBALS
fglcomp -G myglobals.4gl
This will produce two files named
myglobals.h
andmyglobals.c
. - In the C module, include the generated header file and use the global variables
directly:
#include <string.h> #include "f2c/fglExt.h" #include "myglobals.h" int myfunc1(int c) { strcpy(g_name, "new name"); return 0; }
- When creating the C-Extension library, compile and link with the
myglobals.c
generated file.
Tip: Using global variables is not recommended. It makes your code difficult to
maintain. If you need persistent variables, use module variables and write set/get
functions that you can interface with.