Engee documentation
Notebook

Calling C code in Engee

In this demonstration we will explore the possibilities of using C code in Engee scripts. After all, while much of the code can be written in Engee, there are many high quality and mature libraries for numerical computation already written in C.

Engee adheres to the "no templates" philosophy: you can call functions directly from a script without any "linking" code, code generation or compilation.

This is achieved by calling the @ccall macro.

As an example, let's call the clock function from the C standard library. Clock takes no arguments and in our case returns Int32.

In [ ]:
t = @ccall clock()::Int32
print("Тип t: " * string(typeof(t)) * ", при t равном: " * string(t))
Тип t: Int32, при t равном: 365356426

Now let's consider a variant of calling a function written in C. To do this, let's take a file with a function written in C and build an executable file .so from it.

To do this, go to the Engee command line, include the shell line with a semicolon and execute the following commands.

cd /user/start/examples/integrated_languages/calling_c_code gcc -c -fPIC say_y.c -o say_y.o gcc say_y.o -shared -o say_y.so

Below is a screenshot of the execution of this action.

image.png

The C function itself is very simple: it outputs the input argument. The screenshot below shows the code of the function.

image.png

To call the function, we need to specify the path to the .so file and call the function itself.

In [ ]:
@ccall "$(@__DIR__)/say_y.so".say_y(10::Cint)::Cvoid
Введён: y = 10.

Conclusion

In this demonstration, we have explored the capabilities of Engee in terms of applying C, both by calling functions within the script and by connecting custom functions to fulfil our tasks.