Peninsula_Pi
Posts: 11
Joined: Sun Sep 27, 2020 6:55 am

Learning C++ using PI 4

Sun Sep 27, 2020 7:37 am

Hi all,

I’m new to this forum,
new to PI’s,
and new to C++.


So please take this into account by writing your answers .
Because I can’t find any topic solving my question, I now start this topic.

Some days ago, I start learning C++ using online tutorials.
This works fine and I start get use to the way of programming in this language.

For coding I’m using the Eclipse IDE and GNU C++ Compiler on a macOS.
For future task I will need to write C++ w/o access to the macOS but with access to windows.
As far as I understand, there will be differences in the syntax switching the systems and also maybe the Compiler or IDE.
This is why I would like to use a PI as a kind of take away C++ box that I can take everywhere where I will find an in- and output-solution (e.g. keyboard, display, …).


Could you recommend a PI for this approach and should I take another PI than the last one into account for this task?

Thanks in advance!
ciao

User avatar
jahboater
Posts: 6287
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Learning C++ using PI 4

Sun Sep 27, 2020 11:10 am

Peninsula_Pi wrote:
Sun Sep 27, 2020 7:37 am
For future task I will need to write C++ w/o access to the macOS but with access to windows.
As far as I understand, there will be differences in the syntax switching the systems and also maybe the Compiler or IDE.
C++ itself is a portable high level language, you shouldn't have any problems. Obviously if you use a different graphics library on different machines say, then its a different matter. I believe you should be able to run Eclipse on the Pi4, but its not needed of course.

The C/C++ compiler on the Pi is called GCC and it fully supports the latest standards, including early support for draft standards such as C++20. GCC is considered the gold standard in the compiler world. Note that you are probably also using GCC on the MAC, so you shouldn't see any real difference in the compiler usage apart from the Pi version likely being more recent.

On the Pi you will also find a plethora of software development tools (debugger, profilers, editors etc etc).

All this C++ support on the Pi comes pre-installed (included within the OS), so its ready to go.
The C++ compiler, GCC 8.3 is a capable version, but you can upgrade to the latest version (10.2 at the time of writing). This upgrade is done by building the compiler from source (tens of millions of lines of code) which the Pi4 handles easily. That shows how powerful the Pi4 is for software development.
Peninsula_Pi wrote:
Sun Sep 27, 2020 7:37 am
This is why I would like to use a PI as a kind of take away C++ box that I can take everywhere where I will find an in- and output-solution (e.g. keyboard, display, …).

Could you recommend a PI for this approach and should I take another PI than the last one into account for this task?
Obviously get a Pi4. A 4GB or 8GB model I would suggest. C++ takes longer to compile than C. The Pi4 is easily overclocked and when running at 2GHz or 2.1GHz is a quick development platform.
Last edited by jahboater on Sun Sep 27, 2020 11:25 am, edited 1 time in total.
Pi4 8GB running PIOS64 Lite

User avatar
jahboater
Posts: 6287
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Learning C++ using PI 4

Sun Sep 27, 2020 11:12 am

Peninsula_Pi wrote:
Sun Sep 27, 2020 7:37 am
This is why I would like to use a PI as a kind of take away C++ box that I can take everywhere where I will find an in- and output-solution (e.g. keyboard, display, …).
You may also connect to the Pi over the network from another computer, so that no keyboard or screen are needed. You get either a terminal window or a full desktop.
Pi4 8GB running PIOS64 Lite

Peninsula_Pi
Posts: 11
Joined: Sun Sep 27, 2020 6:55 am

Re: Learning C++ using PI 4

Sun Sep 27, 2020 12:47 pm

Appreciate your help!

The idea of accessing the PI from a different network is nice but will not be possible from all networks I need to access the PI and I might need the HW on my desk at the time I’m working with it :)

But display and keyboard will be accessible everywhere I'm planning to code!

First task is – of curse – learning C++ but in future projects I can think of processing measurement data (e.g. streams of current and voltage values) and/or the implementation of “simple” optimization algorithms.

Do you have some examples where to expect the limits of the PI in such projects?

Thanks again :)

dsyleixa123
Posts: 1025
Joined: Mon Jun 11, 2018 11:22 am

Re: Learning C++ using PI 4

Sun Sep 27, 2020 2:59 pm

what I don't understand yet is what your C++ learning on the Pi has to do with Windows or Mac?

ejolson
Posts: 6009
Joined: Tue Mar 18, 2014 11:47 am

Re: Learning C++ using PI 4

Sun Sep 27, 2020 3:04 pm

