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.

asked Oct 9, 2019 at 5:43
\$\endgroup\$
2
  • 2
    \$\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\$ Commented Oct 9, 2019 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\$ Commented Oct 9, 2019 at 11:13

2 Answers 2

6
\$\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.

answered Oct 10, 2019 at 5:05
\$\endgroup\$
1
  • \$\begingroup\$ "To compile C++ code, substitute calls to avr-gcc with calls to avr-g++" -- i'm not 100% sure about this specific platform, but typically gcc uses file extension to determine file type, so avr-gcc should be able to compile C++ just fine. On other platforms, g++ vs gcc changes whether libstdc++ is linked by default, so if this is not available for AVR I suspect there is actually no difference... \$\endgroup\$ Commented Dec 6, 2020 at 8:21
3
\$\begingroup\$

You can use C++ with avr-gcc but not STL. I'm writing std for avr-gcc, except the code that use dynamic memory (so you can't use std::vector or std::queue) and exceptions. You can check my implementation:

https://github.com/amanuellperez/mcu/tree/master/src/std

It compiles with avr-gcc-9.2.0.

answered Dec 5, 2020 at 23:25
\$\endgroup\$
2
  • \$\begingroup\$ sounds cool, i was working on these type of project. semester ended, so did the project. btw, thanks. maybe i'll go through the whole library and learn something new. \$\endgroup\$ Commented Dec 6, 2020 at 11:46
  • \$\begingroup\$ Sorry, re-reading my answer I wrote 'you can use std::vector', but with my library you can't because I'm not planning in implementing dynamic memory for avr. Sorry for my spelling mistake. \$\endgroup\$ Commented Dec 6, 2020 at 21:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.