avr-c++ micro how-to

Go To Last Post
94 posts / 0 new

Pages

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

I totally lost you there, Joe. Honestly. Knowing you (netwise), quite good I think, I suppose there is some really to-the-point wittiness or sarcasm somewhere in there regarding some mistake I made, but I cant spot it.

I have tried Googling "Smileys Jeep gas shortage bottom Pacific" with and without "driver nuts" but came up with nothing..

Help this stupid Swede out, and give me a clue. Please?!

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

Johan,

It wasn't aimed at you, but an attempt to wisecrack about getting into details like char * while using the wrong vehicle for the job in the first place. Some people want to use C++ as C without studying the basics or the philosophical differences between procedural versus object oriented programming. Not pointed at you, just a poor joke about other folks who want to use C++ when they don't seem to know anything about why one would choose C++ over C.

Sorry,
Smiley

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

smileymicros wrote:
Johan,

It wasn't aimed at you, but an attempt to wisecrack about getting into details like char * while using the wrong vehicle for the job in the first place. Some people want to use C++ as C without studying the basics or the philosophical differences between procedural versus object oriented programming. Not pointed at you, just a poor joke about other folks who want to use C++ when they don't seem to know anything about why one would choose C++ over C.

Sorry,
Smiley

So.. i guess you're talking about me.. are not you? And i wonder, why does a philosopher (haha) say that other folks don't know anything about programming paradigms without asking them first?

Before you say anything, try to be sure... this is a tip for life

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

Quote:

Shows as small a program as you possibly can that demonstrates them not working as far as you are concerned.

i've overloaded the << operator to work with a class lcd and strings like: lcd<<"something";

in the function definition i've tried to use the strlen function like this:

Lcd & Lcd::operator<<(char *s)
{

...

int length = strlen(s);

...

}

and i use the variable length at a for loop to print the string.. but it does not make any loop

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

OK, I'd actually like to see a bit more, eg the whole of the implementation of the operator overload, a declaration/creation of an Lcd object and a call/use of the operator.

I dont have AVR Studio handy right now (just taking a break with working overtime). But I did pull together this small test in MS VC++ which works like a charm. I of-course simulate sending the characters to the LCD proper with a call to putchar, but apart from that I suppose I am close to what you have?

// JSkywalker.cpp

#include "stdafx.h"
#include 

