Cicada ---> Online Help Docs ---> Example: neural networks in Cicada

Rules for embedding C/C++ code

The first step in embedding C or C++ code is to use the correct version of Cicada. We can use C code with the C version of Cicada, whose source files ending in .c. Likewise we can use C++ code with the C++ version of Cicada, whose source files end in .cpp. The two versions of Cicada are identical except for the names of their source files.

There are two ways to merge C with Cicada: we can embed C functions inside Cicada (per the tutorial), and we can also embed Cicada inside of a larger C or C++ program. If Cicada is going inside of a larger program, then we need to comment out the following line in Cicada’s userfn.h file.


    // #define CicadaMainProgram
   

An outside C/C++ function can run an instance of Cicada by calling


    errCode = runCicada(argc, argv);
   

With no arguments (argc = 0) Cicada will run the start.cicada script. If we set argc to 1 and pass the name of a script file in argv[0], then Cicada will load and run that script file instead. Any source file that calls runCicada() also needs to include Cicada’s ccmain.h source file.

The procedure for embedding C or C++ functions involves three steps. First, one declares the C/C++ routine in a particular way and writes the code accordingly; this is easy because Cicada-embedded functions look just like the main() function of a stand-alone program. In the second step the author introduces his routine to Cicada by changing a couple of lines in Cicada’s userfn.c or userfn.cpp file. The final, optional step is to write a wrapper in Cicada’s scripting language that allocates storage for the C/C++ function and provides it with a smooth interface.


Prev: The Anagrambler   Next: C function declaration


Last update: May 11, 2017

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