📜 ⬆️ ⬇️

We write in C / C ++ in Windows under KolibriOS

image
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 for other programming languages, the path in KolibriOS is closed. For example, during the evolution of this operating system, there have been several attempts to develop tools or adapt libraries to create C / C ++ applications. The KolibriOS repository still has working examples that use early adaptations of C / C ++ code, for example (root) / programs / games / kosilka or (root) / programs / system / shell, using different C / Asm wrappers and approaches.

For the current time, the most promising of the existing libraries, in my opinion, is newlib. It consists of an adapted libc, C-wrapper over core coreI functions and a toolchain for building.

Unfortunately, the native C / C ++ compiler does not exist in KolibriOS, the current toolchain involves building applications in Windows or Linux.
')
This article is a guide to setting up newlib for Windows.


Install toolchain

• The archive with the current toolchain is located at
ftp.KolibriOS.org/users/Serge/new/Toolchain
Windows version of the archive, msys-kos32-xxx7z
• To install the toolchain, you need mingw / msys; to do this, go to www.mingw.org via the downloads link and download and run the file mingw-get-setup.exe
In the window that appears, after clicking the “install” button, you need to select the installation path, leave the checkboxes next to ".. support for graphics user interface" and click "Continue".
Attention, this article assumes that the installation of mingw is made in c: \ MinGW
In the window that appears, you need to note mingw32-base and msys-base and start the installation.
• After installation, go to the c: \ MinGW \ msys \ 1.0 folder and create the home \ autobuild \ tools folder tree in it
• Unpack msys-kos32-xxx7z to c: \ MinGW \ msys \ 1.0 \ home \ autobuild \ tools
(if everything is done correctly, the kos32-gcc.exe file should be located along the path c: \ MinGW \ msys \ 1.0 \ home \ autobuild \ tools \ win32 \ bin \ kos32-gcc.exe)
• You can test the toolchain by running c: \ MinGW \ msys \ 1.0 \ msys.bat
in the command line type
export PATH=$PATH:/home/autobuild/tools/win32/bin kos32-gcc –v 

The configuration and version information of kos32-gcc should appear on the screen.

Libc install

• Actual libc components are in the KolibriOS svn repository, path (root) / contrib / sdk / sources / newlib
You can also use the KolibriOS.org web interface in the SVN section and download the corresponding archive.
Attention, in this article it is assumed that the installation of newlib is made in d: \ kolibri \ contrib \ sdk \ sources \ newlib
• After installation, go to the d: \ kolibri \ contrib \ sdk folder and create the bin and lib folders in it
• Run c: \ MinGW \ msys \ 1.0 \ msys.bat
• on the command line type
  cd d:/kolibri/contrib/sdk/sources/newlib/libc export PATH=$PATH:/home/autobuild/tools/win32/bin make shared make install 

• As a result, the file libc.dll should appear in the folder d: \ kolibri \ contrib \ sdk \ bin
in the folder d: \ kolibri \ contrib \ sdk \ lib - libapp.a, libc.dll, a, libdll.a

Install Qemu

Qemu is an emulation (and virtualization) system for a computer that supports various architectures. The Qemu installation files are located at qemu.weilnetz.de

• To work with Qemu, you need to download the "LiveCD bootable CD" image of KolibriOS at KolibriOS.org/ru/download
Attention, in this article it is assumed that the image of KolibriOS is located along the path d: /kolibri/dist/kolibri.iso
• After installation, in the working folder (for example, d: \ work) create the file qemu.bat

File d: /work/qemu.bat
  set QEMU_PATH="c:/Program Files/qemu/" set PATH=%PATH%;%QEMU_PATH% qemu-system-i386.exe -L . -m 128 -cdrom d:/kolibri/dist/kolibri.iso -boot d -localtime -vga vmware -net nic,model=rtl8139 -net user -soundhw ac97 -usb -usbdevice tablet 

• You can read more about the KolibriOS startup keys on Qemu at wiki.KolibriOS.org/wiki/Setting_up_QEMU

Create application

You can test the performance of newlib by putting a simple application that displays “hello, world!” To the window and “hello, board!” To the debug panel.

