static keyword problem

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have a simple class called for example MyClass.
Now i declare an instance as static within a function.

void example() {
static MyClass tmp;
}

This code gives me an error like this:
"undefined reference to __cg_xxxxx"

If i delete the "static" keyword it wont generate an error.

It's a project for my arduino.
Do you know what's happening?

I'm sorry, i'm at work right now and can't paste my current code.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ummm... I'm guessing that __cg_xxxxx is a function normally provided by the C++ run time that is not implemented in the avr-gcc run time library. There are a few that are not implemented -- you need to supply your own.

There was an excellent post a few weeks back summarizing the quirks of avr-gcc C++ .. I can't seem to find it right now with search, but as I recall it was posted by one of the regular members shortly after this related thread:
https://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=59085&highlight=

Anyway, you are probably simply running into one of those unimplemented run-time routines. Supply your own and you are good to go.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here's a post that might help

avr-c++ micro how-to

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes! That's the one I was thinking of. Can we make it a sticky?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks, i see it's a common problem.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Constructing a static class within a function isn't common around here.

Maybe you are trying to get around the inability to use the new operator. If that's the case, you can fix that problem easily. Create a file called new_delete.cpp (or something like that) and put this code into it:


#include "stdlib.h"    // for size_t

extern char __heap_start;
char* heap_next_p = &__heap_start;



void* AllocateFromHeap_p(size_t objsize)   {
   void* here_p = heap_next_p;
   heap_next_p += objsize;
   return here_p;
   }


void* operator new(size_t objsize)   {
   return AllocateFromHeap_p(objsize);
   }


void operator delete(void* obj)   {
   }

I wouldn't program an AVR without it.

Of course it doesn't free up memory if you call delete. I rarely use the delete operator in my AVR code anyway. Deleting classes at shutdown is good when running on a PC but there is no shutdown on the AVR. You aren't going to delete your statics either.

Classes that exist temporarily can be put on the stack as usual, but classes that persist can be "newed" if you use this code.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Try providing a stub:

int __cg_xxxxx = 0;

or

void __cg_xxxxx(void) {}

Kludgy, but may work.

C: i = "told you so";