Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Thread

Re: Mixing gcc C & C++

Re: Mixing gcc C & C++

2012-11-27 by Don Kinzer

--- In AVR-Chat@yahoogroups.com, "Chuck Hackett" <egroupscdh@...> wrote:
> I would be using a mix of C & C++.
Not a problem.  All of the prototypes for C-callable functions and all of the declarations for variables defined in C code must be within an extern "C" { } block.  That tells the compiler not to mangle their names.  The usual way to do this is to wrap blocks of function and variable declarations as below.

#if defined(__cplusplus)
extern "C" {
#endif

// C delarations here, e.g.
void foo(uint8_t);
extern uint16_t bar;

#if defined(__cplusplus)
}
#endif

This yields an include file that can be included in either C or C++.

Note, too, that the only functions defined in C++ that can be called from C are those that are not part of a class and whose declarations are within an extern "C" block.

Our ZBasic compiler generates either C or C++ code which is then compiled by the avr-gcc on the backend.  Our ZBasic System Library comprises hundreds of functions written in C and assembly language.  The only change that was required to support C++ intermediate code was to add the extern "C" block described above.

As far as symbolic debugging is concerned, C++ name mangling *may* be an issue.

Don Kinzer
ZBasic Microcontrollers
http://www.zbasic.net

Mixing gcc C & C++

2012-11-27 by Chuck Hackett

I have been using gcc C (WinAVR) for a while and I am wondering at the
feasibility of moving to C++ (WinAVR).

 

In the transition (and probably forever when using libraries, etc.) I would
be using a mix of C & C++.  Is this a problem with development, function
calling sequences, symbolic debugging with AVR Studio, etc.?

 

Cheers,

 

Chuck Hackett

"Good judgment comes from experience, experience comes from bad judgment"

7.5" gauge Union Pacific Northern (4-8-4) 844
<http://www.whitetrout.net/Chuck> http://www.whitetrout.net/Chuck

 

 

 



[Non-text portions of this message have been removed]

Re: Mixing gcc C & C++

2012-11-27 by Don Kinzer

--- In AVR-Chat@yahoogroups.com, "Chuck Hackett" <egroupscdh@...> wrote:
>Is it not advisable to use C++ for embedded AVR
>projects due to excess code size, etc.?
In any particular project you may be using different subsets of C++ features.  If you choose to not use objects, essentially using C++ as a "better C compiler" you'll see no size difference at all as compared to C.

If you do use objects but you avoid virtual methods, avoid creating objects dynamically, and avoid situations that invoke copy constructors, the resulting code for non-static member functions will be very similar to code written in C that passes a structure pointer for the data element, i.e. the implicit "this" pointer serves the same purpose as the explicitly passed structure pointer in C.

Even if you do use virtual inheritance, the extra code overhead will be limited to accessing the v-tbl of the object to retrieve the address of the member function to be called plus the indirect call to it.  Note, too, that the v-tbl for each class is stored in Flash but it is copied to RAM once at startup so you'll have the extra RAM space used for each class.

You'll find "C++ code bloat" warnings on AVR Freaks from time to time and the poster is always challenged to provide an example.  So far, no one has been able to demonstrate anything other that what I've outlined above.

You're aware, I presume, that the Arduino environment is based on C++.  The Arduino code is sometimes criticized for being bloated but, as far as I can tell (and except for the issues described above) it has nothing to do with C++ itself.

Don Kinzer
ZBasic Microcontrollers
http://www.zbasic.net

RE: [AVR-Chat] Mixing gcc C & C++

2012-11-27 by Chuck Hackett

Forgot one other question:  

Is it not advisable to use C++ for embedded AVR projects due to excess code
size, etc.?

When I say "excess" code size I mean unreasonable when taken in light of the
advantages of C++ for encapsulation, etc., etc.  

I realize that the answer to this is different for different circumstances
but just looking for a general guideline.  I am willing to buy a larger
processor to gain the advantages of C++ but I realize that there is a limit
to this because of the limits of AVR memory space.

I also realize that this is an area like ASM .vs. C: you can usually
generate more compact and efficient code using ASM but there are advantages
to using a higher level language such as C and each person/project has to
weigh the pros & cons.

