How to Compile a C++ code in atmel studio?

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

Hello there,

I was wondring how could i compile a C++ code to my atmega16 using atmel studio 7 if you know a direct test way it would be helpful

Last Edited: Fri. Jul 13, 2018 - 10:16 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

See the text in the Output tab? Don't just take pictures of (part of) it. Select the entire text then "Copy" it, finally paste it into a post here. Clearly there's an error in the bit not shown that says what you have done wrong.

 

One things is for sure, as long as you use:

char name[2];

then the line:

omar.name = "omar";

it is wrong for many reasons (some of which apply to both C and C++). A char[2] cannot hold the 5 characters of "omar" (that is 4 characters and one very important 0x00 terminator). So you would want it to be at least [5] anyway. But also neither C nor C++ just don't allow assignment to char arrays using '=' you can't just use:

= "omar";

You would need to use:

#include <string.h>

  strcpy(omar.name, "omar");

though a "safer" version of that is:

  strncpy(omar.name, sizeof(omar.name), "omar");

If you want to use direct assignment with = then this is where C++ starts to get "better" than just C as it comes with a standard template library and one of the standard types in that is std::string so:

class human {
   int age;
   std::string name;
};

...

   omar.name = "omar";

will work OK. The std::string type will "grow" to hold as many characters as you assign to the string. std:string supports '=' because it overloads (reimplements) the meaning of the '=' operator - something possible in C++ but not in C.

 

Just one thing though. The C++ for AVR does NOT have the standard template library. So if you are doing this to learn C++ then don't do it on an AVR. Just learn C++ on a PC using Eclipse or Code::Blocks or Visual Studio and use them to write "PC programs" in which case every feature of C++ will be available to you.

 

(if you DID want to do it for AVR then there are some 3rd party attempts to implement STL which will add things like std::string and std::vector etc but you have to be careful as things like std::string "growing" is done by malloc/realloc and those things don't necessarily work well on limited RAM AVR chips - which is part of the reason STL is not implemented by default in the first place!)

Last Edited: Fri. Jul 13, 2018 - 10:13 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is not a tutorial - this is a question! Therefore should be in has now been moved to the Atmel Studio section.

 

As clawson said, your screenshot doesn't show important details. It's generally better to post the code and the error list by copy & paste - rather than as pictures.

See Tip #1 for instructions.

 

One thing that would be useful as an image is a full view of your project.

 

No need to post an entire screenshot - just clip that piece.

 

Did you watch the Atmel Studio Getting Started videos - as previously advised?

 

 

EDIT

 

has now been moved

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Fri. Jul 13, 2018 - 10:47 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
This is not a tutorial - this is a question! Therefore should be in the Atmel Studio section.
Ah missed that fact - now moved out of the Tutorial forum.

 

@Omar, until you have become skilled in some area of AVR programming and are ready to post a tutorial article that will teach others about some technique you should not post in the Tutorial forum. It's not a location to ask to be tutored. It;s a location to post articles that will tutor others. The Forum description makes this very clear, as does the sticky thread at the top of the forum.

 

In future if you have questions specifically about tiny/mega AVR then post in the Tiny/Mega forum. If it's about general programming of AVR then post in the Copiler/Programming forum. If it's about the use of Ateml Studio then post in the Studio forum and so on.

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

I don't know if an existing project can be converted.  I wanted to add some C++ code to an Atmel example a couple of weeks ago.  I started a new project that duplicated the old one.

 

When you start a new project apparently you need to select C++.  That puts both the C and C++ toolchains in the project so you can use either one.