Cicada ---> Online Help Docs ---> Example: neural networks in Cicada ---> Rules for embedding C/C++ code

Cicada’s userfn source files

Cicada interfaces with a user’s C/C++ code through the file userfn.c. Usually this is the only Cicada source file we need to deal with. The cleanest way to incorporate C/C++ functions is to include their header files at the top of userfn.c, reference them in the UserFunction[] array, and add the source files to Cicada’s Makefile.

To give an explicit example, let’s assume our C function myFunction() is stored in a source file called myFunctionFile.c, and prototyped in myFunctionFile.h. Open up userfn.c (assuming we’re using the C version of Cicada). At the top include our header file.


    // **********************************************
    // #include any user-defined header files here
   
    #include "myFunctionFile.h"
   

Next, add a new entry to the UserFunctions array defined in userfn.c. The name of the C function is myFunction(), but earlier in our examples we invoked it by writing either call("myFunctionInC", ...) or $myFunctionInC(...), so its Cicada name is myFunctionInC. The altered UserFunctions array (which already contained two entries) now reads:


    userFunction UserFunctions[] = { { "pass2nums", &pass2nums }, { "cicada", &runCicada },
                             { "myFunctionInC", &myFunction } };
   

We are done changing userfn.c, but we still need to modify the Makefile so that our C code will get compiled into Cicada. First, add a new object file to the end of the OBJ definition in Makefile:


    OBJ = lnklst.o ... ccmain.o myFunctionFile.o
   

Second, define our new object file by adding the following line to the very end of Makefile:


    myFunctionFile.o: userfn.h myFunctionFile.h myFunctionFile.c
   

All of the Cicada source files and .cicada files should be in the same directory together with Makefile and our myFunctionFile.c and myFunctionFile.h files. Finally, from the command prompt, navigate to that directory and type make cicada CC=gcc. Assuming that both the make tool and a C/C++ compiler like gcc are installed, we will then have an application named cicada with myFunction as embedded as a command.

As mentioned earlier, we can run Cicada from within another application by including ccmain.h and making the following function call:


    rtrn = runCicada(argc, argv)    | argc = 0 or 1
   

where argc is either 0 if Cicada is to run the script start.cicada in the default directory, or 1 if the script to run is a file whose name and path are stored in a C string at argv[0]. (Note that argv[0] is not the pathname that the command prompt traditionally passes to a C program.)


Prev: C function declaration    Next: A minimal wrapper


Last update: May 8, 2024