I'm looking for comments such as one would make advising someone not to use
"printf" in embedded projects (pulls in a lot of code and increases stack
usage) - I use it for formatting debugging messages, etc. but realize that I
might have to give it up if I get close to the code space limit.
 
Cheers,

Chuck Hackett
"Good judgment comes from experience, experience comes from bad judgment"
7.5" gauge Union Pacific Northern (4-8-4) 844
http://www.whitetrout.net/Chuck

Re: [AVR-Chat] Mixing gcc C & C++

2012-11-27 by Martin McKee

Chuck,
The question of if C++ causes too much bloat for embedded has been hashed
out numerous times on avrfreaks.  The verdict -- in most cases it shouldn't
make the least bit of difference... given equal functionality.  Some things
that cost more are:
1) Templates... sometimes.  I've used templates extensively to allow for
safe ( compared to preprocessor magic ) compile-time polymorphism and have
suffered NO expansion in code size or execution time compared to a direct C
implementation.  In other cases, templates can lead to extra program memory
usage, though, still, the same execution speed.  Templates seem to be
pretty easy for the optimizer to grok.

2) Virtual functions.  Run-time polymorphism requires an indirect function
call ( and commensurate increase in execution time ) and storage of the
vtable.  The indirect function call would cost the same as if the
polymorphism were implemented directly in C with function pointers, so it's
not really an extra cost.  Besides, the C++ polymorphism is so much cleaner
than a hacked together "polymorphic" C that it is difficult to argue
against C++ when polymorphism is desired.  One annoying issue with vtables
( on the AVR ) is that they sit in RAM and, thus, use up an already
precious resource.  Still vtables are small and not using virtual methods
gets rid of the cost entirely anyway.

3) Temporary variables.  There are times that temporary variables sneak in
and disrupt the optimizability of code.  Understanding the times that C++
might create such variables usually allows for designing the code to avoid
it, but sometimes there are surprises.  Usually, however, the cost is
relatively minimal anyway.

For projects of any real size, I now use C++ exclusively on the AVR -- most
of my projects are on 8kB -16kB class devices and I rarely worry about
flash consumption.  Granted, these tend to be very focused applications,
but I've done tests comparing to similar pure C implementations and the C++
is always with 10% for memory usage and, typically, even closer, for
execution speed.

Martin Jay McKee

On Tue, Nov 27, 2012 at 8:34 AM, Chuck Hackett <egroupscdh@up844.us> wrote:

> **
>
>
> Forgot one other question:
>
> Is it not advisable to use C++ for embedded AVR projects due to excess code
> size, etc.?
>
> When I say "excess" code size I mean unreasonable when taken in light of
> the
> advantages of C++ for encapsulation, etc., etc.
>
> I realize that the answer to this is different for different circumstances
> but just looking for a general guideline. I am willing to buy a larger
> processor to gain the advantages of C++ but I realize that there is a limit
> to this because of the limits of AVR memory space.
>
> I also realize that this is an area like ASM .vs. C: you can usually
> generate more compact and efficient code using ASM but there are advantages
> to using a higher level language such as C and each person/project has to
> weigh the pros & cons.
>
> I'm looking for comments such as one would make advising someone not to use
> "printf" in embedded projects (pulls in a lot of code and increases stack
> usage) - I use it for formatting debugging messages, etc. but realize that
> I
> might have to give it up if I get close to the code space limit.
>
>
> Cheers,
>
> Chuck Hackett
> "Good judgment comes from experience, experience comes from bad judgment"
> 7.5" gauge Union Pacific Northern (4-8-4) 844
> http://www.whitetrout.net/Chuck
>
>  
>


[Non-text portions of this message have been removed]

Re: Mixing gcc C & C++

2012-11-28 by anickol

In fact, templates can lead to _less_ code size, because they allow for efficient  elimination of dead code.


--- In AVR-Chat@yahoogroups.com, Martin McKee <martinjaymckee@...> wrote:
Show quoted textHide quoted text
>
> 1) Templates... sometimes.  I've used templates extensively to allow for
> safe ( compared to preprocessor magic ) compile-time polymorphism and have
> suffered NO expansion in code size or execution time compared to a direct C
> implementation.

Re: [AVR-Chat] Re: Mixing gcc C & C++

2012-11-28 by Martin McKee

Absolutely, though I must admit, I rarely use them in places where there is
likely to be much in the way of dead code in the first place.

