Author Topic: C++ for the Embedded Programmer  (Read 27060 times)

0 Members and 1 Guest are viewing this topic.

Offline andyturk

  • Frequent Contributor
  • **
  • Posts: 895
  • Country: us
Re: C++ for the Embedded Programmer
« Reply #75 on: May 01, 2018, 11:10:38 am »
Can I ask a question and you help me?

  I've just started to play around with some STM32 (I bought a few bluepills and a discovery) and after a few days I have the setup running for C, STM32CubeMX to generate the projects including FreeRTOS, SW4STM32 IDE (On OSX) and ST programer to load the bin files to the board.

  Now the question, how do I go from this to C++? I don't have any against it (just know less C++ than C but I guess that is solved with time) My question is about the programming environment, how to set up C++ IDE, libraries to get it up and running, an RTOS, etc. Getting it to work with C was ok for me, it did took some time to get all running on OSX but it's running.

JS
It may be as simple as just renaming your .c files to something like .cc or .cpp. That will tell the compiler you're using now to treat your existing source code as C++. You'll probably see a bunch of new warnings and maybe some errors.

Ideally, code should compile clean (i.e., zero warnings, zero errors) with all the compiler warnings enabled. It's likely that all the library code you're using with C right now is already set up to compile correctly in a C++ environment.

Your code should (eventually) work the same way as C++ as it did as C, but one significant difference is the names that end up in the object files will be "mangled". This usually isn't a problem in practice because the debugger/IDE can demangle names it finds in the object code.

Once you've got everything converted over, just start small. Try a namespace. Create a simple class. The first embedded project is often a "blinky" app. How would you do it differently with C++?

Here's one attempt:

Code: [Select]
#pragma once

#include "panamint/rtx5/thread.h"
#include "panamint/rtx5/kernel.h"
#include "customer/nrf52/pin.h"

namespace customer {
namespace project {
class BlinkThread : public panamint::rtx5::SizedThread<400> {
public:
    using Pin       = customer::nrf52::Pin;
    using timeout_t = panamint::rtx5::timeout_t;

    BlinkThread(Pin &led, timeout_t on_time, bool on_state)
        : SizedThread()
        , led_{led}
        , on_state_{on_state}
        , on_time_{on_time}
        , off_time_{panamint::rtx5::msec_to_ticks(1000) - on_time_} {}

    virtual void run() override {
        while (true) {
            led_ = on_state_;
            delay(on_time_);
            led_ = !on_state_;
            delay(off_time_);
        }
    }

protected:
    Pin &           led_;
    const bool      on_state_;
    const timeout_t on_time_;
    const timeout_t off_time_;
};

} // namespace project
} // namespace customer
 

Offline JS

  • Frequent Contributor
  • **
  • Posts: 947
  • Country: ar
Re: C++ for the Embedded Programmer
« Reply #76 on: May 06, 2018, 01:48:01 am »
It may be as simple as just renaming your .c files to something like .cc or .cpp. That will tell the compiler you're using now to treat your existing source code as C++. You'll probably see a bunch of new warnings and maybe some errors.

Ideally, code should compile clean (i.e., zero warnings, zero errors) with all the compiler warnings enabled. It's likely that all the library code you're using with C right now is already set up to compile correctly in a C++ environment.

Your code should (eventually) work the same way as C++ as it did as C, but one significant difference is the names that end up in the object files will be "mangled". This usually isn't a problem in practice because the debugger/IDE can demangle names it finds in the object code.

Once you've got everything converted over, just start small. Try a namespace. Create a simple class. The first embedded project is often a "blinky" app. How would you do it differently with C++?
Thanks! I'll try tonight and come back.

So C projects should work as C++?

Where do I get the libraries to work with STM32F1? the official libraries are for C, or should I rename the libraries as well?

Thanks again!
JS
If I don't know how it works, I prefer not to turn it on.
 

Offline andyturk

  • Frequent Contributor
  • **
  • Posts: 895
  • Country: us
Re: C++ for the Embedded Programmer
« Reply #77 on: May 06, 2018, 02:32:07 am »
Thanks! I'll try tonight and come back.

So C projects should work as C++?

Where do I get the libraries to work with STM32F1? the official libraries are for C, or should I rename the libraries as well?

