Best Practice C++

Hello everyone,

are there people here who write libraries or tasks in c++ on the plc?

I tried it once and realised that the Automation Studio help quickly reaches its limits.

Would you have some basic tips and tricks or best practices regarding c++ on the plc? Or maybe there are training courses from B&R on this topic.

Thank you very much for your help and best regards

Felix

1 Like

Unfortunately B&R does not have training on c/c++. If you need help with C++ the help helped me the most. If you need help with anything, post it here and I’ll try to help you with everything.

GUID
da5a2f9c-6117-4096-9a41-77b035a5df41
447487b8-1ef2-4b16-8fd3-a5053f893f1f

One import thing is to use gcc 6.3.0 and not the default 4.2. With gcc 6.3.0 you can use many of the more modern C++ patterns from C++11 and C++14.

Apart from that there is no general training or approach. It also strongly depends on what you want to solve in C++.
Do you plan to write your whole project in C++? Or do you want to only write some encapsulated C++ libraries with interfaces to IEC languages (e.g. function blocks)?

1 Like

I think there’s many C/C++ pages available in the Help, as Filip pointed out. Here’s some links to the online help:

The community has made a Visual Studio Code plugin to use Microsoft’s C/C++ IntelliSense, and it supports building Automation Studio projects.

I agree with Patrick, the newer compiler supports more modern C++ standards. I believe Automation Studio 6 will have a significant update to the GCC version - @darjiv.

I write my whole (large) project in C++. In my opinion there are no real limitations except the old gcc (which will change soon). For many libraries you’ll find example code for C in the documentation.

For best practice i think that guidelines like CERT C++ or MISRA C++ are a good reference. For other problems it would be the best to ask here.

One very notable limitation is that you CANNOT use objects from one task in another task. Each POU (task or dynamic library) has its own heap. If you create an object in 1 task, then use that object elsewhere, it will likely work in simulation, but once you run it on a machine, you can start getting page faults from malloc.

There are workarounds for this, that make the C++ heap thread safe but I don’t think it’s formally supported. Anyone that knows if that has or will changed, please chime in!

We use C++ quite a bit in libraries, but there are some challenges wrt the runtime system and applications. For example, a c++ object cannot interact directly with IO, HMI, etc. To get any information outside a task you have to have structures defined in .typ and .var files.

I really don’t understand why B&R does not offer more support for such a powerful tool as C++ certainly is.

Yes, there are several pages in Help, but the topic definitely deserves more (training?).

Personally, I would like to extend my skills and start with OOP but unfortunately, I’m not young anymore and switching between languages is not as easy as it was :-). And in this case more support could be more than helpful.

I started very timidly but stuck at the very beginning. As an old school programmer, I use to declare PLC variables and types in files (h and C files) and Help suggest there are no differences between C and C++. But there are (error 5882, _GLOBAL, _LOCAL enabled). So, I ended very disappointed.

Learning an object-oriented programming language like C++ requires not only instructions but also a lot of practical practice.
It is therefore obvious that this cannot simply be integrated into, for example, a 2-day training course.
The Automation Help explains to C++ users the specific things that need to be taken into account when using C++ on a B&R system.
see posting by Tyler Matijevich: Best Practice C++ - #4 by tmatijevich

In order to learn C++ from scratch, there are many learning contents and training programs that support you.
Here are a few links to online courses / vendors who can offer that:

https://www.udemy.com/

https://www.learncpp.com/

Hello @SchmidtF

It looks like you got some good suggestions from multiple users here, and it has been a couple of weeks since the last activity on this post. If the answers helped you with your question, can you help the next person with the same question by marking the solution on the information which helped you the most. If you still have open questions on this topic, can you provide an update?

Thank you!