Page 1 of 2
C++
Posted: Mon Oct 19, 2015 5:05 pm
by GarryBoi
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
Re: C++
Posted: Mon Oct 19, 2015 5:27 pm
by RogerW
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.
Re: C++
Posted: Mon Oct 19, 2015 5:32 pm
by plugwash
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.
Re: C++
Posted: Mon Oct 19, 2015 6:06 pm
by jahboater
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++.
Re: C++
Posted: Mon Oct 19, 2015 6:48 pm
by GarryBoi
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?
Re: C++
Posted: Mon Oct 19, 2015 7:37 pm
by buja
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++.
Re: C++
Posted: Mon Oct 19, 2015 7:47 pm
by DougieLawson
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
Re: C++
Posted: Sat Oct 24, 2015 5:01 pm
by alechk
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.
Re: C++
Posted: Sat Oct 24, 2015 5:54 pm
by alechk
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’
Re: C++
Posted: Sat Oct 24, 2015 6:01 pm
by rpdom
Did you use the correct double-quotes " around the character string?
You'll get that sort of error if you use single quotes ' instead.
Re: C++
Posted: Sat Oct 24, 2015 6:04 pm
by alechk
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.
Re: C++
Posted: Sat Oct 24, 2015 6:43 pm
by alechk
@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!
Re: C++
Posted: Sat Oct 24, 2015 6:48 pm
by rpdom
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...
Re: C++
Posted: Sat Oct 24, 2015 6:55 pm
by FTrevorGowen
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

Re: C++
Posted: Sat Oct 24, 2015 6:58 pm
by DougieLawson
That's due to a cut'n'paste error. You've introduced some wonky characters by editing things using Windows.
Re: C++
Posted: Sat Oct 24, 2015 9:32 pm
by davenull
Re: C++
Posted: Sun Oct 25, 2015 12:55 am
by AndyD
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;
}
Re: C++
Posted: Sun Oct 25, 2015 8:20 am
by DougieLawson
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.
Re: C++
Posted: Sun Oct 25, 2015 8:41 am
by GerardWassink
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
Re: C++
Posted: Sun Oct 25, 2015 9:05 am
by PeterO
DougieLawson wrote:. I don't care about your stinking standards.
So have you've written your own C compiler to compile Lawson's-C ?
PeterO
Re: C++
Posted: Sun Oct 25, 2015 9:18 am
by davenull
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!)
Re: C++
Posted: Sun Oct 25, 2015 9:24 am
by davenull
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!)
Re: C++
Posted: Sun Oct 25, 2015 9:30 am
by PeterO
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
Re: C++
Posted: Sun Oct 25, 2015 9:33 am
by buja
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).
Re: C++
Posted: Sun Oct 25, 2015 9:38 am
by davenull
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)