File d: /work/simple.c
 #include "stdlib.h" #include "stdio.h" #include "string.h" #include "kos32sys.h" //--------------------------------------------------------------- static const char *WND_HEADER_STR = "window header"; static const char *HELLO_WORLD_STR = "hello, world!"; static const char *HELLO_BOARD_STR = "hello, board!\n"; static const uint32_t CUSTOM_BUTTON_ID = 100; //--------------------------------------------------------------- static void RenderWindow(void); static void BoardPuts(const char *c); static void SetMaskForEvents(unsigned int mask); //--------------------------------------------------------------- void RenderWindow() { const uint32_t WND_STYLE = 3; // (window type III, skinned window) const int WND_X = 100; const int WND_Y = 100; const int WND_W = 320; const int WND_H = 320; const color_t WND_COLOR = 0xffffff; const int BUTTON_W = 24; const int BUTTON_H = 24; const int BUTTON_X = WND_W / 2 - BUTTON_W - 8; const int BUTTON_Y = (WND_H - BUTTON_H) / 2; const color_t BUTTON_COLOR = 0x9e9e9e; const color_t TEXT_COLOR = 0xff0000; BeginDraw(); DrawWindow(WND_X, WND_Y, WND_W, WND_H, WND_HEADER_STR, WND_COLOR, WND_STYLE); draw_text_sys(HELLO_WORLD_STR, WND_W / 2, WND_H / 2, strlen(HELLO_WORLD_STR), TEXT_COLOR); DefineButton(65536 * BUTTON_X + BUTTON_W, 65536 * BUTTON_Y + BUTTON_H, CUSTOM_BUTTON_ID, BUTTON_COLOR); EndDraw(); } //--------------------------------------------------------------- // coreAPI #63 asm function call example void BoardPuts(const char *s) { unsigned int i = 0; while(*(s + i)) { asm volatile ("int $0x40"::"a"(63), "b"(1), "c"(*(s + i))); i++; } } //--------------------------------------------------------------- // coreAPI #40 set event mask void SetMaskForEvents(unsigned int mask) { asm volatile ("int $0x40"::"a"(40), "b"(mask)); } //--------------------------------------------------------------- int main() { enum SysEventTypes { REDRAW_EVENT = 1, GUI_BUTTON_EVENT = 3 }; // enabling only REDRAW_EVENT and GUI_BUTTON_EVENT, 0000000000000101b SetMaskForEvents(0x5); // open BOARD application and check for a text message BoardPuts(HELLO_BOARD_STR); RenderWindow(); for(;;) { const uint32_t e = get_os_event(); switch(e) { case REDRAW_EVENT: { RenderWindow(); } break; case GUI_BUTTON_EVENT: { const uint32_t bt = get_os_button(); if(bt == CUSTOM_BUTTON_ID || bt == 1) // 1 -- [x] window button id { return 0; } } break; } } return 0; } //--------------------------------------------------------------- 

File d: / work / Makefile
 EXEC = simple CC = kos32-gcc LD = kos32-ld OBJCOPY = kos32-objcopy SDK_DIR:= /d/kolibri/contrib/sdk LDFLAGS = -static -S -nostdlib -T$(SDK_DIR)/sources/newlib/app.lds -Map $(EXEC).map --image-base 0 CFLAGS = -c -O2 -fomit-frame-pointer -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 export PATH=$PATH:/home/autobuild/tools/win32/bin:/MinGW/bin default: $(EXEC) $(EXEC):$(EXEC).o makefile $(LD) $(LDFLAGS) $(LIBPATH) -o $(EXEC) *.o -lgcc -lapp -lc.dll $(OBJCOPY) $(EXEC) -O binary %.o : %.c makefile $(CC) $(CFLAGS) $(INCLUDES) -o $@ $< 

To build the application, you need to run c: \ MinGW \ msys \ 1.0 \ msys.bat
in the command line type
 cd d:/work make 

The result of the execution will be an executable file in KolibriOS

It is important :
To build a c ++ project, you need to use the kos32-gcc compiler, the order in which the libraries are specified for the linker is: libgcc last, libc after libsupc ++ and gcc_eh.

Example
$(LD) $(LDFLAGS) $(LIBPATH) -o $(EXEC) *.o -lapp -lsupc++ -lgcc_eh -lc.dll -lgcc

Transferring files between Windows and KolibriOS

The easiest way to transfer files between Windows and KolibriOS is a flash-usb drive.
Another way to use Qemu is to create a disk image and mount it as hdd. In this case, qemu.bat is supplemented by another key, -hda d: \ work \ c100.img, where c100.img is the name of the image. The image should be already formatted, so it is better not to create it, but remove it from a small flash-usb drive using Win32DiskImager.exe utility
You can edit the image using WinImage .

If when loading KolibriOS the contents of the / hd0 / 1 disk are empty, you need to reboot the system and in the offered boot menu select the item "[b] Add disks visible through BIOS" and enable this option (1)

Known Issues

By default, applications built with newlib require the /KolibriOS/lib/libc.dll file in KolibriOS.
When using Qemu, this may cause problems with the “minimal” version of kolibri.img.
There are two solutions to this problem:

• Use the iso version (LiveCD boot CD)
If the “simple” application generates an error at startup, most likely the old version of libc.dll is most likely on the image. Usually on the ftp.KolibriOS.org/users/Serge/new/Toolchain already mentioned above you can find the latest libraries in the sdk.zip archive. A simple way to update the libraries on the image is to use the MagicISO utility.

• Use the built-in functionality of KolibriOS "searchapp":
- copy the kolibri.lbl from d: \ kolibri \ dist \ kolibri.iso to the root of the disk (flash-usb drive, disk image * .img)
(kolibri.iso can be unpacked with the 7z archiver or winrar, kolibri.lbl is in the root)
- in the root of the same disk drive create a folder tree kolibrios / lib
- copy the executable file (“simple”) to the / kolibrios folder
- copy libc.dll from d: \ kolibri \ contrib \ sdk \ bin to the folder / kolibrios / lib

useful links

All header-files of wrappers and adapted libraries are in (kolibri) \ contrib \ sdk \ sources \ newlib \ libc \ include

Description of system functions
(example of working with coreAPI in the example simple.c, function board_puts)

Newlib forum thread

File archive for this article
• the _kos folder contains ready-made files for launch in KolibriOS
• the _win32 folder contains image / c100.img (disk image for Qemu with compiled “simple”), makefile, qemu.bat, simple.c

PS
I express my deep gratitude to mentor Sourcerer for their patience, help and advice!

image

Source: https://habr.com/ru/post/229231/


All Articles