GarryBoi
Posts: 7
Joined: Thu Sep 17, 2015 10:01 pm

C++

Mon Oct 19, 2015 5:05 pm

Hi,

Just wondering for a beginner like me, why C and C++ are always in the same thread? Does this mean all the C code even the basic will work on C++ or vise versa?

I really want to learn C++ but all i can read here is about C compiling when it comes on GPIO. I know some basic of how C++ work but still cant figure out how to use GPIO on codeblock or qtcreator.

If it is ok can someone please give an example here a simple blinking LED code using C++ code and how to do you run it? thanks

btw, i tried to search in google/youtube about raspberry C++ but always come up to c code. a Sample.c file always shows up not Sample.cpp

User avatar
RogerW
Posts: 285
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: C++

Mon Oct 19, 2015 5:27 pm

C++ grew out of C. In the early days the C++ compiler generated C code which was then compiled by the C compiler. C++ is an object oriented language whereas C is procedural.

Because of the way C++ has developed the C language is an almost complete subset of C++. For most purposes a C program can be considered a C++ one but not vice versa.

C is still used to write many libraries because both C and C++ programs can use a C library. A C program cannot use a C++ library.

plugwash
Forum Moderator
Forum Moderator
Posts: 3462
Joined: Wed Dec 28, 2011 11:45 pm

Re: C++

Mon Oct 19, 2015 5:32 pm

C++ is mostly an extension of C. The main things to watch out for.

1: when calling a C library from C++ code or writing C++ code that will be called by C code you have to use "extern "C"" blocks to tell the compiler it should use C calling conventions and no name mangling. For many libraries the supplied headers will do this for you.
2: C++ is stricter on type compatibility than C. This may mean you need more typecasts.

jahboater
Posts: 4762
Joined: Wed Feb 04, 2015 6:38 pm

Re: C++

Mon Oct 19, 2015 6:06 pm

Much (most) systems software is written in C including Raspbian (Linux) itself and most of the utilities. It is the natural language for this sort of thing.
While C++ is a very powerful and complex language, for a blinking LED it offers little or no advantage over C.

C++ was originally called "C with Classes". Of course it has evolved a lot since then, but it is still easy (and getting easier) to compile well written C with a C++ compiler, noting the caveats mentioned in the posts above.

Perhaps study the "C" GPIO example code. For example, look at the pigpio library. There is, on the pigpio website a file called "minimal_gpio.c" which is not too difficult to understand.
Maybe then you can decide: use a C library, or, if your application is simple enough, write it all directly in C++.

GarryBoi
Posts: 7
Joined: Thu Sep 17, 2015 10:01 pm

Re: C++

Mon Oct 19, 2015 6:48 pm

Wow thanks you guys for the quick reply.

However, can someone please post a sample of C and C++ program like a simple LED blinking. Those library still hard for me to read it. Sorry a super newbie, all i know in C++ is to use a variable and If else statement right now and i want to use this simple command and practice it with the GPIO.

I already download a lot of video tutorial that use C in GPIO but not C++ code. I just want an example how to turn ON and OFF the GPIO using C++.

EDIT:
I use "sudo python programName.py to run a simple blinking LED using python language.
How to run C++ or the .cpp in terminal? Is it sudo Cplusplus programName.cpp?

User avatar
buja
Posts: 504
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: C++

Mon Oct 19, 2015 7:37 pm

GarryBoi wrote:Wow thanks you guys for the quick reply.
EDIT:
I use "sudo python programName.py to run a simple blinking LED using python language.
How to run C++ or the .cpp in terminal? Is it sudo Cplusplus programName.cpp?
OK, if you are on this level you have some work to do. :)
C and C++ programs need to be compiled before you can run them, that is a big difference with a language like Python, that runs through an interpreter. My advice would be to read an introductory book in programming in C or C++, or try to find a decent on-line course or website to get you started with C or C++.

User avatar
DougieLawson
Posts: 36308
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: C++

Mon Oct 19, 2015 7:47 pm

Python is an interpreted language.
C/C++ are compiled languages.

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main (int argc, char *argv[])
{

 printf ("Hello World \r\n");

}
Compile that with
gcc -o h hello.c

Run that with
./h
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

alechk
Posts: 17
Joined: Thu Aug 06, 2015 7:53 am

Re: C++

Sat Oct 24, 2015 5:01 pm

I entered your example program using nano saving as hello.c, and then entered gcc -o h hello.c and got back the following:

hello.c: In function ‘main’:
hello.c:8:1: error: stray ‘\302’ in program
hello.c:8:1: error: stray ‘\240’ in program

I'm new to Linux and C. I'd be grateful for help in understanding the messages and in taking some corrective action.

alechk
Posts: 17
Joined: Thu Aug 06, 2015 7:53 am

Re: C++

Sat Oct 24, 2015 5:54 pm

small update:

Instead of copying and pasting the code I typed it in directly and got the following error:

Code: Select all

/usr/include/stdio.h:359:12: note: expected ‘const char * __restrict__’ but argument is of type ‘int’

User avatar
rpdom
Posts: 15371
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: C++

Sat Oct 24, 2015 6:01 pm

Did you use the correct double-quotes " around the character string?

Code: Select all

printf ("Hello World \r\n");
You'll get that sort of error if you use single quotes ' instead.

Code: Select all

printf ('Hello World \r\n');

alechk
Posts: 17
Joined: Thu Aug 06, 2015 7:53 am

Re: C++

Sat Oct 24, 2015 6:04 pm

Final update:

I changed void main to int main and it appeared to compile, producing the Hello World message on typing ./h

Why it works now is a problem for me. If anyone can suggest a good tutorial on using C to help me make sense of all this I'd be most grateful.

alechk
Posts: 17
Joined: Thu Aug 06, 2015 7:53 am

Re: C++

Sat Oct 24, 2015 6:43 pm

@rpdom:

I did use double quotes but I think some of the errors occurred due to my copying and pasting introducing hidden characters maybe. I found a suggestion elsewhere on the internet to change void to int and it seemed to work - why I haven't a clue!

User avatar
rpdom
Posts: 15371
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: C++

Sat Oct 24, 2015 6:48 pm

alechk wrote:@rpdom:

