some issue with c++

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
amirsadig

some issue with c++

Post by amirsadig »

In my current master thesis, I should implement a real time OS (using ucos-ii) with powerpc mpc5200. it also required to add c++ support.
my OS has newlib (libc) and with it I have a full standard c with all input/output functionality.
now when i compile this example with my OS:

Code: Select all

#include <iostream>

using namespace std;

class test {
        public:
        test() {printf("hallo amir 1\n"); };
        ~test() {};
        void printhallow(void);       
};

void test::printhallow(void)
{
    printf("hallo amir 2\n");      
    //cout << " here 23 \n"; 
}

extern "C" void testCPP(void)
{
        test *t = new test();       
        t->printhallow();
}
this example work fine, but when I uncomment //cout .. part
I got undhandled expection (not c++ exception, it is like interrupt)

should I initialise something before call that code?
amirsadig

Re:some issue with c++

Post by amirsadig »

just to mention.
I use GCC 4.1.1 and latest newlib and libstdc++-v3
bluecode

Re:some issue with c++

Post by bluecode »

Just a thought: cout is most likely a global/static object, which needs to be initialized. I suspect you don't call the constructors of global/static objects on application startup, do you?
Look into th faq if you don't...

But you know, that you have to port a standard c++ library in order to use it? You can't just link your app with the standard C++ library of linux and then execute it under your os.
amirsadig

Re:some issue with c++

Post by amirsadig »

surely I have a cross compile stdc++ library.

yes I don't call any of global/static objects, because I don't what they are and where they are located ;D

I will read a bout them and on how i can i call them
bluecode

Re:some issue with c++

Post by bluecode »

ok, if yu want to check whether there are static/gobal objects or not, then you can just look at the size of the ctors section in your executable (given that you don't discard that section).
amirsadig

Re:some issue with c++

Post by amirsadig »

thank for your quick help!!
I call now the global/static objects.
now I don't get any exception. that is good.
but no output come.
note that printf() work but "cout << ..." not !
bluecode

Re:some issue with c++

Post by bluecode »

hm,

ok, I looked a little bit into the gnu stdlibc++ source and came up with the following:
in src/ios_init.cpp there is a class constructor called ios_base::Init::Init(), which seems to initialize the cout/cin/* objects. You should check whether this constructor (I'm not yet sure if it is a constructor and if it is belonging to a global/static object) is really called (some printf'ing is sufficient).
Another thing: Did you also implement the placement new (again, look into the faq)? Because this is used quite much by this constructor.
amirsadig

Re:some issue with c++

Post by amirsadig »

ios_base::Init::Init()
is got called from one of the global objects.

I don't have my own implementation I use the one with stdlc++, which call at end my malloc and free functions.

I found that my libc ist fully initialized before I call "cout", so stdout is not valid.

now it work fine.

thank you alot for your help
Post Reply