Calling C functions through EGL libraries

EGL programs can invoke C functions through Library parts with the stereotype nativeLibrary.
Follow these steps to prepare to invoke a C function from EGL:
  1. Identify the C functions to use in your EGL program and make sure they return an INT value of zero it they succeed.
  2. Download the EGL runtimes and extract the stack library and application object file to your computer:
    1. Download the EGL runtime file as explained in Installing the EGL runtime code for Java.
    2. Unzip the file to a temporary directory and find the following files:
      For the platform-specific stack libraries:
      • AIX®: EGLRuntimes/Aix/bin/libstack.so
      • Linux®: EGLRuntimes/Linux/bin/libstack.so
      • Win32:
        • EGLRuntimes/Win32/bin/stack.dll
        • EGLRuntimes/Win32/bin/stack.lib
      For the platform-specific application object files:
      • AIX: EGLRuntimes/Aix/bin/application.o
      • Linux: EGLRuntimes/Linux/bin/application.o
      • Win32: EGLRuntimes/Win32/bin/application.obj
  3. Compile all C code into one shared library and link it with the appropriate platform-specific stack library.

    Your C code receives values from EGL using pop external functions and returns values to EGL using return external functions.

    To compile all C code into a shared library:
    1. Using standard methods, compile all of your C code into one shared library and link it with the appropriate platform-specific EGL stack library.
    2. In the following platform-specific examples, file1.c and file2.c are C files containing functions invoked from EGL:
      On AIX (the ld command must be on a single line):
      cc -c -Iincl_dir  file1.c file2.c
      ld -G -b32 -bexpall -bnoentry 
         -brtl file1.o file2.o -Lstack_lib_dir 
         -lstack -o lib1_name -lc
      On Linux (the gcc command must be on a single line):
      cc -c -Iincl_dir  file1.c file2.c
      gcc -shared file1.o file2.o -Lstack_lib_dir
          -lstack -o lib1_name

      On Windows® (the link command must be on a single line):

      cl /c -Iincl_dir file1.c file2.c 
      link /DLL file1.obj file2.obj 
           /LIBPATH:stack_lib_dir 
           /DEFAULTLIB:stack.lib /OUT:lib1_name
      incl_dir
      The directory location for the header files.
      stack_lib_dir
      The directory location for the stack library.
      lib1_name
      The name of the output library.
    Note: If your C code is using any of the IBM® Informix® ESQL/C library functions (BIGINT, DECIMAL, DATE, INTERVAL, DATETIME), then the ESQL/C library must also be linked.
  4. Create a function table.

    The function table is a C source file which includes the names of all C functions to be invoked from the EGL program. In the following function table example, c_fun1 and c_fun2 are names of the C functions. All of the functions identified in the code must have been exported from the C shared library created in a previous step.

    #include <stdio.h>
    struct func_table {
    
          char *fun_name;
          int (*fptr)(int); 
    };
    
    extern int c_fun1(int);
    extern int c_fun2(int);
    /* Similar prototypes for other functions */
    
    struct func_table ftab[] =
                {
                     "c_fun1", c_fun1,
                     "c_fun2", c_fun2,
                     /* Similarly for other functions */
                     "", NULL
                };  

    Create a function table based on the example above, and populate the function table with the appropriate C functions. Indicate the end of the function table with "", NULL.

  5. Compile the function table and the appropriate platform-specific application object file into a shared library, and link this shared library with the shared library created in Step 2 and the stack library.

    The application object file is the interface between the EGL code and the C code.

    The following two artifacts must be compiled into one shared library and linked with the stack library and the library created in Step 2 above:
    • function table
    • application object file

    Compile the new shared library using the following example, where ftable.c is the name of the function table and mylib is the name of the C shared library created in Step 2 and lib_dir is the directory location for mylib. Specify lib2_name by using the dllName property or the vgj.defaultI4GLNativeLibrary Java™ runtime property.

    On AIX (the ld command must be on a single line):

    cc -c  ftable.c
    ld -G -b32 -bexpall -bnoentry 
       -brtl ftable.o application.o 
       -Lstack_lib_dir -lstack -Llib_dir 
       -lmylib -o lib2_name -lc

    On Linux (the gcc command must be on a single line):

    cc -c  ftable.c
    gcc -shared ftable.o application.o 
        -Lstack_lib_dir -lstack -Llib_dir 
        -lmylib -o lib2_name

    On Windows (the link command must be on a single line):

    cl /c ftable.c
    link /DLL ftable.obj application.obj 
         /LIBPATH:stack_lib_dir 
         /DEFAULTLIB:stack.lib 
         /LIBPATH:lib_dir 
         /DEFAULTLIB:mylib.lib /OUT:lib2_name

    Link the three libraries together.

With your C shared library, function table, and stack library linked, you are now ready to invoke the C functions from your EGL code. For information on how to invoke a C function in EGL, see Invoking a C function from an EGL program.


Feedback