MPC5748G C++ startup code

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

MPC5748G C++ startup code

1,852 Views
kfirbs
Contributor III

Hi,

I have C++ code that contains Classes with inheritance.

For some reason upon a call to a virtual function the app crashes.

From what I understand there is a problem in the startup code that doesn't call the constructors and destructors.

example:

class ClassA
{
public:
virtual int sum() = 0;
};

Inherited Class:

class ClassB
{
public:
ClassB(int startValue);

int sum();

private:
int fStartValue;
};

Calls in main:

ClassA* pObjectA;
ClassB objectB(10);

main() {

pObjectA = &objectB;

total += pObjectA->sum();

}

The exception happens upon the call to sum.

Can anyone help me resolve this issue?

9 Replies

1,478 Views
kfirbs
Contributor III

Thanks Martin,

Can you send me the sample project you have created with the startup code and linker script?

0 Kudos
Reply

1,478 Views
martin_kovar
NXP Employee
NXP Employee

Hello,

please see the post attachment.

Regards,

Martin

0 Kudos
Reply

1,478 Views
kfirbs
Contributor III

Thanks for the source, I get the followinlg linker error:

powerpc-eabivle-g++ -o "MPC5748G-C++Test_Z4_0.elf" "@MPC5748G-C++Test_Z4_0.args"
./src/main.o:(.rodata._ZTV6ClassA[_ZTV6ClassA]+0x8): undefined reference to `__cxa_pure_virtual'
./src/main.o:(.rodata._ZTI6ClassB[_ZTI6ClassB]+0x0): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
./src/main.o:(.sdata._ZTI6ClassA[_ZTI6ClassA]+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'
collect2.exe: error: ld returned 1 exit status
make: *** [MPC5748G-C++Test_Z4_0.elf] Error 1

0 Kudos
Reply

1,478 Views
martin_kovar
NXP Employee
NXP Employee

Hello,

which version of S32 Design Studio you use? This project can be compiled (and linked) in S32DS for Power Architecture version 1.2. Please see the link below:

https://nxp.flexnetoperations.com/control/frse/download?agree=Accept&element=8199217 

If you have any other questions, please feel free to write me back.

Regards,

Martin

0 Kudos
Reply

1,478 Views
martin_kovar
NXP Employee
NXP Employee

Hello,

could you please explain me, what you try to do? The code you wrote above cannot be compiled and I do not know what you try to achieve.

Do you try to create polymorphism using virtual functions? If yes, this solution is completely incorrect. If no, please clarify me, what you try to do.

About constructors destructors, this issue is fixed in S32 Design Studio v1.2. You can download if from the link below:

http://www.nxp.com/webapp/swlicensing/sso/downloadSoftware.sp?catid=S32-DS-E2001-2 

Regards,

Martin

1,478 Views
kfirbs
Contributor III

Hi,

I wanted to present the general idea of the code, this can be taken to any environment and any compiler.

I know that the code above is pseudo an doesn't include startup code, linker script or even includes to headers.

This of course shows polymorphism yes, what is that incorrect? it works in linux, and it compiles fine with the eabivle-gcc compiler.

0 Kudos
Reply

1,478 Views
martin_kovar
NXP Employee
NXP Employee

Hello,

I understand it is pseudo-code without linker, startup, etc, but from my point of view, if you want to show how polymorphism works, the code should look like:

class ClassA
{

public:
virtual int sum() = 0;

};


class ClassB : public ClassA    //there must be inheritance
{

public:
//ClassB(int startValue);          //I comment this because gcc is not able to compile this, but it does not matter


int sum()
{
    return fStartValue = 10;     //this is only example to show, that it works correct
}

private:
int fStartValue;

};

ClassA* pObjectA;
ClassB* objectB = new ClassB();  //it was necessary to create objectB dynamically, because the code you used above cannot be compiled using S32DS

in main

int total = 0;
pObjectA = objectB;
total = pObjectA->sum();

this code works and correctly returns value 10 defined in function sum in class B.

Please try this code in S32DS v1.2 and let me know if it works. Also if I am not correct and I did not understand your idea, please let me know.

Regards,

Martin

0 Kudos
Reply

1,478 Views
kfirbs
Contributor III

Thanks Martin, several questions:

1. Why did you comment out the constructor of ClassB with parameter load?

2. I wish that ClassB will be global and not dynamically located pointer. thus pObjectA will point to actual ClassB as shown in my example.

0 Kudos
Reply

1,478 Views
martin_kovar
NXP Employee
NXP Employee

Hello,

1) I commented this, because I am not able to compile.

pastedImage_3.png

If you want to use constructor, you should use standard declaration like:

class ClassB : public ClassA
{

public:
ClassB(int startValue)
{
    this->fStartValue = startValue;
}


int sum()
{
    return fStartValue = 10;
}

private:
int fStartValue;

};

2) I finally did it the way you want. The reason, why I was not able to compile it before was inheritance. If class A is inherited by ClassB, you can simply write pObjectA = &objectB;  Please see code bellow:

//This code is out of main function

ClassA* pObjectA;
ClassB objectB(10);

//This code is in main function

int total = 0;
pObjectA = &objectB;
total = pObjectA->sum();

After sum is called, number 10 is stored in variable total.

If you have any other questions, please feel free to write me back.

Regards,

Martin

0 Kudos
Reply