abhi143
Posts: 125
Joined: Mon Oct 30, 2017 4:17 am

Raspberry with C++ program

Thu Jun 07, 2018 3:50 am

Hello Forum!

I bought the Raspberry Pi 3 because I love the Raspberry Pi very much.I have to make Project with C++ language. I installed Raspbian on my Pi. I want to control my Pi in C++ language. I tried to run C++ Program and I can run c++ example program on Raspberry Pi. For Example : - g++ -o program program.cpp

I would like to write the C ++ program for interacting on the hardware. I thought it's batter to do experiment with LED. I would like to Turn on/off LED on Raspberry Pi with C++ language.

I have searched on internet.. I didn't found any simple Example. From google search I think I have to install WiringPi so I have Installed WiringPi on Raspberry Pi.

I hope someone can help me with my problem?

Best regards!

Abhi143

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

Re: Raspberry with C++ program

Thu Jun 07, 2018 6:12 am

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

Heater
Posts: 9355
Joined: Tue Jul 17, 2012 3:02 pm

Re: Raspberry with C++ program

Thu Jun 07, 2018 6:20 am

I like the pigpio library by joan http://abyz.me.uk/rpi/pigpio/

Also see examples of other methods https://elinux.org/RPi_GPIO_Code_Samples including how to do it yourself and access the GPIO ports directly.

abhi143
Posts: 125
Joined: Mon Oct 30, 2017 4:17 am

Re: Raspberry with C++ program

Thu Jun 07, 2018 7:56 am

PeterO wrote:
Thu Jun 07, 2018 6:12 am
http://wiringpi.com/examples/blink/
I understand we can use any library to access GPIO pin of Hardware. The program is in c language in your link. I am looking for c++ language so that I can write program for LED , switch , motor and .so on

Heater
Posts: 9355
Joined: Tue Jul 17, 2012 3:02 pm

Re: Raspberry with C++ program

Thu Jun 07, 2018 8:00 am

You can use C libraries from C++.

You can copy that do it yourself example in C from https://elinux.org/RPi_GPIO_Code_Samples into your C++ application.

User avatar
joan
Posts: 13411
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Raspberry with C++ program

Thu Jun 07, 2018 8:03 am

Any C library can be used from C++.

The closest to what you seem to want is http://www.airspayce.com/mikem/bcm2835/

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

Re: Raspberry with C++ program

Thu Jun 07, 2018 8:05 am

abhi143 wrote:
Thu Jun 07, 2018 7:56 am
I am looking for c++ language so that I can write program for LED , switch , motor and .so on
Just for interest, why do you need C++ for that instead of C?
What features of C++ are you needing?
C is perfectly capable for GPIO - for example the libraries tend to be written in C.
For most purposes C is a subset of C++ and is generally much simpler.

abhi143
Posts: 125
Joined: Mon Oct 30, 2017 4:17 am

Re: Raspberry with C++ program

Thu Jun 07, 2018 1:28 pm

jahboater wrote:
Thu Jun 07, 2018 8:05 am
Just for interest, why do you need C++ for that instead of C?
I think there is big difference in C and C++ language. C is structural language where as C++ is object oriented language. I have read the forum post. Most of people suggest to learn c++ for Raspberry Pi. Just for my interest I want to write c++ program for LED, switch and servo motor. I searched on google but I didn't find example program so I thought I should asked here.

I just want to know how do we program with c++ language. I am looking c++ program to turn on/off Led. once I get it idea then I can write more program so I all i need is one program.

Heater
Posts: 9355
Joined: Tue Jul 17, 2012 3:02 pm

Re: Raspberry with C++ program

Thu Jun 07, 2018 1:45 pm

You have already been given many examples of how to control a LED from C++. Just follow thing links and read them.

They may well be written in C but but C++ is largely a superset of C.

If they are libraries you can use them from C++.

If they are examples of code that you can cut and paste into your program then I'm sure they will compile and work.

If you want an object oriented solution then wrap that C code in a C++ class.

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

Re: Raspberry with C++ program

Thu Jun 07, 2018 4:30 pm

