2

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();
  }

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

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

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.

8
  • 1
    What compiler are you using? And please update the question with the command line that the IDE is invoking the compiler with. Commented Jul 19, 2014 at 21:31
  • It is possible for you to update your IDE to the beta version arduino.cc/en/main/software#toc3? Your version seems to not support C++11. Commented Jul 19, 2014 at 21:34
  • I don't know what compiler I am using. I'm new to Arduino and was poking around the install directory but don't know what to look for. Yes I can try the update. I downloaded this IDE from Arduino but it seems very outdated compared to the beta you linked to. Commented Jul 19, 2014 at 21:47
  • Using thew 1.5.7 IDE results in: fatal error: cstdint: No such file or directory Commented Jul 19, 2014 at 21:55
  • I found this which suggests it cannot be done with within the Arduino IDE: stackoverflow.com/questions/16224746/… Commented Jul 19, 2014 at 22:03

1 Answer 1

4

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.

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.