3

I am developing a kernel in C++. But I do not want to write a stdlib; for that purpose I have downloaded STLport http://www.stlport.org/, but I don't know how to install and use it.

I am using Linux for building my kernel.

How can I use c++ standard libs in my kernel?

And I do not want to port all libs from STLport. How can I exclude a selection of libs? Like std::string, std::vector etc.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • How about just extending 2.6.35 with boost? I think it should suit all your needs! – Andrejs Cainikovs Sep 19 '11 at 11:43
  • Thanks for your reply, But I am not referring Linux Kernel for developing my kernel. I am developing totally different kernel from Linux as well as Unix. –  Sep 19 '11 at 11:46
  • I am using Linux Fedora Core 15 –  Sep 19 '11 at 11:46
  • When i am installing stlport it gives error like this: `/usr/bin/ld: cannot find -lsupc++` –  Sep 19 '11 at 11:50
  • provide more details about your environment (env variables, compiler version and how you invoke it), how you installed STLPort, etc – CharlesB Sep 19 '11 at 12:01
  • I am using Fedora OS for building my kernel. For compilation I am using GCC 4.6 –  Sep 19 '11 at 12:04
  • i am installing STLport using this commands: ./configure --prefix=../c++_kernel/stl --without-rtti --without-thread --with-cxx=g++ && make && make check –  Sep 19 '11 at 12:05

3 Answers3

2

I am not sure whether STL in kernel is actually good to have, but if you really want to try, it's very fun. I have written my own OS and when I had memory allocation in the kernel, the first thing I did was porting STLport (5.2.1). It was working well so far, although the kernel itself is still too preliminary.

Anyway, I can share some experience on porting it.

  1. Porting STLport requires no building and very few prerequisites, just include the headers and let the compiler know it's path (-I option for gcc). The template classes will be compiled with your cpp source files.

  2. STLport is configurable, you can disable what you can't afford and select what you want, such as iostream, debug, exception, RTTI and threading. Just checkout the documentation and then get to the configuration headers, it's very nicely commented (e.g. stlport/stl/config/user_config.h)

  3. As the most basic you'll need malloc and free, or maybe new, delete and variants. That's enough for porting std string, containers and algorithms, IIRC. But it's neither thread safe nor memory allocation optimized, you need to be very careful when you rely on it.

  4. You can do you own iostream, it's just template classes and global objects (BTW, I hacked ELF sections and manually initialized my global objects by calling the functions), but this needs more work.

admiring_shannon
  • 904
  • 6
  • 16
2

I would probably advise against using the STL in Kernel development. STL will assume some form of standard library support of which there is none in your kernel. Also most memory allocation operations have no bounds for the time that they can take and are therefore unsuitable for use in interrupt handlers. Exceptions are another thing that can cause major headaches in the kernel

doron
  • 27,972
  • 12
  • 65
  • 103
  • So that I do not want to use all functions from STL, i want to use only basic STLs like std::string, std::vector, math.h which will not affect my kernel. I want to standard iostream lib but it should call my own system function `write` or `put_char`. So please help me, How can i do that? –  Sep 19 '11 at 13:16
  • 1
    The STL doesn't do memory allocations itself; that's another part of the Standard Library. Considering that he's writing a kernel in C++, he presumably has a proper `operator new` that is suitable for kernel use. – MSalters Sep 19 '11 at 13:18
  • Yes, i have proper `operator new` for memory allocation. I just want to use very basic functions like `IO operations, string operations, algebraic operations etc.`.Thank you. –  Sep 19 '11 at 13:22
  • 1
    don't use strings, use static arrays since you never know when strings do memory allocations. Also a kernel will have its own way of doing IO, if at all, so please forget anything from the standard library. – doron Sep 20 '11 at 00:04
2

In order for STL to work you have to port several things like static initialization (for i.e. std::cin and std::cout) and stack unwinding...

you'd have to port i.e.: libsupc++ and have that in your kernel. Basically all this stuff shouldn't be in the Kernel in the first place. DON'T use Vectors use static arrays because vectors might reallocate your data!

also all that stuff will bloat your kernel for nothing!

you can have a look what L4 allows itself to be used in kernel. they don't do memory allocation and they don't do exceptions (unpredictable) and they especially don't do STL.

The latter links shall give you an idea what you need to port to get c++ operating system support. Libsupc++ is part of gcc. it's purpose is to encapsulate all the parts where runtime code is needed.

Useful information about libsupc++

Useful information about c++ operating system support

Alexander Oh
  • 24,223
  • 14
  • 73
  • 76
  • How can I do that? I don't know the procedure. When I was installing STLport it was giving me error that `/usr/bin/ld: cannot find -lsupc++` –  Sep 19 '11 at 17:28