class Lcd
{
public:
   Lcd & operator<<(char * s)
   {
      for (size_t i= 0; i

I will try something similar in AVR Studio when I get home, if I'm not too tired.

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

Quote:

It wasn't aimed at you, but an attempt to wisecrack about getting into details like char * while using the wrong vehicle for the job in the first place.

That's okay, Joe. I wasn't upset but rather in wisecrack mode myself. Still, and you know this, my position is that many (dare I say "most"?) times C++ is just as good vehicle as C for embedded systems. We might then be in a situation where someone uses an appropriate vehicle but does not know why (bad), and maybe how to operate it (worse).

Please note that I am not accusing JSkywalker being in either situation, but merely dwelling on Joes scenario in general!

Quote:

Sorry

No need to be, Joe. If you actually succeed in hurting me for real, we will settle that in a PM or two. No need for old grumpy beards to fight such things out in public. Anfd you haven't even gotten close yet. :wink:

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

Here's what what I did:

1) Put the Lcd class in a C++ file, and in that file also put a global function test() that exercises the overloaded operator and gave that function C linkage (so that it can be called from main() that is in a C file):

//JSkywalker.cpp
#include 
#include 

class Lcd
{
public:
   Lcd & operator<<(char * s)
   {
      for (size_t i= 0; i

2) wrote a simple main.c, containing main() that calls test(). No, I didnt bother to create a header file for the test() function. Call me sloppy if you like...

//main.c
#include 

void test();

int main(void)
{
   test();

   return 0;
} 

3) Fiddled a bit with the makefile before I got it right (it's been a while since my last experiments with C++ on AVRs).

4) Built the whole shebang, started up AVR Studio, loaded the ELF file and stepped through the code. I see it looping inside the overloaded Lcd::operator<< outputting the chars to PORTB one after the other.

A total success as far as I am concerned.

So now tell us in similar detail, and preferrably in a likewise minimal test/demo project, what your problem is.

Attachment(s): 

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

Never mind

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

Quote:

Never mind

What?

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

All strings r working.. i've debugged it and i saw it works.. thanks for the help

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

Hello Anyone,
doing embedded stuff with winCE for some time, now i'm back to 8 bit uC but won't miss c++ anymore. Thanks to a few hints from this great thread i'm quite happy now with AVRStudio and C++. Debugging is not as comfortable as with VisualStudio, but in former time we were happy when we had a console for debug output.

Now i would like to do some things like wrapping the peripherals i work with in C++ classes, or create waitable events and stuff.

Therefore it would be necessary to hook the handler methods of some objects to certain ISR vectors.
The only approach coming to my mind would be a dispatcher class. I could create some ugly file with all the ISR handlers in it, which call a static function of this dispatcher class, forwarding some ID or the current IP value to it, to tell which ISR was firing. Objects could then register their instance pointer and the address of a handler method to a global object of that dispatcher class.

This is of course ugly and absolutely not uC-like. Probably you cracks can tell a much better way. Any ideas?

Regards
Manuel

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

There has been a few discussions here and at avr-libc and avr-gcc lists about ISR's and C++

http://jennaron.com.au/avr/class...

http://lists.gnu.org/archive/htm...
http://lists.gnu.org/archive/htm...

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

Thank you for those links, the first one already pretty much answers my questions.

Manuel

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

Although not AVR or avr-gcc specific, these might also be interesting reads:
http://www.embedded.com/9900245?...
http://www.embedded.com/columns/...

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

abrecht wrote:
i'm quite happy now with AVRStudio and C++. Debugging is not as comfortable as with VisualStudio,
Yes, the AVRStudio debugger says all class member data is "out of scope". That could be considered "not so comfortable". :)

Still, for me C++ is the only way to go.

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

I belive that readers of this forum would appreciate some concrete, AVR-specific examples that would allow for a quantitative comparison between C and C++ implementation of essentially the same problem specification.

To draw a fair comparison, one would need two equivalent AVR projects designed in object-oriented way, one in C and the other in C++. Otherwise, one would compare different programming paradigms and designs, not the direct impact of the programming language and compiler.

In this context, I believe that the projects at:

http://www.state-machine.com/avr

provide a unique opportunity to quantitatively compare the impact of C++. Specifically, the "QDK/C AVR-GNU" and "QDK/C++ AVR-GNU" projects use object-oriented designs with classes and single inheritance. They are both based on a small state-machine framework on top of which runs the classic "Dining Philosophers Problem" test application consisting of five Philosopher state machines and one Table state machine. The framework code consists mostly of a conditional logic to execute hierarchical state machines as well as an event queue, a timer module, and a tiny preemptive kernel. The Dining Philosophers application consists of state machines. I believe that this code is quite representative for typical projects that run on AVRmega MCUs. (These specific projects have been compiled for the AVR Butterfly board with the AVRmega169 MCU.)

So, here is the quantitative size comparison for the WinAVR/avr-gcc compiler (data taken from the map files in the release configuration):

                | "QDK/C AVR-GNU"  | "QDK/C++ AVR-GNU"
----------------+------------------+--------------------
Code size (ROM) |   5454 bytes     |  6200 bytes (+14%)
----------------+------------------+--------------------
Data size (RAM) |     48 bytes     |    88 bytes (+80%)

The C++ code uses the "Embedded C++" subset, which avoids exceptions and templates leaving basically only the object-oriented features (classes, inheritance, and virtual functions). Also, the C++ code is not using new and delete (and the C code is not using malloc and free).

The code size increase of 14% of C++ compared to C is typical, although for other compilers I've seen an increase of only about 5% for the same "Dining Philosophers" project.

However, the data size increase of 40 bytes is unusually high. I would expect an increase of some 12 bytes for the virtual pointers stored inside the 6 state machine objects that have virtual tables. The mapfiles indicate that the WinAVR compiler allocates virtual tables in the data space (RAM), which is not optimal because v-tables are synthesized at compile time and can typically be stored in ROM. I'm not aware of any compiler options for WinAVR to control placement of virtual tables. Perhaps someone knows how to force the v-tables into the program space (ROM)?

Finally, I hope that my post could help dispel the widespread belief that you can't do object-oriented (or at least object-based) programming in C. In fact, it is quite easy to implement classes and single inheritance in C. I've blogged about it at EmbeddedGurus.net:

http://www.embeddedgurus.net/sta...

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

Quote:

The mapfiles indicate that the WinAVR compiler allocates virtual tables in the data space (RAM), which is not optimal because v-tables are synthesized at compile time and can typically be stored in ROM. I'm not aware of any compiler options for WinAVR to control placement of virtual tables. Perhaps someone knows how to force the v-tables into the program space (ROM)?

AFAIK there is no way to get avr-gcc to store the V-tables in ROM (ie in flash memory). This is a performance hit that you get when using C++ (with the avr-gcc compiler) on AVRs.

Thank you for this post. I'll try to find some time to analyze in more detail what constitutes the increases in code and data size.

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

I took a quick look at the code. It seems it wasn't written as a C++ program. I'd guess it was either a quick copy from C code or it was written by a C programmer masquerading as a C++ programmer. I don't know how that would affect the code size.

I'm reminded of a task switching kernel we used at my previous job. It was taken from Dr. Dobbs Journal. It was written in the C language but it was obviously written by a Fortran programmer. I re-wrote every line of code, which typically happens when I use someone elses code. It was buggy too. It suffered from the "priority inversion" problem. Once fixed and cleaned up, it was pretty neat.

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

Hi All,

I browsed through this thread and failed to find source files where this discussion would be captured. Are those available anywhere? I could do a shot and upload something I have been using - based on the topmost parts of this thread. However, I am rather new in C++ and especially in embedded environment. I am not sure how *right* my files would be and certainly would appreciate more experienced developers to confirm or fix the files to a good shape.

It would also be nice to have these files available on the first post or other easily found location.

-mikko

Attachment(s): 

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

When just starting out with C++ and embedded programming it's easier to start with single thread programs. So using the -fno-threadsafe-statics switch will allow you to bypass the __cxa_guard stuff for a while. With embedded systems __cxa_guard wont be standard and will depend a lot on how the program is implemented. The best you can do is create stub files that can be filled in later when the specifics of the program/RTOS are known.

Attachment(s): 

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

which c++ compiler should we be using? the G++ one or C++ one?

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

Just invoke avr-gcc, if the source files have a .cc or .C extension the C++ compiler will be invoked.

As you can see it doesn't actually matter which is actually used:

C:\WinAVR-20100110\bin>fc /b "avr-c++.exe" "avr-g++.exe"
Comparing files avr-c++.exe and AVR-G++.EXE
FC: no differences encountered

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

Whatever discussed here, it seems the people at Arduino[1] are able to make C++ work (at least up to some point). Maybe their code can provide some pointers.

--
Alex.

[1] I hope I am allowed to use the A-word in this forum :)

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

Has anybody got this to work with avr studio 5?
I couldn't find a way to configure a project for C++. I started a new project and added C++ files to it, but they didn't compile, they weren't even INCLUDED in the list of files to BE compiled, even though they were added to the project.

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

Have you tried AS6? It fixes a lot of the problems AS5 had.

I just started AS6 said "new project" then "c++ executable". It created a .cpp file for me and I typed this into it:

/*
 * GccApplication2.cpp
 *
 * Created: 15/05/2012 14:29:00
 *  Author: cliff
 */ 


#include 

class foo {
	public:
	void setvar(uint8_t c);
	void printvar();
	private:
	uint8_t myvar;
};

void foo::setvar(uint8_t c) {
	myvar = c;
}

void foo::printvar() {
	PORTB = myvar;
}

int main(void)
{
	foo x;
	x.setvar(37);
	x.printvar();
    while(1)
    {
        //TODO:: Please write your application code 
    }
}

When I build the .lss contains:

void foo::setvar(uint8_t c) {
	myvar = c;
  92:	fc 01       	movw	r30, r24
  94:	60 83       	st	Z, r22
}
  96:	08 95       	ret

00000098 <_ZN3foo8printvarEv>:

void foo::printvar() {
	PORTB = myvar;
  98:	fc 01       	movw	r30, r24
  9a:	80 81       	ld	r24, Z
  9c:	88 bb       	out	0x18, r24	; 24
}
  9e:	08 95       	ret

000000a0 
: int main(void) { a0: cf 93 push r28 a2: df 93 push r29 a4: 0f 92 push r0 a6: cd b7 in r28, 0x3d ; 61 a8: de b7 in r29, 0x3e ; 62 foo x; x.setvar(37); aa: ce 01 movw r24, r28 ac: 01 96 adiw r24, 0x01 ; 1 ae: 65 e2 ldi r22, 0x25 ; 37 b0: 0e 94 49 00 call 0x92 ; 0x92 <_ZN3foo6setvarEh> x.printvar(); b4: ce 01 movw r24, r28 b6: 01 96 adiw r24, 0x01 ; 1 b8: 0e 94 4c 00 call 0x98 ; 0x98 <_ZN3foo8printvarEv> bc: ff cf rjmp .-2 ; 0xbc

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

clawson wrote:
I just started AS6 said "new project" then "c++ executable". It created a .cpp file for me and I typed this into it:

Cool. :D

I was suprised to see this feature when I started to use AS6.

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." - Marcus Aurelius               

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

Hello. Brilliant post; thanks loads for all the information. I'm trying to use new[] and delete[] on an Arduino (using Eclipse) and I think that native support for new[] and delete[] might have made some progress since the original post at the top of this thread.

Specifically, on avr-gcc 4.7.0 (installed on the latest version of Ubuntu: 12.10) it appears that new, new[], delete and delete[] all work out of the box.

But on avr-g++ 4.3.2 (installed by latest version of the Arduino IDE on Windows: 1.0.1) it appears that new and delete work out of the box but new[] and delete[] produce a undefined reference to `operator new[](unsigned int)' error.

Please may I ask two specific questions:

Firstly, is there any way to automatically detect whether new[] and delete[] are already defined? (e.g. a preprocessor directive of some sort)? For example, could we do something like #ifndef _OPERATOR_NEW_[]_ ?!

Secondly, on a platform which already has native new and delete but doesn't have new[] and delete[], should we make use of new and delete and our definitions of new[] and delete[]? e.g. something like this (I'm rather new at this game of manually defining new[] and delete[] so forgive me if this is a dumb suggestion):

void * operator new[](size_t size)
{
    return operator new(size);
}

void operator delete[](void * ptr)
{
    delete ptr;
} 

This appears to compile but produces this warning:

new_test.cpp: In function 'void operator delete [](void*)':
new_test.cpp:25: warning: deleting 'void*' is undefined
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Firstly, is there any way to automatically detect whether new[] and delete[] are already defined? (e.g. a preprocessor directive of some sort)? For example, could we do something like #ifndef _OPERATOR_NEW_[]_ ?!

Well one way would be to use these symbols:

E:\arduino-1.0.1\hardware\tools\avr\libexec\gcc\avr\4.3.2>avr-gcc -E -dM junk.c | grep GNU
#define __GNUC_PATCHLEVEL__ 3
#define __GNUC__ 4
#define __GNUC_MINOR__ 3

(it seems rather curious that the directory is called 4.3.2 but it appears to be 4.3.3?). Anyway just check the minor with something like:

#if __GNUC_MINOR__ == 3
 // define new/delete
#endif

Or perhaps more rigorously:

#define GCC_VERSION (__GNUC__ * 10000 \
                     + __GNUC_MINOR__ * 100 \
                     + __GNUC_PATCHLEVEL__)
...
#if GCC_VERSION <= 40302
 // define new/delete
#endif

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

jack_kelly wrote:

This appears to compile but produces this warning:

new_test.cpp: In function 'void operator delete [](void*)':
new_test.cpp:25: warning: deleting 'void*' is undefined

The compiler can't know how to properly delete the pointer since it's void and the actual type it points to isn't specified.

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

I have the following base class:
Class SerialPort
[
public:
SerialPort(size_t inputSize, size_t outputSize, uint_32 baudRate);
virtual bool Put(uint8_t data) = 0;
MyBuffer *GetInputBuff();
MyBuffer *GetOutputBuff();
// member variables are protected and not shown
};

I derive a class SerialPort0 from this
class SerialPort0 : public SerialPort
{
Public:
SerialPort0(size_t inputSize, size_t outputSize, uint32_t baudRate);
bool Put(uint8_t data);
};

And finally I build my main class
class MainClass
{
public:
MainClass();
~MainClass();
void Tick();
SerialPort *GetSerialPort();
void SetPort(SerialPort* _port);
private:
SerialPort *m_serialPort;
}

In the init code
I do the following

MainClass x,y;

x.setPort(y.GetSerialPort());

when I look at the m_serialPort I'm getting a Runtime Error message "An invalid DIE offset was passed to getDieFromId".

I'm stuck any ideas?

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

Quote:

I'm stuck any ideas?

This is not the place to discuss this - this thread serves to hold long time information that will be of benefit to all users of avr-g++. For specific usage questions start a separate (non sticky) thread.

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

Thanks loads for the quick replies! I've been grepping through my /usr directory to see if I can find where new, new[], delete and delete[] are defined on my shiny new Ubuntu 12.10 system.

It appears Arduino 1.0.0 introduced the files /hardware/arduino/cores/arduino/new.cpp and new.h, the latter of which defines NEW_H and the functions new and delete (but not new[] or delete[]).

It also appears the binary file /usr/lib/gcc/avr/4.7.0/cc1plus includes the text delete[] several times. So I guess gcc-avr 4.7 has support for new, new[], delete and delete[]. Which is nice!

For reference, it seems that the x86 version of new etc. is defined in /usr/include/c++/4.7/new (which defines _NEW)

There is some discussion of adding new and delete to avr-libc: http://savannah.nongnu.org/patch... and http://lists.nongnu.org/archive/...

Some discussion about adding new and delete support in Arduino: http://code.google.com/p/arduino...

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

jack_kelly wrote:
It also appears the binary file /usr/lib/gcc/avr/4.7.0/cc1plus includes the text delete[] several times. So I guess gcc-avr 4.7 has support for new, new[], delete and delete[]. Which is nice!
cc1plus is the actual c++ compiler, so you shouldn't assume support just from that. Since the compiler needs to know that new is a special keyword even if there is no library present that supports it.

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

Here's my "new.h" file which hopefully detects whether the relevant functions are defined and, if not, defines them:

#ifndef _CUSTOM_NEW_H_
#define _CUSTOM_NEW_H_

/* It appears that version 4.7.0 of avr-gcc defined
 * new[] and delete[] so if we detect an earlier version
 * then we must define new[] and delete[] ourselves.
 * This "#define GCC_VERSION" trick from clawson:
 * http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=59453&start=all&postdays=0&postorder=asc#1002400 */

#define GCC_VERSION (__GNUC__ * 10000 \
                     + __GNUC_MINOR__ * 100 \
                     + __GNUC_PATCHLEVEL__)

#if GCC_VERSION < 40700

/* Arduino 1.0.0 introduced the files
 * hardware/arduino/cores/arduino/new.cpp and new.h
 * the latter of which defines NEW_H.  
 * So if NEW_H is not defined then we should
 * define new and delete. */

#ifndef NEW_H
#define NEW_H
#include 
void * operator new(size_t size);
void operator delete(void * ptr);
#endif /* NEW_H */

// Arduino 1.0.1 doesn't include new[] or delete[]
void * operator new[](size_t size);
void operator delete[](void * ptr);

#endif /* GCC_VERSION */

#endif /* _CUSTOM_NEW_H_ */

let me know if this looks wrong!

Last Edited: Tue. Oct 16, 2012 - 09:08 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

atomicdog wrote:
cc1plus is the actual c++ compiler, so you shouldn't assume support just from that. Since the compiler needs to know that new is a special keyword even if there is no library present that supports it.

Ah, cool, thanks. That's interesting. So where do you think new[] and delete[] might be defined on my Ubuntu 12.10 system? (new[] and delete[] work out-of-the box on Ubuntu 12.10... the only reason I'm having to define them again is for folks on older systems).

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

jack_kelly wrote:
atomicdog wrote:
cc1plus is the actual c++ compiler, so you shouldn't assume support just from that. Since the compiler needs to know that new is a special keyword even if there is no library present that supports it.

Ah, cool, thanks. That's interesting. So where do you think new[] and delete[] might be defined on my Ubuntu 12.10 system? (new[] and delete[] work out-of-the box on Ubuntu 12.10... the only reason I'm having to define them again is for folks on older systems).

I'm not sure where exactly it would be but should be in a library or object file somewhere. I believe looking at the Map file will tell you exactly which library file the function new was pulled in from.

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

I'd imagine they'd most likely be in libc.a so try avr\lib

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

pls,i try to install avr-gcc compiler on my window 7 but its not installing what im i to do pls help me

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

Broxbourne wrote:
Quote:
You can use Student-Lockroom programming to achieve the result.

Thanks, I didni want to install avr-gcc compiler on my window 7 but not installing what im i to do

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

Felix,

This is not the correct thread to diagnose problems about installing avr-gcc. Start a new thread and in it give more detail about what you are trying to do and what does not work. For example where did you get the avr-gcc you are trying to use on Win7 and what happens when you try to install. Do NOT answer in this thread - start a new one with clearer description.

Moderator.

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

This information is perhaps the best for a C++ starter on AVR Studio. I must say I have skipped a lot of time and energy digging that.

Thank you TFrancuz for sharing that. And thanks for finding me the following error!

"C:\Program Files\Atmel\Atmel Toolchain\AVR8 GCC\Native\3.4.2.876\avr8-gnu-toolchain\bin\avr-g++.exe" -o JBot.elf  I2C.o uart.o hmc5883l.o JBot.o   -Wl,-Map="JBot.map" -Wl,--start-group -Wl,-lm  -Wl,--end-group -Wl,--gc-sections -mmcu=atmega16a 
		I2C.o:(.rodata._ZTV3I2C[vtable for I2C]+0x4): undefined reference to `__cxa_pure_virtual'
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you want to use new and delete and/or new[] and delete[], you will need to include malloc and free or something similar.  That's usually done on a PC but not so much on a microcontroller. 

 

If you just want to use new and/or new[], I have a simple function that will allocate from the heap.  It may be somewhere in this thread, or I can post it.

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

What the dickens is Student-Lockroom programming? 

madGambol

 

Pages