Engee documentation
Notebook

Compiling C code in Engee using GCC

Among the built-in tools of the Engee platform, there are also tools for working with generated code. In this example, we will learn how to compile the code and call it from the Engee script.

More information about executing external programs in the Engee environment can be found in the documentation.

Compilation tools in Engee

Engee executes models and programs on a Unix-compatible kernel, using libraries and tools unique to Engee. There are enough third-party tools included in the delivery of the operating system kernel, which makes it possible to create specific custom software development and testing paths and models.

In Engee, engineers can use many tools from the standard Unix kernel package, for example:

  • gcc – C code compiler
  • g++ – C++ code compiler

In addition to these, the package usually contains a lot of additional utilities and interpreters for different programming languages (python, perl...).

Let's run the following cell to find out the compiler version. gcc (and at the same time check its performance):

In [ ]:
;gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/12/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 12.2.0-14' --with-bugurl=file:///usr/share/doc/gcc-12/README.Bugs --enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-12 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-12-bTRWOB/gcc-12-12.2.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-12-bTRWOB/gcc-12-12.2.0/debian/tmp-gcn/usr --enable-offload-defaulted --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.2.0 (Debian 12.2.0-14) 

At the time of creating this example, the Engee system allowed the use of a compilation chain. gcc versions 12.2.0 although we need this command more for debugging purposes – to make sure that the compiler is installed and running.

Compiling code in Engee

Just in case, let's go to the same folder where this example is located.:

In [ ]:
cd( @__DIR__ )

In this demo, we will run a simple program that just prints an informational message.

Here is the code of the program that we will compile and run.:

#include <stdio.h>

int main (void)
{
  printf ("Hello, world!\n");
  return 0;
}

To get an executable file from the source code that is in the file hello.c, it is enough to execute the following command:

In [ ]:
;gcc -Wall hello.c -o hello

As a result, an executable file is generated in the working folder of this example. hello, which can be run with arguments from the command line or from a script.

Execution of compiled code

How do I get the output of this code in a script or on the command line? The first option is to run the command in the mode shell. This way you can get the output, but not put it in a variable.

In [ ]:
;./hello
Hello, world!

The command works similarly run but its difference is that it creates a process whose execution can be controlled.

In [ ]:
run( `./hello` )
Hello, world!
Out[0]:
Process(`./hello`, ProcessExited(0))

If the result of the compiled file needs to be placed in a variable, you can use the commands read or readchomp (this command option eliminates line breaks):

In [ ]:
c = readchomp( `./hello` )
Out[0]:
"Hello, world!"

Your code can accept parameters via a string of arguments passed as part of the invoked command. Thus, only serialized (text) data can be transferred to the code.

Another option is to compile the code with the key. -shared. make a library of functions out of it and pass any binary parameters to it using @ccall.

Finally, not the most technologically advanced, but very reliable option is to exchange data through files located in the file storage.

Conclusion

In Engee, engineers can use both high-level tools such as data inspector, interactive scripts and modeling canvas, as well as the lowest-level commands of the operating system kernel.