Posted by JohanEkdahl: Wed. May 6, 2009 - 05:55 AM
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?!
As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here.
No guarantees, but if we don't report problems they won't get much of a chance to be fixed! Details/discussions at link given just above.
"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]
Posted by smileymicros: Wed. May 6, 2009 - 03:29 PM
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.
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
Posted by JohanEkdahl: Wed. May 6, 2009 - 07:00 PM
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.
As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here.
No guarantees, but if we don't report problems they won't get much of a chance to be fixed! Details/discussions at link given just above.
"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]
Posted by JohanEkdahl: Wed. May 6, 2009 - 07:10 PM
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:
As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here.
No guarantees, but if we don't report problems they won't get much of a chance to be fixed! Details/discussions at link given just above.
"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]
Posted by JohanEkdahl: Wed. May 6, 2009 - 08:48 PM
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...
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.
As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here.
No guarantees, but if we don't report problems they won't get much of a chance to be fixed! Details/discussions at link given just above.
"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]
Posted by JohanEkdahl: Thu. May 7, 2009 - 08:29 PM
1
2
3
4
5
Total votes: 0
Quote:
Never mind
What?
As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here.
No guarantees, but if we don't report problems they won't get much of a chance to be fixed! Details/discussions at link given just above.
"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]
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?
As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here.
No guarantees, but if we don't report problems they won't get much of a chance to be fixed! Details/discussions at link given just above.
"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]
Posted by quantum-leap: Sun. Aug 16, 2009 - 06:59 PM
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.
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):
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:
Posted by JohanEkdahl: Sun. Aug 16, 2009 - 07:08 PM
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.
As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here.
No guarantees, but if we don't report problems they won't get much of a chance to be fixed! Details/discussions at link given just above.
"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]
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.
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.
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.
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 :)
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.
Posted by jack_kelly: Tue. Oct 16, 2012 - 08:52 AM
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):
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_[]_ ?!
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".
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.
Posted by jack_kelly: Tue. Oct 16, 2012 - 08:33 PM
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)
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.
Posted by jack_kelly: Tue. Oct 16, 2012 - 09:02 PM
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:
* https://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_ */
Posted by jack_kelly: Tue. Oct 16, 2012 - 09:04 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).
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.
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.
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.
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?!
As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here.
No guarantees, but if we don't report problems they won't get much of a chance to be fixed! Details/discussions at link given just above.
"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]
- Log in or register to post comments
TopJohan,
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
- Log in or register to post comments
TopSo.. 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
- Log in or register to post comments
Topi'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
- Log in or register to post comments
TopOK, 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?
I will try something similar in AVR Studio when I get home, if I'm not too tired.
As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here.
No guarantees, but if we don't report problems they won't get much of a chance to be fixed! Details/discussions at link given just above.
"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]
- Log in or register to post comments
TopThat'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!
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:
As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here.
No guarantees, but if we don't report problems they won't get much of a chance to be fixed! Details/discussions at link given just above.
"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]
- Log in or register to post comments
TopHere'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):
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...
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):
As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here.
No guarantees, but if we don't report problems they won't get much of a chance to be fixed! Details/discussions at link given just above.
"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]
- Log in or register to post comments
TopNever mind
- Log in or register to post comments
TopWhat?
As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here.
No guarantees, but if we don't report problems they won't get much of a chance to be fixed! Details/discussions at link given just above.
"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]
- Log in or register to post comments
TopAll strings r working.. i've debugged it and i saw it works.. thanks for the help
- Log in or register to post comments
TopHello 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
- Log in or register to post comments
TopThere 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...
~~John
TWI C source code
- Log in or register to post comments
TopThank you for those links, the first one already pretty much answers my questions.
Manuel
- Log in or register to post comments
TopAlthough not AVR or avr-gcc specific, these might also be interesting reads:
http://www.embedded.com/9900245?...
http://www.embedded.com/columns/...
As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here.
No guarantees, but if we don't report problems they won't get much of a chance to be fixed! Details/discussions at link given just above.
"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]
- Log in or register to post comments
TopStill, for me C++ is the only way to go.
- Log in or register to post comments
TopI 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):
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...
Miro Samek
www.state-machine.com
- Log in or register to post comments
TopAFAIK 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.
As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here.
No guarantees, but if we don't report problems they won't get much of a chance to be fixed! Details/discussions at link given just above.
"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]
- Log in or register to post comments
TopI 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.
- Log in or register to post comments
TopHi 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):
- Log in or register to post comments
TopWhen 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):
~~John
TWI C source code
- Log in or register to post comments
Topwhich c++ compiler should we be using? the G++ one or C++ one?
- Log in or register to post comments
TopJust 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:
- Log in or register to post comments
TopWhatever 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 :)
- Log in or register to post comments
TopHas 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.
- Log in or register to post comments
TopHave 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:
When I build the .lss contains:
- Log in or register to post comments
TopCool. :D
I was suprised to see this feature when I started to use AS6.
"I may make you feel but I can't make you think" - Jethro Tull - Thick As A Brick
"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc
"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3
"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
- Log in or register to post comments
TopHello. 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):
This appears to compile but produces this warning:
http://jack-kelly.com
- Log in or register to post comments
TopWell one way would be to use these symbols:
(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:
Or perhaps more rigorously:
- Log in or register to post comments
Top~~John
TWI C source code
- Log in or register to post comments
TopI 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?
- Log in or register to post comments
TopThis 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.
- Log in or register to post comments
TopThanks 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...
http://jack-kelly.com
- Log in or register to post comments
Top~~John
TWI C source code
- Log in or register to post comments
TopHere's my "new.h" file which hopefully detects whether the relevant functions are defined and, if not, defines them:
let me know if this looks wrong!
http://jack-kelly.com
- Log in or register to post comments
TopAh, 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).
http://jack-kelly.com
- Log in or register to post comments
Top~~John
TWI C source code
- Log in or register to post comments
TopI'd imagine they'd most likely be in libc.a so try avr\lib
- Log in or register to post comments
Toppls,i try to install avr-gcc compiler on my window 7 but its not installing what im i to do pls help me
- Log in or register to post comments
Top- Log in or register to post comments
TopFelix,
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.
- Log in or register to post comments
TopThis 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!
Putimas Shopping Assistant
- Log in or register to post comments
TopIf 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.
- Log in or register to post comments
TopWhat the dickens is Student-Lockroom programming?
madGambol
- Log in or register to post comments
TopPages