Martin Jay McKee

On Wed, Nov 28, 2012 at 4:40 AM, anickol <anickol@yahoo.com> wrote:

> **
>
>
>
> In fact, templates can lead to _less_ code size, because they allow for
> efficient elimination of dead code.
>
>
> --- In AVR-Chat@yahoogroups.com, Martin McKee <martinjaymckee@...> wrote:
> >
> > 1) Templates... sometimes. I've used templates extensively to allow for
> > safe ( compared to preprocessor magic ) compile-time polymorphism and
> have
> > suffered NO expansion in code size or execution time compared to a
> direct C
> > implementation.
>
>  
>


[Non-text portions of this message have been removed]

Re: Mixing gcc C & C++

2012-12-06 by Don Kinzer

--- In AVR-Chat@yahoogroups.com, "Chuck Hackett" <egroupscdh@...> wrote:
> Relay.cpp|297|warning: only initialized variables
> can be placed into program memory area
This is a known problem with a known solution, see the AVR Freaks link below.

http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=87080

In our code base, we've adapted the solution described in a slightly different way but the effect is the same.

Incidentally, in the Arduino code base the problem still exists as of v1.0.2.

Regards,

Don Kinzer
ZBasic Microcontrollers
http://www.zbasic.net

RE: [AVR-Chat] Re: Mixing gcc C & C++

2012-12-06 by Chuck Hackett

Thanks to all for the guidance on mixing gcc C & C++.  I tried converting
one of the modules within my current project and the first thing I ran into
was the fact that I had:

typedef uint8_t bool;

and C++ did not like me trying to redefine his "bool" symbol :-)  ... easily
fixed ...

Then I received the following error:

Relay.cpp|297|warning: only initialized variables can be placed into program
memory area

On the following line of code (the actual statement was buried within a
"trace" macro I use but this duplicates the error):

printf_P( PSTR( "xyz" ) ) 

After expanding the PSTR macro, it results in:

printf_P( ((const PROGMEM char  *)("XYZ")) )

Isn't PSTR creating an initialized variable?

Do I have to break this statement into two statements: one declaring the
variable and one using the variable?  If so, since this is inside a "trace"
macro how do I automatically generate unique variable names?

Obviously, all this is to prevent all trace format strings (there are a lot
of them) from consuming valuable SRAM.

For reference, the full macro is:

