We write in C / C ++ on Linux under KolibriOS

Introduction



KolibriOS is a miniature operating system, the kernel and most of the programs of which are written in assembly language. This, of course, does not mean that it is impossible to write for KolibriOS in other programming languages.



This article is a guide to setting up a toolchain for Linux.



Let's start



To do this, you need to download:





Create a folder / home / USER / autobuild (where USER is the username). Next, let's create a link:



sudo ln -s /home/USER/autobuild /home/autobuild


Let's go to / home / autobuild. Create a directory tree / home / autobuild / tools / win32. Download the above toolchain and unpack it to / home / autobuild / tools / win32. Next, download the SDK from FTP and unpack it over / home / autobuild / tools / win32 / lib and / home / autobuild / tools / win32 / mingw32 / lib (in two places - because both paths are used in makefiles). Now comes the fun part.



Download SVN



Highlight one folder. I will refer to it here as / home / USER / KOS_SVN. Run in the terminal:



cd /home/USER/KOS_SVN
svn co svn://kolibrios.org


You need to wait until the entire SVN is downloaded.



Preparing for compilation



To be able to use the tools, you need to register the path to the folder with the tools in the "PATH" environment variable in the "/ etc / environment" file.



sudo nano /etc/environment


And add at the end of the file:



:/home/autobuild/tools/win32/bin


You also need to download the libisl library :



wget http://board.kolibrios.org/download/file.php?id=8301libisl.so.10.2.2.7z && 7z x file.php?id=8301libisl.so.10.2.2.7z

sudo mv libisl.so.10.2.2 /usr/lib/x86_64-linux-gnu && sudo ln -s /usr/lib/x86_64-linux-gnu/libisl.so.10.2.2 /usr/lib/x86_64-linux-gnu/libisl.so.10

sudo chmod go-w /usr/lib/x86_64-linux-gnu/libisl.so.10 && sudo chmod go-w /usr/lib/x86_64-linux-gnu/libisl.so.10.2.2


Another known issue:

/home/autobuild/tools/win32/bin/../libexec/gcc/mingw32/5.4.0/cc1: error while loading shared libraries: libmpfr.so.4: cannot open shared object file: No such file or directory



Corrected by link:



sudo ln -s /usr/lib/x86_64-linux-gnu/libmpfr.so.6 /usr/lib/x86_64-linux-gnu/libmpfr.so.4


Compilation



Sample programs are located in / home / USER / KOS_SVN / contrib / sdk / samples. Take Cairo for example. Let's go to the folder and say make . If everything is successful, the cairo binary will appear in the folder , which runs in KolibriOS.



One more example



hello.c :



#include <kos32sys.h>

char* title = "Window";

void _draw_window(){
    BeginDraw();
    DrawWindow(100,100,400,200,title,0x80ffffff,0x13);
    EndDraw();
}

int main()
{
    _draw_window();
    for (;;)
    {
       switch(get_os_event())
       {
          case 1:
             _draw_window();
             continue;
          case 2:
             // key pressed, read it and ignore
             get_key();
             continue;
          case 3:
             // button pressed; we have only one button, close
             if(get_os_button() == 1) return 0;
             continue;
       }
    }
}


The following Makefile will work for it (replace spaces with tabs):



CC = kos32-gcc
LD = kos32-ld 

SDK_DIR:= /home/USER/KOS_SVN/contrib/sdk

LDFLAGS = -static -S -nostdlib -T $(SDK_DIR)/sources/newlib/app.lds --image-base 0

CFLAGS = -c -fno-ident -O2 -fomit-frame-pointer -fno-ident -U__WIN32__ -U_Win32 -U_WIN32 -U__MINGW32__ -UWIN32

INCLUDES= -I $(SDK_DIR)/sources/newlib/libc/include
LIBPATH:= -L $(SDK_DIR)/lib -L /home/autobuild/tools/win32/mingw32/lib

SOURCES = hello.c   \
      $(NULL)

OBJECTS =  $(patsubst %.c, %.o, $(SOURCES))

default: hello.kex

hello.kex: $(OBJECTS) Makefile
    $(LD) $(LDFLAGS) $(LIBPATH) --subsystem native -o hello.kex $(OBJECTS) -lgcc -lc.dll
    objcopy hello.kex -O binary

%.o : %.c Makefile $(SOURCES)
    $(CC) $(CFLAGS) $(INCLUDES) -o $@ $<


If you have any problems, write in the comments.




All Articles