constructors

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Adek336

constructors

Post by Adek336 »

This codes defines a class, makes a global variable of type ¨class foo¨ and inits it. I tested that the constructor is executed unless I compile as I would compile a kernel... does anybody know why is this so?

Cheers,
Adrian

class foo
{
public:
foo (int);
...
};

foo bar (100);

int main()
{
}
mikeleany

RE:constructors

Post by mikeleany »

You're not including the built-in startup code when you link your kernel, and this is what calls the constructors and destructors of static and global objects. Also, if you have the following global variable, it won't be initialized:

int foo = bar(100);

Because function bar() would have to be called before main(). The following example of global declarations is also interesting:

int x = 5, y = 6;
int z = x + y;

With gcc (may be different with other compilers) z is computed at run-time, before main() is called, so it wouldn't work with your kernel either.

In order for all these things to work, you have to write your own functions to call the constructors and destructors (in my two examples, bar(100) and x + y are treated as constructors). Unfortunately, I haven't seen a good tutorial or document on how to do this. But even if there were, it would have to be compiler specific. The best one I've seen is the second half of the C++ tutorial at: http://www.invalidsoftware.net/os/?the_id=11
There are a few problems with it. First, atexit() is a C and POSIX standard function, which does not do what the tutorial says (but you will still need a function like his atexit(), just call it a different name). Second, the constructors should be called starting from the end of the list and going toward the beginning, while the destructors should be called starting from the beginning and going to the end. This will make sure they are called in the proper order. Third, it says nothing about __cxa_atexit and __dso_handle. __cxa_atexit is (sort of) a C++ version of atexit. You will need to impliment it if you want your destructors called. __dso_handle has to do with dynamically shared libraries. Even though you're probably not using any for your kernel, you still need to define it. Just define it like this:

  void * __dso_handle = 0;

You can find more about __cxa_atexit and atexit at the following sites:

http://www.linuxbase.org/spec/refspecs/ ... texit.html
http://www.opengroup.org/onlinepubs/007 ... texit.html

Also, a description of atexit can be found by typing "man atexit" at the command prompt in linux.

One last thing. You can ignore the final parameter of __cxa_atexit. It just has to do with dynamicly shared libraries.

I think I covered everything, but I might have missed a thing or two. If you don't understand something or have any other questions about it, just ask. I may write up a proper tutorial for it soon.
Adek336

RE:constructors

Post by Adek336 »

wow..! I now have a working cxa_atexit and the destructors are called! wow..! now I still ´ve got the gxx_personality_v0 with no code. what is it supposed to do?

BTW, console IO in C kernels is with printf() and in C++ with cout yes?

Cheers and thanx,
Adrian.
mikeleany

RE:constructors

Post by mikeleany »

gxx_personality_v0? Hmm... I think I've seen that once before, but I don't remember for sure what caused it. But I don't think it's supposed to be ther. My guess would be that you need another flag or two passed to either the compiler or linker. I use the following flags for different parts of making my kernel.

Preprocessor flags: -nostdinc -nostdinc++
Compiler flags:     -fno-builtin -fno-exceptions -fno-rtti
Linker flags:       -T myscript -static

Just play around with the flags you use and see if it helps. And you do use your own linker script instead of the built-in one, right?
mikeleany

RE:constructors

Post by mikeleany »

Almost forgot. Yes, console output is (typically) with printf() in C and cout in C++.
Post Reply