Calling C functions from 4GL

We can call my_c_func in the 4GL source like any other function:

# my_4gl_src.4gl

 

MAIN

 DEFINE n1, n2 INTEGER

 CALL my_c_func(10) RETURNING n1, n2

 DISPLAY n1

 DISPLAY n2

END MAIN

The 4GL source can be compiled using fglc, as per usual. The C source must be compiled into a dynamic library and linked against lib4glc in the Lycia distribution. Assuming our C source is compiled into a library with the base name my_c_lib, we can dynamically link it to the 4GL module as follows:

qlink my_4gl_src.4o -lib my_c_lib -o my_prog.4o

 

More information about qlink can be found here.

The same can be achieved, if you create a 4GL program in Lycia Studio. To link a dynamic C library located in the project to a 4GL program do this:

Step 2. Select the library from the list and click Finish.

 

If the dynamic library is not built, the command to build the 4GL program will cause the library to build also. This only works if the library is an internal one, i.e. it is located in the same project as the 4GL program. For more information about how to link internal and external libraries in LyciaStudio, refer here.

Differences in work with C using LyciaStudio and c4gl

In LyciaStudio all C sources can be only in the form of a C library. You can not use C main functions as a program entry point. It was possible with c4gl as it converted 4GL files to C sources and then built them as C programs. With LyciaStudio, you need to add a main function wrapper in the 4GL code:

1. You need to rename the main function in the C file to some other function (for example, c_main).

2. Connect the C module in the C library.

3. Create a 4GL file, where you should add the main block and call your c_main in it.

Usage example

main function was renamed to c_main

# cor_main.c

 

c_main()

{

int nResult;

...

Then, we create a new 4GL file cor_register_main. 4GL which called the c_main function from cor_main.c

# cor_register_main.4gl

 

GLOBALS "globals.4gl"

MAIN

  DEFER QUIT

  DEFER INTERRUPT

  CALL c_main() RETURNING gs_exit

  CALL exit_prog()

END MAIN

 

Contact Us

Privacy Policy

Copyright © 2024 Querix, (UK) Ltd.