Peninsula_Pi wrote:
Sun Sep 27, 2020 12:47 pm
Appreciate your help!

The idea of accessing the PI from a different network is nice but will not be possible from all networks I need to access the PI and I might need the HW on my desk at the time I’m working with it :)

But display and keyboard will be accessible everywhere I'm planning to code!

First task is – of curse – learning C++ but in future projects I can think of processing measurement data (e.g. streams of current and voltage values) and/or the implementation of “simple” optimization algorithms.

Do you have some examples where to expect the limits of the PI in such projects?

Thanks again :)
I agree with the advice on getting the Pi 4B 4GB or 8GB model.

In my opinion, most of the limits of the Pi 4B when used for C/C++ development relate to the use of an SD card to hold the root filesystem. This can, in part, be mitigated by using an A1 class SD card as sold by SanDisk. Another possibility, if you don't mind the extra clutter, is to use an external SSD.

While it may be possible to arrange a Pi and SSD in a compact case convenient to carry about, the external SSD could interfere with WiFi as all USB3 devices are wont to do. For this reason I would recommend using a non-counterfeit A1 class SD card to start out with.

User avatar
jahboater
Posts: 6287
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Learning C++ using PI 4

Sun Sep 27, 2020 4:20 pm

Peninsula_Pi wrote:
Sun Sep 27, 2020 12:47 pm
First task is – of curse – learning C++ but in future projects I can think of processing measurement data (e.g. streams of current and voltage values) and/or the implementation of “simple” optimization algorithms.

Do you have some examples where to expect the limits of the PI in such projects?
For small and simple "learning" stuff like that, there are no limits specific to the Pi.

The Pi4 has its GPIO which is an option not available on PC's and MAC's. Processing measurement data is a popular use of the Pi.

The biggest program you will likely run is actually the C++ compiler!
The Pi4 is perfectly capable of building truly huge programs like the Linux kernel, QT, the GCC compiler etc.
Last time I looked, the three stage GCC build required some 51 million lines of code to be compiled. The Pi4 can do that with ease.

The time to compile these small "learning" programs would likely be measured in milliseconds.
I'm thinking little test programs of 100 lines or less.
Larger programs of a few thousand lines might take a second or so to compile. (A 12,000 line C program took 1.3 seconds on my Pi4). If you are learning C++, the example programs wont be anywhere near that large.
The Pi4 is perfectly usable!
Given the aims of the RPF charity, you could even argue that its designed for the job (helping people learn programming).

By the way, why do you want to learn C++?
Why not start with C?
C++ is a huge language, and very difficult to learn. Few, if any, people fully understand all of C++17 and its STL. C is smaller, easier to learn, and much more popular. Also if you did want to migrate to C++ later, C is mostly a subset of C++, so you can use what you have learned.

The Pi4 will have many other languages available out of the box. For example, you might like to experiment for fun with a powerful interpreted language like Python. Its all free!
Last edited by jahboater on Sun Sep 27, 2020 4:56 pm, edited 3 times in total.
Pi4 8GB running PIOS64 Lite

User avatar
jahboater
Posts: 6287
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Learning C++ using PI 4

Sun Sep 27, 2020 4:34 pm

ejolson wrote:
Sun Sep 27, 2020 3:04 pm
While it may be possible to arrange a Pi and SSD in a compact case convenient to carry about, the external SSD could interfere with WiFi as all USB3 devices are wont to do. For this reason I would recommend using a non-counterfeit A1 class SD card to start out with.
I agree.

I use these and never have performance issues.
https://www.amazon.co.uk/SanDisk-Extrem ... 7c877&th=1
The tiny micro SD card in the slot at the end is much neater and tidier than an external USB SSD.

Of course a USB3 SSD or M2 card is always an option if you want to store vast amounts of data or media files and access them faster than the micro SD card can handle.

The Pi4 models have 2GB, 4GB, or 8GB of memory. Even though your programs and compilations will likely not use that much, the system will "cache" programs and data in memory so that later access is very fast.

This means that the first time you run the program it is read off the disk, the second and subsequent times it will be in memory and will start up instantly. Having said that, in my experience most programs start instantly anyway on the Pi, even the first time. Perhaps that's because its ext4 filesystem is so efficient.
Pi4 8GB running PIOS64 Lite

dsyleixa123
Posts: 1025
Joined: Mon Jun 11, 2018 11:22 am

Re: Learning C++ using PI 4

Sun Sep 27, 2020 5:36 pm