Thanks again!
JS
Yup, you should be able to compile a C project (with slight modifications) with a C++ compiler. Of course, the closer your C code is to being standard compliant (e.g., C99), the better. If you've got a lot of implicit conversions between integers and pointers for instance, C++ won't like that.

When you "convert" your own source code to C++, you'll still keep using the OEM libraries in their original form. C and C++ are compatible that way.
 

Offline JS

  • Frequent Contributor
  • **
  • Posts: 947
  • Country: ar
Re: C++ for the Embedded Programmer
« Reply #78 on: May 06, 2018, 03:38:50 am »
Ok, 46 errors and 2 warnings. 44 of them "(blah blah) could not be resolved" (first blah is field, symbol or type, second some designator) The other two errors were invalid argument. The two warnings also overlap with two errors. I'm using STM HAL libraries here so I don't know if C++ compiler it's missing them or what, I copied a working C project, renamed the sources and tried to run it.

Rebuild the index and errors go away, it does compile, but the compiled file doesn't work as it did when it was a C project.
Yup, you should be able to compile a C project (with slight modifications) with a C++ compiler. Of course, the closer your C code is to being standard compliant (e.g., C99), the better. If you've got a lot of implicit conversions between integers and pointers for instance, C++ won't like that.

When you "convert" your own source code to C++, you'll still keep using the OEM libraries in their original form. C and C++ are compatible that way.
What kind of modifications should I expect to need? My code is automatically mostly generated by the STM32cubeMX, I added a line or two to get it started doing something (driving a PWM pin)

JS

PS:I fell like highjacking the thread, should I start a new one asking for help in this and leave a link?
If I don't know how it works, I prefer not to turn it on.
 

Offline andyturk

  • Frequent Contributor
  • **
  • Posts: 895
  • Country: us
Re: C++ for the Embedded Programmer
« Reply #79 on: May 06, 2018, 04:13:15 am »
If you push your project up to a git repo somewhere (e.g., github, bitbucket, etc.) I'll take a look.
 

Offline JS

  • Frequent Contributor
  • **
  • Posts: 947
  • Country: ar
Re: C++ for the Embedded Programmer
« Reply #80 on: May 06, 2018, 01:23:18 pm »
If you push your project up to a git repo somewhere (e.g., github, bitbucket, etc.) I'll take a look.
Here it is: https://github.com/CyborgJ/PWMJoaquin4.git

The only line of code I've done there is line 100 of main.c in /src
Other than that is the generated code from STM32CubeMX

JS
If I don't know how it works, I prefer not to turn it on.
 

Offline andyturk

  • Frequent Contributor
  • **
  • Posts: 895
  • Country: us
Re: C++ for the Embedded Programmer
« Reply #81 on: May 07, 2018, 04:22:31 am »
I cloned and compiled your project with ac6. It seems to build without any warnings or errors, but doesn't have any C++ code as far as I can tell.

Simply renaming main.c to main.cc produced 200+ errors. From a pure compilation perspective, I think those can be easily fixed, but perhaps ac6 is confused somehow. I'm not an expert in Eclipse/AC6 (and don't want to become one either).

I sent you a pull request (PR) with some changes and an example C++ class that's called from main().
 

Offline JS

  • Frequent Contributor
  • **
  • Posts: 947
  • Country: ar
Re: C++ for the Embedded Programmer
« Reply #82 on: May 07, 2018, 06:43:53 am »
I cloned and compiled your project with ac6. It seems to build without any warnings or errors, but doesn't have any C++ code as far as I can tell.

Simply renaming main.c to main.cc produced 200+ errors. From a pure compilation perspective, I think those can be easily fixed, but perhaps ac6 is confused somehow. I'm not an expert in Eclipse/AC6 (and don't want to become one either).

I sent you a pull request (PR) with some changes and an example C++ class that's called from main().

  Thanks for all the support! That's right, just C, one of the first examples I managed to run on the µC from Cube.

  For what I can see, with C++ is not getting the same paths for linked resources, and for some reason I can't fix those as I do with C, seems to ignore the project linked paths as I add the necessary ones and just keep the same errors.

   I'll take a look for the pull request and come back

JS
If I don't know how it works, I prefer not to turn it on.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf