【问题标题】:How to enable C++17 in the Arduino IDE 1.8.5+如何在 Arduino IDE 1.8.5+ 中启用 C++17
【发布时间】:2019-05-11 03:37:03
【问题描述】:

所以我正在制作一个需要 std::variant 的程序,所以显然我需要 C++17 但这不是在没有编译器标志的 Arduino 中,只是我无法弄清楚要做什么标志以及如何更改编译器标志。 (顺便说一句,我使用的是 Windows,而不是 Linux(尽管我有一台 Linux 笔记本电脑))

我曾尝试使用 Arduino boost 库,但它似乎是为 C++ 而不是 Arduino 编写的,尽管它说它是为 Arduino 制作的, 我也尝试过解决方法,但它们占用了太多内存。

我希望它在 Arduino 中具有 std 或至少一个 std::variant 等价物,但我还没有找到获得它的方法。

提前致谢!

编辑:如果没有办法在 Arduino 中执行 C++17,那么有人可以链接教程以使用 C++ 编写 Arduino 板

【问题讨论】:

    标签: windows arduino stl c++17 arduino-ide


    【解决方案1】:

    使用 boost.variant 检查https://github.com/vancegroup/arduino-boost

    iostream、sstream、string、variant 和其他类似的头文件在 avr-libc 和使用 avr-g++(和 avr-libc)的 Arduino 中不可用

    你可以检查https://github.com/maniacbug/StandardCplusplushttps://github.com/mike-matera/ArduinoSTL

    对于没有已知数据类型的分配值,您可以编写您的类并查看https://github.com/radinParsaei/Value

    【讨论】:

      【解决方案2】:

      Arduino 编程语言是 C++ 的子集,有一些细微的变化。 您可以在项目中引用 .CPP 和 .H 文件,只要它们不使用 Arduino 不支持的 C++ 功能。

      我相信最终会有人发挥创造力并找到一种方法将具有 C++17 功能的代码编译成 AVR 机器代码(如果他们还没有的话)。

      但请记住,这些都是微型芯片,通常以 10-20 MHz 的频率运行,具有大约 2 KB 的工作内存。标准模板库是 C++ 的一大飞跃,但在每个字节都很重要时,它可能不是最佳选择。

      【讨论】:

      • 我已经知道这一点,但是当我尝试做#include <boost/variant.cpp> 或其他任何事情时,由于它没有一些标准库,它给出了大量的错误。我需要某种形式的 Arduino 中的 std::variant 或 boost::variant 或用于 C++17 的 Arduino 库。
      • 试试这个:github.com/maniacbug/StandardCplusplus(顺便说一句:variant 是 c++ 17 的标准,无论如何都不需要提升)但是,我敦促您重新考虑设计决策 - 因为诸如:variant 和 any 之类的东西是AVR / MCU / 低级计算的不明智选择。我会使用指针和 reinterpret_cast——如果你真的需要这样的功能(但坦率地说:我想知道你为什么要在 Arduino 中使用它?)——或者,更好的是,使用 memcpy。
      猜你喜欢
      • 2018-01-23
      • 1970-01-01
      • 2016-12-29
      • 2021-03-30
      • 2017-05-09
      • 2023-03-19
      • 1970-01-01
      相关资源
      最近更新 更多