You are here

C++ implementation

I want to include my own C++ functions into Simile Projects, can someone tell what how to do it? I have already created a test C++ function and added it into procs.cpp file in Function folder, however, when I try to use that function it tells me- the function does not exist. Lest assume C++ code is correct, I have tested it with my own compiler. What should I do?

Forums: 

OK, you have created a c++ function and put it where the model program can call it. Now you need to tell the logic of Simile that the function exists so calls to it can be built into the model programs.

To do this you include a declaration in one of the .pl files in the same directory as your procs.cpp file. The declaration has a format like the following example:

function(gaussian, real, [real, real, real]).

This says you have included a function called gaussian which provides a real (i.e., float or double) result, and takes three real arguments. Note the period at the end -- it's part of Prolog syntax.

You can also use 'int' or 'boolean' for your argument or result data types. The name of the .pl file in which you put it determines the sub-heading under which your function will appear in the function selection menus. You can add a new .pl file if you want a new heading for it.

It's good to see people experimenting with these features so please keep in touch!

--Jasper

I have included
function(num, int, [int]).
in Arithmetic.pl, The function in procs.cpp is:
int num(int j)
{
int result;
result=j*2;
return(result);
}
The function is visible in pull down list, but when i run the model Simile tells that it does not recognize this function and then freezes.
How this files are being evaluated and at what stage? How can I solve this problem.

The massage is "Invalid Command name"

I am sorry to say, we are still having a few teething problems with this feature! In order to help you with this I would like to know which version of Simile you are using, and with which operating system.

It would also be helpful if you could send me the full text of your error message.

In the mean time I have made your function work in a model by putting the Prolog declaration in a file called Arithmetic.pl and the c++ definition in Arithmetic.cpp, and placing both these in a directory called Functions under Simile's local directory. This is called "My Documents/My Simile files" for Simile 4.1, but for older versions it is "Documents and Settings//.simile" and is a hidden directory. It is best to put your own functions etc in this directory because if you put them in the installation directory they may get overwritten when installing a new version of Simile.

Cheers
Jasper

My version is 4.0 enterprise. I created folder "Functions" in a local directory ".simile" I saved test.cpp and test.pl to the function folder
test.cpp:
int times_two_alex(int j)
{
int result;
result=j*2;
return(result);
}
test.pl:
function(times_two_alex, int, [int]).
In Simile it created new folder Procedures with my test function, but when i put it into my model and run I get error massage:
An exception occurred while building this model. It generated this error massage:
consistency_error(format($stream(2146930576),[84,104,105,115,32,109,111,100,101,108,32,99,97,110,110,111,116,32,98,101,32,98,117,105,108,116,32,
98,101,99,97,117,115,101,32,105,116,32,99,111,110,116,97,105,110,115,32,116,104,101,32,117,115,101,114,45,100,101,102,105,110,101,100,32,102,
117,110,99,116,105,111,110,32,126,97,44,32,119,104,105,99,104,32,115,104,111,117,108,100,32,104,97,118,101,32,97,32,100,101,102,105,110,105,
116,105,111,110,32,105,110,32,116,104,101,32,102,105,108,101,32,126,97,44,32,98,117,116,32,116,104,105,115,32,102,105,108,101,32,105,115,32,
109,105,115,115,105,110,103,46],compile:[times_two_alex/1,C:/Documents,and,Settings/Student/.simile/Functions/test.tcl]),[84,104,105,115,32,109,111,
100,101,108,32,99,97,110,110,111,116,32,98,101,32,98,117,105,108,116,32,98,101,99,97,117,115,101,32,105,116,32,99,111,110,116,97,105,110,115,
32,116,104,101,32,117,115,101,114,45,100,101,102,105,110,101,100,32,102,117,110,99,116,105,111,110,32,126,97,44,32,119,104,105,99,104,32,115,
104,111,117,108,100,32,104,97,118,101,32,97,32,100,101,102,105,110,105,116,105,111,110,32,105,110,32,116,104,101,32,102,105,108,101,32,126,97,
44,32,98,117,116,32,116,104,105,115,32,102,105,108,101,32,105,115,32,109,105,115,115,105,110,1
03,46],compile:[times_two_alex/1,C:/Documents,and,Settings/Student/.simile/Functions/test.tcl],format_arguments)
I also copied Gaussian function into a new folder and I got the same error massage

This is interesting. It seems to be looking for your procedure in the folder test.tcl -- and this means it is looking for a Tcl version of the procedure rather than a c++ one. This would happen if you were running your model using the 'Debug' menu entry rather than the 'Run' entry, since 'Debug' attempts to execute the model as Tcl code which normally produces more informative error messages than c++, but does not execute as quickly.

So you can either go ahead and create the file test.tcl with a Tcl implementation of your procedure in it, or run the model in c++ using the 'Run' entry. The problem that is causing the error message to look so horrible will be fixed in the next release of Simile (sadly it's not fixed in 4.1)

--Jasper

It was nice of you to mention that that Debug compiles Tcl and Run compiles C++, because after I have started hitting Run button my C++ functions started to compile and work, yes I did not know something as obvious as this, so in future version just add something descriptive to buttons:
Run (Compiles C++)
Debug (Compiles Tcl)
Thank for you prompt responses. Really appreciate you help.
Alex.

For C++
1. It wants extra new Line in your source code in order to compile
2. Names of all functions must be unique even if you place them in different folders

HIT RUN FOR C++ CODE TO COMPILE

HIT DEBUG FOR TCL CODE TO COMPILE