I agree,
1st learn C and the special Linux things for that, e.g. by this tutorial
http://fractal.math.unr.edu/~ejolson/pi ... dified.pdf

then learn controlling GPIOs and i2c and UART and SPI e.g. by wiringPi (still available) or on my behalf also pigpio, both are also C-based.

I always am editing and compiling directly on the Pi (BT mouse and keyboard and 10" or 13" HDMI touchscreen or even optional a 24" HDMI monitor), crosscompiling is no option and much too cumbersome.

For C++, you can always include all your C routines and libs you already have learned (as already has been pointed out). Both for C and C++ programming I am using the Geany IDE.
But for C++ GUI programming I would suggest a convenient C++ GUI programming IDE like qt5 creator. Also here you can include all your wiringPi or pthread or whatever code or libs to create neat and pretty GUI windows applications also for sensor readings, motor control, and dashboards.
Last edited by dsyleixa123 on Mon Sep 28, 2020 11:23 am, edited 1 time in total.

Peninsula_Pi
Posts: 11
Joined: Sun Sep 27, 2020 6:55 am

Re: Learning C++ using PI 4

Mon Sep 28, 2020 7:16 am

Thanks for all your replies.

Why C++?
Has something to do with my job btw. future job opportunities -> but first fun since I like to write stuff performing math:).
But if C can easily be integrated in C++, I will think about learning C after my actual C++ course.

I'm already familiar with MATLAB and have some insights in Python. Therefore, I know how to write in these languages algorithms like optimization stuff or simulations of mobile communication systems.
But never care about programming and never learned the stuff from the beginning. In the past it was only the engineering approach.

Why windows, mac and PI? As far as I understand there will be differences in the syntax switching between these OS's because there will be different compilers ...
And as a beginner I don't want to mess around with vendor or OS specific properties or at least not more than I have to...
ejolson wrote: While it may be possible to arrange a Pi and SSD in a compact case convenient to carry about, the external SSD could interfere with WiFi as all USB3 devices are wont to do. For this reason, I would recommend using a non-counterfeit A1 class SD card to start out with.
Mhm why do they interfere with WiFi?
Is this a PI problem, USB3 standard, or what?
Never had any issues with USB3 and WiFi in the past.

ejolson
Posts: 6009
Joined: Tue Mar 18, 2014 11:47 am

Re: Learning C++ using PI 4

Mon Sep 28, 2020 7:50 am

Peninsula_Pi wrote:
Mon Sep 28, 2020 7:16 am
Thanks for all your replies.

Why C++?
Has something to do with my job btw. future job opportunities -> but first fun since I like to write stuff performing math:).
But if C can easily be integrated in C++, I will think about learning C after my actual C++ course.

I'm already familiar with MATLAB and have some insights in Python. Therefore, I know how to write in these languages algorithms like optimization stuff or simulations of mobile communication systems.
But never care about programming and never learned the stuff from the beginning. In the past it was only the engineering approach.

Why windows, mac and PI? As far as I understand there will be differences in the syntax switching between these OS's because there will be different compilers ...
And as a beginner I don't want to mess around with vendor or OS specific properties or at least not more than I have to...
ejolson wrote: While it may be possible to arrange a Pi and SSD in a compact case convenient to carry about, the external SSD could interfere with WiFi as all USB3 devices are wont to do. For this reason, I would recommend using a non-counterfeit A1 class SD card to start out with.
Mhm why do they interfere with WiFi?
Is this a PI problem, USB3 standard, or what?
Never had any issues with USB3 and WiFi in the past.
It's a general problem with USB3 and any radio device transmitting in the 2.4GHz band. See, for example

https://www.intel.com/content/www/us/en ... paper.html

User avatar
jahboater
Posts: 6287
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Learning C++ using PI 4

Mon Sep 28, 2020 8:22 am

Peninsula_Pi wrote:
Mon Sep 28, 2020 7:16 am
Why windows, mac and PI?
I think you will find its the same compiler on the Pi as it is on the MAC. Try typing "gcc -v" on the MAC and see what you get. Might be a different version of the compiler of course but that wont matter for your purposes. On Windows, you can install GCC for free (called mingw).
Peninsula_Pi wrote:
Mon Sep 28, 2020 7:16 am
As far as I understand there will be differences in the syntax switching between these OS's because there will be different compilers ...
What compiler differences? Please could you enlighten us?
Established languages like C and C++ have strict ISO standards. Any compiler should correctly implement the minimum requirements of the standard it claims to support, or it is defective. So write "standard conforming" C or C++ and you should have no trouble. Again, if you are learning C with simple test programs you will not have any issues.

