C++ pure virtual class with newlib

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

C++ pure virtual class with newlib

1,695 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by franku on Sun Aug 19 11:41:17 MST 2012
Hi out there, I hope someone can give me a hint on a problem building a c++ static library using newlib and lpcxpresso. Everything works fine, bit I have a size-problem.

I have got a class that has pure virtual methods. Another class derives this base class and implements the function:

class A {
 public:
   virtual void method() = 0;
}
class B : public A {
   virtual void method() { // do something } 
 }
Using the above code links appr. 60kB more code from newlib into the application than if I use the following code, where the base class method is already implemented but overwritten in the derived class:

class A {
 public:
   virtual void method() {} // <-- the only change
}
class B : public A {
   virtual void method() { // do something } 
}
I know that gcc for other small devices is capable to build this with smaller code, probably with a commandline switch, but I do not know how.

Does someone have a hint on how I could prevent the compiler to  auto-implement that much functions used for the pure virtual class?
0 Kudos
7 Replies

1,321 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by dpursell on Thu Oct 25 09:44:45 MST 2012

Quote: daniel.widyanto
Sorry to revive old thread.

The Newlib implementation of '__cxa_pure_virtual()' is bloated, causing any C++ program with pure virtual method (eg. virtual mymethod() = 0 ) suddenly grow in size. Redefining __cxa_pure_virtual() reduces the code size significantly.

Seems like http://wiki.osdev.org/C%2B%2B_Bare_Bones is quite useful for those who wanted to try C++ in LPCXpresso (crossing fingers )




Thank you for the reply! I had given up hope that anyone had experience in the matter

I'm able to save quite a bit of code space in a simple test program by including this:

extern "C" void __cxa_pure_virtual()
{
  while (1)
    ;
}


The code size with vs without this snippet is 0x2954 to 0x4CDC (difference of 0x2388).

However, in my actual program, where I'm not only using multiple files but also a separate library, including this function only reduces my code size from 0x3E1FC to 0x3E190 (difference of 0x6C). I've tried including this function in the library or in the main project, and both give the same result.

Obviously this won't make an enormous amount of difference since even a code reduction of 0x2388 won't make that much of a dent in my program (there seems to be something else causing bloat too) but it would still be helpful if anyone has an idea why it works so well in a simple test program but not in practice.

I'll keep testing too and post anything I find here for others.

Thanks again!
0 Kudos

1,321 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by daniel.widyanto on Thu Oct 25 03:36:28 MST 2012
Sorry to revive old thread.

The Newlib implementation of '__cxa_pure_virtual()' is bloated, causing any C++ program with pure virtual method (eg. virtual mymethod() = 0 ) suddenly grow in size. Redefining __cxa_pure_virtual() reduces the code size significantly.

Seems like http://wiki.osdev.org/C%2B%2B_Bare_Bones is quite useful for those who wanted to try C++ in LPCXpresso (crossing fingers :) )
0 Kudos

1,321 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by dpursell on Wed Aug 29 12:06:14 MST 2012
Hello franku,

I'm running into a similar problem with massive code bloat while linking to CMSIS2 and using pure virtual functions. I checked the link you mentioned, but it's just an explanation of __cxa_pure_virtual() and I'm not sure how that ties in with the code bloat issue. If you could elaborate a little on your solution I would greatly appreciate it.

Thanks!
0 Kudos

1,321 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by franku on Sun Aug 26 01:56:17 MST 2012

Quote: ArtjomGromak
You application code size is ~60 kB?
What options is used for compiler and linker?
I test you code (class B derived from A) and my application size is ~1.5 kB.
If is called B.method application size not increses (B.method is empty).



Solved this issue following this link.

Thanks for your help.
0 Kudos

1,321 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by franku on Sat Aug 25 03:19:23 MST 2012
However, this problem seems not regarding newlib but stdc++ lib.
0 Kudos

1,321 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by franku on Sat Aug 25 03:13:12 MST 2012
Yes, it is this large.

I am currently using a  c++ static lib and the CMSISv2p00_LPC17xx library (in pure C). The application is c++, too.

I am using the standard gcc flags that include -fno-rtti and -fno-exceptions.

Recently I figured out that if I link against the CMSIS lib at least with one single function the code size will blow up from ~1,5k to ~88k.
0 Kudos

1,321 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ArtjomGromak on Wed Aug 22 10:07:37 MST 2012
You application code size is ~60 kB?
What options is used for compiler and linker?
I test you code (class B derived from A) and my application size is ~1.5 kB.
If is called B.method application size not increses (B.method is empty).
0 Kudos