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 or userfn.cpp. 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/userfn.cpp, 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. First, we open up userfn.c (since we will be using the C version of Cicada). At the top we include the header file.


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

Next, we 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 call("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 we add a new object file to the end of the OBJ definition in Makefile:


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

Second, we 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 we can navigate to that directory and type make cicada. 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, the other point of interest is the following line in userfn.h:


    #define CicadaMainProgram
   

This line will causes Cicada’s int main() function to compile, which is appropriate if Cicada is to be a stand-alone application. On the other hand, if Cicada is to be embedded within a larger C/C++ application, then we need to comment this line out. To run Cicada from within another application we need to include ccmain.h and make 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 11, 2017

Get Cicada scripting language at SourceForge.net. Fast, secure and Free Open Source software downloads