GCC fully supports all the standards, so you are alright on the Pi and the MAC. You can install GCC on a PC, or use the Microsoft compiler.
The Microsoft compiler is more limited in this respect but they did recently announce support for C99 (albeit the bare minimum).
And C99 is fine for what you want.
Peninsula_Pi wrote:
Mon Sep 28, 2020 7:16 am
And as a beginner I don't want to mess around with vendor or OS specific properties or at least not more than I have to...
As far as different OS's are concerned, write standard C or C++ and you will be OK most of the time whilst learning.
The Pi OS and the MAC OS are superficially similar here (MacOS is based on an early version of BSD UNIX).
Windows is different (for example it still uses CRLF for record separators instead of just LF), but while learning C you probably wont see any real problems.

One reason why C is so popular is because it is so easy to write portable programs.
Pi4 8GB running PIOS64 Lite

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

Re: Learning C++ using PI 4

Mon Sep 28, 2020 8:34 am

I thought Macs used Clang. Not that that should make any difference.

Microsoft has always had some little extensions of their own. Just don't use them.

I do all my C/C++ on Windows using the GCC that comes with Debian. Running under the Linux Subsystem for Windows on win 10. Of course the resulting executables don't run under Windows directly but, meh, why would I want to do that.
Memory in C++ is a leaky abstraction .

User avatar
jahboater
Posts: 6287
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Learning C++ using PI 4

Mon Sep 28, 2020 8:44 am

Heater wrote:
Mon Sep 28, 2020 8:34 am
I thought Macs used Clang. Not that that should make any difference.
May well be. Its been ten years since I worked on MAC's, we used GCC then.

And yes it should not make a difference. Clang is a good compiler.
Furthermore it tries hard to be compatible with GCC.
Pi4 8GB running PIOS64 Lite

ejolson
Posts: 6009
Joined: Tue Mar 18, 2014 11:47 am

Re: Learning C++ using PI 4

Mon Sep 28, 2020 10:23 am

jahboater wrote:
Mon Sep 28, 2020 8:44 am
Heater wrote:
Mon Sep 28, 2020 8:34 am
I thought Macs used Clang. Not that that should make any difference.
May well be. Its been ten years since I worked on MAC's, we used GCC then.

And yes it should not make a difference. Clang is a good compiler.
Furthermore it tries hard to be compatible with GCC.
One way to make sure you are not using any operating system extensions is to actually compile and run the program in Macintosh, Windows and Pi to see if it works. For a beginner it is not always clear which functions are part of the C standard library and which are operating system specific. Even then, there can be minor differences that make things not work as expected. For example, is malloc signal safe on every system?

User avatar
jahboater
Posts: 6287
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Learning C++ using PI 4

Mon Sep 28, 2020 11:08 am

ejolson wrote:
Mon Sep 28, 2020 10:23 am
One way to make sure you are not using any operating system extensions is to actually compile and run the program in Macintosh, Windows and Pi to see if it works.
Yes exactly. That's what I would do. Build and test it on every platform possible.
But I was suggesting that for simple learning programs, its mostly a non-issue.
ejolson wrote:
Mon Sep 28, 2020 10:23 am
Even then, there can be minor differences that make things not work as expected. For example, is malloc signal safe on every system?
The common library functions, including the math library, are precisely specified in the standard:
C18 says about malloc:

Code: Select all

2. For purposes of determining the existence of a data race, memory allocation functions behave as
though they accessed only memory locations accessible through their arguments and not other
static duration storage.
but I don't think that's of much interest to a beginner :)
Pi4 8GB running PIOS64 Lite

Peninsula_Pi
Posts: 11
Joined: Sun Sep 27, 2020 6:55 am

Re: Learning C++ using PI 4

Mon Sep 28, 2020 2:39 pm

Good info's thanks!

I will study the USB3 issue.
EMC is wizardry anyway ... at least in the mind of the HW development :D

I will study the possibility to work on all mentioned platforms but will purchase a PI 4 anyway since I want to play with GPIOs.
There is a lot unclear in terms of the overall structure of operation systems I might need to study soon.

One last question:
PI 4
8GB RAM
SanDisk A1 class SD card
original PI power supply
case

do I forget something?

ejolson
Posts: 6009
Joined: Tue Mar 18, 2014 11:47 am

Re: Learning C++ using PI 4

Mon Sep 28, 2020 2:46 pm