//
//	Trace formatter
//
//	Note: The "##" below is required so that the compiler will accept
//			"RelayBoard_Trace( fmt )" i.e.: no args after "fmt"
//
#if !defined( RelayBoard_Trace_Level )
#	error RelayBoard_Trace_Level is not defined
#endif
#if RelayBoard_Trace_Level != TL_None
#define RelayBoard_Trace_P( Level, format, ... ) \
	if (Level <= RelayBoard_Trace_Level) \
		printf_P( PSTR( format ), ##__VA_ARGS__ ) \
	else \
		;		
#else
#define RelayBoard_Trace_P( Level, format, ... ) /* Trace is turned off */
#endif
 
Cheers,

Chuck Hackett
"Good judgment comes from experience, experience comes from bad judgment"
7.5" gauge Union Pacific Northern (4-8-4) 844
http://www.whitetrout.net/Chuck

Re: Mixing gcc C & C++

2012-12-06 by Don Kinzer

--- In AVR-Chat@yahoogroups.com, "Chuck Hackett" <egroupscdh@...> wrote:
> Is there a place where one can download parts
> of it such as "std::list", etc.?
A Google search turns up several possible sources including:

http://gcc.gnu.org/libstdc++/

That said, I would be very wary of using any std:: code on a device with severely limited RAM without examining it carefully.  I would not be surprised to find a lot of heap-intensive operations and other issues that you would probably prefer to avoid.

You might post a question on AVR Freaks asking about others' experience using std:: with avr-gcc.

Regards,

Don Kinzer
ZBasic Microcontrollers
http://www.zbasic.net

RE: [AVR-Chat] Re: Mixing gcc C & C++

2012-12-06 by Chuck Hackett

> From: Don Kinzer
> ....
> This is a known problem with a known solution, see the AVR Freaks link
> below.
> 
> http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=8
> 7080
> ....

Thanks Don ...

I gather that GCC (WINAVR) does not provide support for "std::" items.  Is
there a place where one can download parts of it such as "std::list", etc.?
 
Cheers,

Chuck Hackett
"Good judgment comes from experience, experience comes from bad judgment"
7.5" gauge Union Pacific Northern (4-8-4) 844
http://www.whitetrout.net/Chuck

RE: [AVR-Chat] Re: Mixing gcc C & C++

2012-12-06 by Chuck Hackett

Can anyone recommend a good C++ book/web site for someone who knows C and
wants to transition to C++ (specifically in an AVR environment)?

I am currently working my way through "C++ Fro Programmers" by Paul J.
Deitel.  It was the only thing I could find at Barnes & Noble that didn't
look too skewed towards MS VC++ and would serve as a "reference" book (i.e.:
good index, etc.) in addition to a guide.

I use MS VC# for PC-based projects (I'm good enough to get things done but
not what I would call an expert C# programmer) so I am somewhat familiar
with OOD.
 
Cheers,

Chuck Hackett
"Good judgment comes from experience, experience comes from bad judgment"
7.5" gauge Union Pacific Northern (4-8-4) 844
http://www.whitetrout.net/Chuck

Re: [AVR-Chat] Re: Mixing gcc C & C++

2012-12-06 by Martin McKee

I would agree that it would make sense to avoid the std namespace container
classes, etc.  The problem is that they are designed to be dynamically
resizing and, thus, make use of new ( and subsequently, in most AVR, 'new'
implementations, malloc ).  Dynamic memory allocation in most any small
embedded project is a Bad Idea (tm).

One option that I have considered is to rewrite a similar set of
collections to be fixed size.  I've got a fixed size ring buffer and vector
floating around somewhere, and a bitset; they're all tested to be nearly
optimal ( certainly close enough given how much they simplify
implementation ).  But I don't know of a complete set to replace the
standard library containers.

If the standard containers were replaced though, most of standard
algorithms would be able to be used without fear of excessive cost, they do
not need the dynamic allocation.

Other things in the standard library that are designed for dynamic
allocation ( and, therefore are less than ideal for small embedded ) are
std::string and the I/O stream classes.  I also made fixed size versions of
these at one point.  While it was not nearly as optimal ( once compiled )
as the container classes above, I, at least, found it satisfying to be able
to do all the stream input and output I was used to.  The I/O streams,
however, did not really match the standard C++ stream API... can't remember
now why I broke compatibility! There was some large optimization I could
make by not doing things dynamically though... I think...

Martin Jay McKee

On Thu, Dec 6, 2012 at 9:04 AM, Don Kinzer <dkinzer@gmail.com> wrote:

> **
>
>
> --- In AVR-Chat@yahoogroups.com, "Chuck Hackett" <egroupscdh@...> wrote:
> > Is there a place where one can download parts
> > of it such as "std::list", etc.?
> A Google search turns up several possible sources including:
>
> http://gcc.gnu.org/libstdc++/
>
> That said, I would be very wary of using any std:: code on a device with
> severely limited RAM without examining it carefully. I would not be
> surprised to find a lot of heap-intensive operations and other issues that
> you would probably prefer to avoid.
>
> You might post a question on AVR Freaks asking about others' experience
> using std:: with avr-gcc.
>
>
> Regards,
>
> Don Kinzer
> ZBasic Microcontrollers
> http://www.zbasic.net
>
>  
>


[Non-text portions of this message have been removed]

Re: Mixing gcc C & C++

2012-12-08 by Don Kinzer

--- In AVR-Chat@yahoogroups.com, "Chuck Hackett" <egroupscdh@...> wrote:
> Can anyone recommend a good C++ book/web site for someone who
> knows C and wants to transition to C++
I used "C++ for C Programmers" by Ira Pohl for the initial transition.  After that, I found "Thinking in C++" by Bruce Eckel useful as well as the Scott Meyers books "Effective C++" and "More Effective C++".

None of these are specific to avr-gcc and it is quite unlikely that you'll find a book that is focused on such a narrow market.  However, Scott Meyers does have an electronic book (PDF) on the topic "Effective C++ in an Embedded Environment".  I haven't looked at this but if it is as well done as his other books I would expect it to be useful.

http://www.artima.com/shop/effective_cpp_in_an_embedded_environment

Don Kinzer
ZBasic Microcontrollers
http://www.zbasic.net

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.