I did use double quotes but I think some of the errors occurred due to my copying and pasting introducing hidden characters maybe. I found a suggestion elsewhere on the internet to change void to int and it seemed to work - why I haven't a clue!
Yes, the copy paste method does sometimes introduce nasty characters :(

I'm not sure why changing main from void to int fixed the problem, although really main should return an integer status. Ideally you should put something like a return(0) at the end, I think.

I've tried the code myself, with both void and int and I can't get it to fail. Odd...

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 5105
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: C++

Sat Oct 24, 2015 6:55 pm

alechk wrote:Final update:
I changed void main to int main and it appeared to compile, producing the Hello World message on typing ./h
Why it works now is a problem for me. If anyone can suggest a good tutorial on using C to help me make sense of all this I'd be most grateful.
Being somewhat "long in the tooth"** I'm unfamiliar with any "web-based" tutorial's but, when first working with any new programming language (or variant) I have found the "Sam's Teach yourself ..." series helpful. A quick search on Amazon suggests that such can be acquired (used) for a few pounds (or, maybe borrowed from a local library).
Trev.
** having just passed 21 for the 3'rd. time ;)
Still running Raspbian Jessie or Stretch on some older Pi's (an A, B1, B2, B+, P2B, 3xP0, P0W, 2xP3A+, P3B+, P3B, B+, A+ and a B2) but Buster on the P4B's. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

User avatar
DougieLawson
Posts: 36308
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: C++

Sat Oct 24, 2015 6:58 pm

That's due to a cut'n'paste error. You've introduced some wonky characters by editing things using Windows.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: C++

Sat Oct 24, 2015 9:32 pm

I'm just struggling with C and C++ and Geany by my own.

here are some of my experiences and tortures, I hope you can read German or use Google translate successfully:

http://www.mindstormsforum.de/viewtopic ... 689#p67777 Geany IDE

http://www.mindstormsforum.de/viewtopic ... 689#p67773 wiringPi, pigpio installation

http://www.mindstormsforum.de/viewtopic ... 689#p67778 wiringPi, pigpio examples

http://www.mindstormsforum.de/viewtopic ... 689#p67771 ANSI C

http://www.mindstormsforum.de/viewtopic ... 689#p67772 C++

http://www.mindstormsforum.de/viewtopic.php?f=78&t=8689 TOP

but to be honest, it's absolutely frustrating... :|
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
AndyD
Posts: 2331
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: C++

Sun Oct 25, 2015 12:55 am

DougieLawson wrote:

Code: Select all

void main (int argc, char *argv[])
That is not a valid signature for the main function in a C program. The only valid signatures for the main function in C99 are int main(void) or int main(int argc, char *argv[]) (or equivalent).

Code: Select all

#include <stdio.h>

int main(void)
{
    printf("Hello World\n);

    return 0;
}

User avatar
DougieLawson
Posts: 36308
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: C++

Sun Oct 25, 2015 8:20 am

If my main function doesn't set a return code then it won't return an int, therefore void reflects exactly what the code is doing. I don't care about your stinking standards.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
GerardWassink
Posts: 103
Joined: Sun Aug 02, 2015 5:57 pm
Location: Ulrum (Gr), Netherlands
Contact: Website

Re: C++

Sun Oct 25, 2015 8:41 am

alechk wrote:Final update:
... If anyone can suggest a good tutorial on using C to help me make sense of all this I'd be most grateful.
An entry level tutorial can easily be found I guess, as mentioned earlier in this thread. When I want to know something, I always return to the famous book "The C programming language", the original "C-Bible" by Kernigan and Ritchie.

HTH,
Gerard
---------ooooO----- \\\\\|/// -----Oooo--------
Hacker on ELF-II, ZX80/1, Commodore 64, 8080, x86,
IBM 370 family mainframes
Machine code! Assembly! C good second.
Running Pi's with Hercules and S/370 OS's

-------------oooO-----------Oooo-------------

User avatar
PeterO
Posts: 5067
Joined: Sun Jul 22, 2012 4:14 pm

Re: C++

Sun Oct 25, 2015 9:05 am

DougieLawson wrote:. I don't care about your stinking standards.
So have you've written your own C compiler to compile Lawson's-C ? :D
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: C++

Sun Oct 25, 2015 9:18 am

IMO that's a quite OT discussion, not suitable for beginners - can you please continue your uppish, petty, and lecturing conversation by personal messages?
For a beginner there are different things which do come first, not such a rediculous nitpicking!

(edit:
wish yourself an int for Christmas and drop the void , period!)
Last edited by davenull on Sun Oct 25, 2015 9:29 am, edited 1 time in total.
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: C++

Sun Oct 25, 2015 9:24 am

back to topic,
for general purposes about C and C++, to me always the cplusplus website was a real good helper, providing tutorials and language references and even feat. lots of code examples to either function, command, or expression:

http://www.cplusplus.com/doc/tutorial/

http://www.cplusplus.com/reference/clibrary/

HTH!

(admittedly this is NOT about compiler and makefile settings for the Linux gcc and g++ compilers though!)
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
PeterO
Posts: 5067
Joined: Sun Jul 22, 2012 4:14 pm

Re: C++

Sun Oct 25, 2015 9:30 am

davenull wrote:IMO that's a quite OT discussion, not suitable for beginners - can you please continue your uppish, petty, and lecturing conversation by personal messages?
For a beginner there are different things which do come first, not such a rediculous nitpicking!
You think the importance of standards is off topic do you ? You'll waste a lot of time until you realise how wrong you are !

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
buja
Posts: 504
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: C++

Sun Oct 25, 2015 9:33 am

GerardWassink wrote:
alechk wrote:Final update:
... If anyone can suggest a good tutorial on using C to help me make sense of all this I'd be most grateful.
An entry level tutorial can easily be found I guess, as mentioned earlier in this thread. When I want to know something, I always return to the famous book "The C programming language", the original "C-Bible" by Kernigan and Ritchie.

HTH,
Gerard
K&R is still a very good book, especially if you already know another programming language. It might be difficult for a real beginner, which is why I would recommend two other books:
  • C Programming - A Modern Approach, by K.N. King
  • Programming in C - A complete introduction to the C programming language, by Stephen G. Kochan
Pick either one, they are both good, and cover C99, where K&R has not been updated after C89 (no shocking differences though).

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: C++

Sun Oct 25, 2015 9:38 am

Santa, as I said,
wish yourself an int for Christmas and drop the void , period!
And now keep on-topic, please, or send me (or either one) a PM!
(otherwise you destroy the flow of information)
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

Return to “C/C++”