jahboater wrote:
Mon Sep 28, 2020 11:08 am
ejolson wrote:
Mon Sep 28, 2020 10:23 am
Even then, there can be minor differences that make things not work as expected. For example, is malloc signal safe on every system?
The common library functions, including the math library, are precisely specified in the standard:
C18 says about malloc:

Code: Select all

2. For purposes of determining the existence of a data race, memory allocation functions behave as
though they accessed only memory locations accessible through their arguments and not other
static duration storage.
but I don't think that's of much interest to a beginner :)
That says nothing about what happens to the heap if a signal is received while malloc is allocating memory. This is not a race condition between threads but interrupt processing related to the way system calls are handled when recovering from a signal.

On another note, one of the things I had to learn when writing my first numerical codes was the big versus little endian problem when reading and writing arrays of floating point data using two different operating systems. As far as I know, this is not something that is specified by the language standard. At the time this affected portability of C code between a PC and an SGI Origin 2000. Today it mostly affects portability between AIX and Linux.

ejolson
Posts: 6009
Joined: Tue Mar 18, 2014 11:47 am

Re: Learning C++ using PI 4

Mon Sep 28, 2020 3:03 pm

Peninsula_Pi wrote:
Mon Sep 28, 2020 2:39 pm
Good info's thanks!

I will study the USB3 issue.
EMC is wizardry anyway ... at least in the mind of the HW development :D

I will study the possibility to work on all mentioned platforms but will purchase a PI 4 anyway since I want to play with GPIOs.
There is a lot unclear in terms of the overall structure of operation systems I might need to study soon.

One last question:
PI 4
8GB RAM
SanDisk A1 class SD card
original PI power supply
case

do I forget something?
I would suggest a micro HDMI cable to connect the 4B to the telly.

If that's the official case, then you'll also need a drill and a bit to bore holes in it for some unofficial ventilation. Be sure to use a bit designed for plastics.

I think many people simply mount their bare circuit boards to some convenient vertical surface using m2.5 standoffs for good convection and better cooling.

Presumably you already have a USB mouse and keyboard.

Although the original Pi used to be a bit picky about which keyboard was used, the 4B seems to work with everything.

User avatar
jahboater
Posts: 6287
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Learning C++ using PI 4

Mon Sep 28, 2020 5:12 pm

ejolson wrote:
Mon Sep 28, 2020 2:46 pm
That says nothing about what happens to the heap if a signal is received while malloc is allocating memory.
Yes, but it does appear to be mandating that the *alloc functions are "pure".
ejolson wrote:
Mon Sep 28, 2020 2:46 pm
On another note, one of the things I had to learn when writing my first numerical codes was the big versus little endian problem when reading and writing arrays of floating point data using two different operating systems. As far as I know, this is not something that is specified by the language standard. At the time this affected portability of C code between a PC and an SGI Origin 2000. Today it mostly affects portability between AIX and Linux.
Yes and I don't like AIX much, so I can use this to separately detect floating point and integer endianness ...

Code: Select all

static_assert( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__,
  "The (integer) byte order must be little endian (use -mlittle-endian)" );
static_assert( __FLOAT_WORD_ORDER__ == __ORDER_LITTLE_ENDIAN__,
  "The (float) byte order must be little endian (use -mlittle-endian)" );
:)
Pi4 8GB running PIOS64 Lite

lurk101
Posts: 289
Joined: Mon Jan 27, 2020 2:35 pm

Re: Learning C++ using PI 4

Mon Sep 28, 2020 10:02 pm

ejolson wrote:
Mon Sep 28, 2020 2:46 pm
On another note, one of the things I had to learn when writing my first numerical codes was the big versus little endian problem when reading and writing arrays of floating point data using two different operating systems. As far as I know, this is not something that is specified by the language standard. At the time this affected portability of C code between a PC and an SGI Origin 2000. Today it mostly affects portability between AIX and Linux.
All things UDP/TCP/IP/Ethernet remain big-endian, so it affects comms software for all little-endian connected systems Same for crypto.

User avatar
jahboater
Posts: 6287
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Learning C++ using PI 4

Mon Sep 28, 2020 11:42 pm

lurk101 wrote:
Mon Sep 28, 2020 10:02 pm
All things UDP/TCP/IP/Ethernet remain big-endian, so it affects comms software for all little-endian connected systems Same for crypto.
I always wondered how much faster networking would be if they had chosen the network byte order to be the right way round :)
(so that ntohl and htonl did nothing on the popular platforms)
Pi4 8GB running PIOS64 Lite

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13374
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Learning C++ using PI 4

Tue Sep 29, 2020 12:59 pm

please stay on-topic!

Return to “C/C++”