Engee documentation
Notebook

Calling the C code in Engee

In this demo, we will explore the possibilities of using C code in Engee scripts. After all, although most of the code can be written in Engee, there are many high-quality and mature libraries for numerical calculations 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 macro @ccall.

As an example, let's call the clock function from the C standard library. Clock does not accept 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 the option of calling a function written in the C language. To do this, take a file with a function written in C and assemble the executable file .so from it.

To do this, go to the Engee command line, turn on the shell line using 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 this action.

image.png

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

image.png

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

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

Conclusion

In this demo, we have analyzed the capabilities of Engee in terms of using C, both by calling functions inside the script and by connecting user functions to perform the tasks assigned to us.