错误:'constexpr' 未命名类型 m-arduino ide [英] error: 'constexpr' does not name a type m- arduino ide

查看:47
本文介绍了错误:'constexpr' 未命名类型 m-arduino ide的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Arduino 1.0.5-r2 IDE 编译此 C++ 代码

I'm trying to compile this C++ code using the Arduino 1.0.5-r2 IDE

#include <cstdint>
#include "mcal_reg.h"

class led
{
public:
  // Use convenient class-specific typedefs.
  typedef std::uint8_t port_type;
  typedef std::uint8_t bval_type;

  // The led class constructor.
  led(const port_type p,
      const bval_type b) : port(p),
                           bval(b)
  {
    // Set the port pin to low.
    *reinterpret_cast<volatile bval_type*>(port)
      &= static_cast<bval_type>(~bval);

    // Set the port pin to output.
    *reinterpret_cast<volatile bval_type*>(port - 1U)
      |= bval;
  }

  void toggle() const
  {
    // Toggle the LED via direct memory access.
    *reinterpret_cast<volatile bval_type*>(port)
      ^= bval;
  }

private:
  // Private member variables of the class.
  const port_type port;
  const bval_type bval;
};

namespace
{
  // Create led_b5 on portb.5.
  const led led_b5
  {
    mcal::reg::portb,
    mcal::reg::bval5
  };
}

int main()
{
  // Toggle led_b5 in a loop forever.
  for(;;)
  {
    led_b5.toggle();
  }

包含文件 mcal_reg.h 是这样的:

And the include file mcal_reg.h is this:

  #ifndef _MCAL_REG_2011_11_04_H_
  #define _MCAL_REG_2011_11_04_H_

  #include <cstdint>

  namespace mcal
  {
    namespace reg
    {
      constexpr std::uint8_t portb = 0x25U;

      constexpr std::uint8_t bval0 = 0x01U;
      constexpr std::uint8_t bval1 = 0x01U << 1U;
      constexpr std::uint8_t bval2 = 0x01U << 2U;
      constexpr std::uint8_t bval3 = 0x01U << 3U;
      constexpr std::uint8_t bval4 = 0x01U << 4U;
      constexpr std::uint8_t bval5 = 0x01U << 5U;
      constexpr std::uint8_t bval6 = 0x01U << 6U;
      constexpr std::uint8_t bval7 = 0x01U << 7U;
    }
  }

#endif // _MCAL_REGISTERS_2011_11_04_H_

}

尝试编译导致以下编译错误:

Trying to compile results in the following compilation error:

mcal_reg.h:17: error: 'constexpr' does not name a type

参考这一行:constexpr std::uint8_t portb = 0x25U;

referring to this line: constexpr std::uint8_t portb = 0x25U;

我在包含 mcal_reg.h 文件的库文件夹中设置了一个 mcal_reg 目录.这是我的第一个 Arduino 项目,我正在编写一个要闪存到独立 AVR 芯片的程序.但是我无法编译这个程序.我的系统是 Windows 7.我只安装了 Arduino IDE 附带的软件.(没有单独的 GNU,Visual Studio 女士...等)请帮忙.

I have setup a mcal_reg directory in my libraries folder which contains the mcal_reg.h file. This is my first Arduino project and I'm writing a program to be flashed to a standalone AVR chip. But I can't compile this program. My systems is Windows 7. and I only have the software that came with the Arduino IDE installed. (No separate GNU, Ms Visual Studio...etc) Please help.

推荐答案

要使用 constexpr 等 C++11 功能,您需要将 IDE 更新到当前测试版 (http://arduino.cc/en/main/software#toc3).然后通过编译器标志 -std=c++11 启用 C++11 支持.

To use C++11 Features like constexpr you need to update your IDE to the currently beta version (http://arduino.cc/en/main/software#toc3). And then enable C++11 support via compiler flag -std=c++11.

要添加编译器标志,找到正确的platform.txt(参见此处) 和然后添加/更改为

To add a compiler flag find the right platform.txt (see here) and then add/change to

## Compiler global definitions
compiler.path={runtime.ide.path}/tools/avr/bin/
compiler.c.cmd=avr-gcc
compiler.c.flags=-c -g -Os -w -ffunction-sections -fdata-sections -MMD -std=c++11

这篇关于错误:'constexpr' 未命名类型 m-arduino ide的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