abhi143 wrote:
Thu Jun 07, 2018 1:28 pm
I just want to know how do we program with c++ language.
Well if you are certain you need the power of C++ (and can cope with its hideous complexity), then its easy as there is a C++ compiler included in Raspbian.
Raspbian is the official OS for the Pi. The most commonly used languages are Python or C/C++. You do not need to install anything.

Use any editor you like (nano or vim are commonly used), and write your program - say "led.cpp" to start with.
Compile it with:
g++ led.cpp -o led
You will likely need to add the pigpio or WiringPi libraries which are also included in Raspbian.
Then run your program with
sudo ./led
and hopefully your led will flash on or off.
Last edited by jahboater on Thu Jun 07, 2018 4:44 pm, edited 1 time in total.

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

Re: Raspberry with C++ program

Thu Jun 07, 2018 4:43 pm

abhi143 wrote:
Thu Jun 07, 2018 1:28 pm
I think there is big difference in C and C++ language.
Yes indeed there is. C++ is vastly more powerful and complex than C. But as has been pointed out above, C++ includes C as a subset.
C is structural language where as C++ is object oriented language.
What is a structural language?
Mr Stroustrup is offended when you call C++ object orientated. It is in fact multi-paradigm and OO is just one aspect.
Most of people suggest to learn c++ for Raspberry Pi.
They do not. Mostly Python is recommended because it is great for beginners.
Otherwise C. You do not need the power of C++ to turn a led on and off.

This is a great book, written by the creator of C++ himself if you want to learn C++.
https://www.amazon.co.uk/C-Programming- ... stroustrup
Its a little out of date in that it only goes up to C++11, whereas the current version is C++17.
However there is more than enough to confuse you totally :)

abhi143
Posts: 125
Joined: Mon Oct 30, 2017 4:17 am

Re: Raspberry with C++ program

Thu Jun 07, 2018 5:47 pm

jahboater wrote:
Thu Jun 07, 2018 4:30 pm
abhi143 wrote:
Thu Jun 07, 2018 1:28 pm
I just want to know how do we program with c++ language.
Well if you are certain you need the power of C++ (and can cope with its hideous complexity), then its easy as there is a C++ compiler included in Raspbian.
Raspbian is the official OS for the Pi. The most commonly used languages are Python or C/C++. You do not need to install anything.

Use any editor you like (nano or vim are commonly used), and write your program - say "led.cpp" to start with.
Compile it with:
g++ led.cpp -o led
You will likely need to add the pigpio or WiringPi libraries which are also included in Raspbian.
Then run your program with
sudo ./led
and hopefully your led will flash on or off.
That's all I have already explain in my first post. I think I couldn't explain my question very well..
Fine I will try some of program

ghans
Posts: 7755
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: Raspberry with C++ program

Thu Jun 07, 2018 6:01 pm

Uhh ...

Code: Select all

g++ -lwiringPi led.cpp -o led
is the correct compilation command. Don't forget to link against WiringPi !

abhi143, do you know what linking is ?

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

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

Re: Raspberry with C++ program

Thu Jun 07, 2018 6:16 pm

ghans wrote:
Thu Jun 07, 2018 6:01 pm
is the correct compilation command. Don't forget to link against WiringPi !
Yes, my bad! I did say:
"You will likely need to add the pigpio or WiringPi libraries which are also included in Raspbian."
and should have said how :)

julyjim
Posts: 117
Joined: Tue Jan 31, 2017 5:04 am

Re: Raspberry with C++ program

Sun Jun 10, 2018 12:16 am

I'll probably get flamed for saying this, but...
to my best knowledge there are no pure C++ "libraries" to START with.
However, if you are serious about learning programming in C++ do not start with I/O hardware interfacing.
Also using "commands" is not C++ programming.
Find IDE of some substance and "write " simple "hello word" in C++.
I am pretty happy with Eclipse , but I would not recommend the "version" ported to Raspbian - it is OK but outdated. Actually the Eclipse I am using has "canned" C++ "Hello world" build- in.
If your really want to "blink LED" - there are many copies of "GPIO" "library" to do that - but in C.
I have spent few months "porting" it to C++, and learn a lot about BCM2835 processor hardware in the process.
At present the code is still "under construction " and not open to public scrutiny.

