Society of Robots - Robot Forum

Software => Software => Topic started by: Private Reid on March 15, 2008, 10:43:10 PM

Title: C or C++
Post by: Private Reid on March 15, 2008, 10:43:10 PM
Ok, im not 100% sure that this question should be posted here, but I don't know where else it's supposed to go :-\

Can I program a robot in "C" or "C++"?
What is the difference?
 ???
Title: Re: C or C++
Post by: siempre.aprendiendo on March 16, 2008, 03:25:59 AM
They are two different languages, but the basic syntax of C++ (as Java, C#,...)  is very similar to C.

The C language (http://en.wikipedia.org/wiki/C_language) support the structured programming (http://en.wikipedia.org/wiki/Structured_programming) paradigm.
C++ (http://en.wikipedia.org/wiki/C%2B%2B) (and Java, C# and others) support object oriented programming (http://en.wikipedia.org/wiki/Object-oriented_programming) and structured programming. Some features of C++ (exceptions, STL library,...) require more memory than C, so usually is not fully supported (http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_cplusplus) by the compilers used to program microcontrollers.
Title: Re: C or C++
Post by: Kohanbash on March 16, 2008, 08:49:20 AM
You can use either one.

If you are using a micro than you probably want C.
If you have a computer on board than you can use either (C++ is better for advanced navigation/planning code)
Title: Re: C or C++
Post by: JesseWelling on March 16, 2008, 12:41:54 PM
Keep in mind that even if you don't have to it is still a good idea to follow object oriented design while using C. At work we build it up in levels.


It may seem like a waste of time, but once you get the Hardware Layer and some of the Signal layer done, making your second Robot will go a lot faster.


Title: Re: C or C++
Post by: Webbot on March 18, 2008, 12:14:09 PM
So if I want to program in C++ using AVR Studio then does anyone know what I need to change in my makefile etc. Had a look at the AVR Freaks web site but there doesn't appear to be a simple answer. The free gnu compiler appears to have come from the Unix world so a lot of the comments are confusing for a mere Windows user. Have many years of experience with MS Visual C++ so its not the coding that confuses me - its how you get AVR Studio to work - it currently throws out errors about my class definition even for a simple 'blank' class eg
Code: [Select]
class Fred {
public:
Fred();
virual ~Fred();
}

gives the following errors in the build
Code: [Select]
Compiling: 8Mhz_Webbot.c
avr-gcc -c -mmcu=atmega8 -I. -gstabs   -O0 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -Wa,-adhlns=8Mhz_Webbot.lst  -std=gnu99 -Wp,-M,-MP,-MT,8Mhz_Webbot.o,-MF,.dep/8Mhz_Webbot.o.d 8Mhz_Webbot.c -o 8Mhz_Webbot.o

8Mhz_Webbot.c:9: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'Fred'
make: *** [8Mhz_Webbot.o] Error 1
Build failed with 1 errors and 0 warnings...
Title: Re: C or C++
Post by: Admin on March 27, 2008, 02:23:20 PM
I'm not entirely sure, but I believe that the gnu compiler cannot handle many of the C++ commands . . . I think the problem is where you say 'public:' in your code - it doesn't recognize it.