我们在KolibriOS下的Linux上用C / C ++编写

介绍



KolibriOS是一个微型操作系统,其内核和大多数程序都是用汇编语言编写的。当然,这并不意味着不可能用其他编程语言编写KolibriOS。



本文是为Linux设置工具链的指南。



开始吧



为此,您需要下载:





创建一个文件夹/ home / USER / autobuild(其中USER是用户名)。接下来,让我们创建一个链接:



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


让我们转到/ home / autobuild。创建目录树/ home / autobuild / tools / win32。下载上述工具链并将其解压缩到/ home / autobuild / tools / win32。接下来,从FTP下载SDK,然后通过/ home / autobuild / tools / win32 / lib和/ home / autobuild / tools / win32 / mingw32 / lib解包(在两个地方-因为两个路径都在makefile中使用)。有趣的来了。



下载SVN



突出显示一个文件夹。我在这里将其称为/ home / USER / KOS_SVN。在终端中运行:



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


您需要等待,直到下载了整个SVN。



准备编译



为了能够使用这些工具,您需要在“ / etc / environment”文件中的“ PATH”环境变量中使用工具注册该文件夹的路径。



sudo nano /etc/environment


并在文件末尾添加:



:/home/autobuild/tools/win32/bin


您还需要下载libisl



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


另一个已知问题:/

home/autobuild/tools/win32/bin/../libexec/gcc/mingw32/5.4.0/cc1:加载共享库时出错:libmpfr.so.4:无法打开共享库文件:否这样的文件或目录



通过链接更正:



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


汇编



示例程序位于/ home / USER / KOS_SVN / contrib / sdk /示例中。以开罗为例。让我们转到该文件夹​​并说make如果一切成功,cairo二进制文件将出现在文件夹中,该文件夹在KolibriOS中运行。



再举一个例子



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;
       }
    }
}


以下Makefile将适用于此文件(用制表符替换空格):



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 $@ $<


如果有任何问题,请在评论中写。




All Articles