-1
\$\begingroup\$

I have implemented C++ queue in my codes, those I run on my PC.

Now I'm programming an ATmega128 micro-controller to implement a .c code. Can I use that queue.

Will it work?

If not please suggest me a queue to implement.

\$\endgroup\$
  • 1
    \$\begingroup\$ no, you cannot use a C++ standard library queue in in C code. Also, a lot of the paradigms about lifetime of objects and memory handling underlying the C++ standard queue are questionable on microcontrollers to way to resource-intense for an ATmega128. C++ is not C with a bit of syntactic sugar. So, find a microcontroller C library that offers a queue. \$\endgroup\$ – Marcus Müller Oct 9 at 7:49
  • 1
    \$\begingroup\$ Using C++ on any 8 bit MCU is an extremely bad idea. Overall, in order to use C++ on any MCU, you must have a deep knowledge of how C++ code is translated to machine code, or otherwise that language will be nothing but harmful bloat. Using a legacy 8 bit MCU for new projects in the year 2019 isn't the best of ideas either. \$\endgroup\$ – Lundin Oct 9 at 11:13
3
\$\begingroup\$

It is very well possible to use C++ on the AVR. Arduino has been doing it for ages. Unfortunately, avr-g++ does not ship with a C++ standard library (the successor to the old STL), so you have to rely on C++ core language features. C++, just like C, has many features that don't map well to small microcontrollers; you need to find out which ones. For starters, exceptions, RTTI (typeid), new/delete, and anything that requires those (especially standard library containers like vector, string and queue) don't really work here. Typical microcontroller programs use ring buffers and fixed arrays instead.

On the other hand, classes (including virtual functions), templates/Metaprogramming, constexpr, proper constants, avoiding macros, function overloads and RAII can be put to good use without sacrificing efficiency.

To compile C++ code, substitute calls to avr-gcc with calls to avr-g++. Make sure to use a recent version, since C++ support is in active development.

\$\endgroup\$
0
\$\begingroup\$

avrgcc is gcc and therefore it supports c++, but you have to tell it that you're compiling c++ if you want to use C++ syntax. This may be as simple as using .c++ as your file extension, or more complex depending on what your build chain is.

\$\endgroup\$
  • 2
    \$\begingroup\$ This is a bit misleading. While you're right, avr-gcc is "just" gcc, it's also a fork and not just yet another backend for GCC, so C++ language capabilities are pretty restricted. Also, C++ has a lot of features that you'd really not use on a microcontroller – who exactly is going to manage your abort when you do exception handling? Do you really want to do resource management potentially in an ISR? don't get me wrong: You can write 8-bit-microcontrollerable C++, but it's not the "general purpose" C++ that you'd write out of free will. \$\endgroup\$ – Marcus Müller Oct 9 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.