Do not let naysayers to discourage you from picking to learn / use C++.
"inheritance " is not four letter dirty word - it is a challenge to manage it.
I cannot wait to start "coding" for multiple processors.

Best of luck to you.

Heater
Posts: 9355
Joined: Tue Jul 17, 2012 3:02 pm

Re: Raspberry with C++ program

Sun Jun 10, 2018 12:37 am

"inheritance " is the tip of the iceberg when thinking of the mind numbing vastness and complexity of C++. See thread on dynamic arrays a bit further down this forum section.

Of course there are pure C++ libraries. That is what std:: is. What about boost and asio? But no matter, we can use C libraries from C++ anyway.

Why would one not start with I/O interfacing when learning C++? Kids new to the Arduino do it every day. For them the "Hello World" program is quite likely flashing a LED.

abhi143
Posts: 125
Joined: Mon Oct 30, 2017 4:17 am

Re: Raspberry with C++ program

Sun Jun 10, 2018 6:51 am

julyjim wrote:
Sun Jun 10, 2018 12:16 am

However, if you are serious about learning programming in C++ do not start with I/O hardware interfacing.
Also using "commands" is not C++ programming.
Find IDE of some substance and "write " simple "hello word" in C++.

If your really want to "blink LED" - there are many copies of "GPIO" "library" to do that - but in C.
I have spent few months "porting" it to C++, and learn a lot about BCM2835 processor hardware in the process.
At present the code is still "under construction " and not open to public scrutiny.

Best of luck to you.
I am not too good but I am not too bad in c++ programming. I did practice on GCC compiler for basic learning . I have programmed node mcu esp8266 using arduino ide in c++ language.

That's why I was looking for the example

1dot0
Posts: 430
Joined: Mon Nov 28, 2016 12:31 pm

Re: Raspberry with C++ program

Sun Jun 10, 2018 2:27 pm

abhi143 wrote:
Sun Jun 10, 2018 6:51 am

I am not too good but I am not too bad in c++ programming. I did practice on GCC compiler for basic learning . I have programmed node mcu esp8266 using arduino ide in c++ language.

That's why I was looking for the example
As a user of Arduino (Mega, Due, esp8266) and the Raspi I daresay that there are worlds in between the simplified Arduino Sketch C++ and "real" C++ for Linux. As already stated, there is no all-in-one IDE and API to Pi like Arduino for microprocessors.

[spoiler]
(actually there is a way to install the Arduino framework for the Pi, i.e. to use the Pi like an Arduino microprocessor:
https://github.com/me-no-dev/RasPiArduino,
but I wouldn't recommend that ftm, because it is no widely used).
[/spoiler]


IMO you should start with simple C programs, featured by wiringPi for GPIO control, and it wouldn't harm if you compile them by the g++ compiler from the start, not by gcc. It will also introduce possibilities to use integrated C++ code additionally, right from the beginning or perhaps anytime in the future (that's how I am doing).
For the very 1st steps I would advise you to start with this tutorial on C for the Raspi (revised edition):
http://fractal.math.unr.edu/~ejolson/pi ... dified.pdf
and then proceed to examples given on wiringPi tutorial websites.

Just to mention:
AFAIK, the Arduino API functions digitalRead, digitalWrite and analogWrite (actually pwm write for digital pins) are basically also implemented by C functions, not C++, whilest they can be wrapped by the general Arduino Wiring C++ classes.
The same would it be if you use the comparable Raspi C functions digitalRead, digitalWrite and (soft)PwmWrite ("analogWrite") of the wiringPi C API libs, also wrappable by C++.

PS,
BTW, as already written before, wiringPi is already pre-installed in Raspbian, but you mentioned that you installed it a 2nd time.
Did you do that by apt-get install or a different way? (re-installing by the wrong command may cause malfunctions!)

Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 4 guests