espressif / arduino-esp32

Arduino core for the ESP32
GNU Lesser General Public License v2.1
13.42k stars 7.37k forks source link

Preprocessor Macros lead to compiler errors #9321

Closed insunaa closed 7 months ago

insunaa commented 7 months ago

Board

LilyGo TTGO LoRa32

Device Description

Just a TTGO

Hardware Configuration

Not relevant

Version

latest master (checkout manually)

IDE Name

PlatformIO

Operating System

Linux

Flash frequency

not relevant

PSRAM enabled

yes

Upload speed

not relevant

Description

In cores/esp32/binary.h

#define B1 1

Causes compilation issues when Arduino.h is included in any project where the literal B1 already means something.

With newer versions of fmtlib/fmt

problems are also caused by

#define F(string_literal) (FPSTR(PSTR(string_literal)))

Example: using&including fmtlib/fmt where it's used as a template typename

template <bool B = false> constexpr auto count() -> size_t { return B ? 1 : 0; }
template <bool B1, bool B2, bool... Tail> constexpr auto count() -> size_t {
  return (B1 ? 1 : 0) + count<B2, Tail...>();
}

Sketch

#include <fmt/core.h>
#include "Arduino.h"

Debug Message

~/.platformio/packages/framework-arduinoespressif32/cores/esp32/binary.h:31:12: error: expected '>' before numeric constant
.pio/libdeps/TTGO-MQTT-lora32-v1/fmt/include/fmt/core.h:1118:58: error: redefinition of 'template<bool <anonymous> > constexpr size_t fmt::v8::detail::count()'

Other Steps to Reproduce

Could this be handled by an enum instead?

namespace arduino {
    enum Binary : uint32_t {
        B0 = 0,
        B00 = 0,
        B000 = 0,
        B0000 = 0,
        B00000 = 0,
        .
        .
        .
    };
};

I have checked existing issues, online documentation and the Troubleshooting Guide

Jason2866 commented 7 months ago

For what is this file there in general?

insunaa commented 7 months ago

I have absolutely no idea, but it's there in the reference Arduino API, too. Tho they use namespaces and enums appropriately: https://github.com/arduino/ArduinoCore-API/blob/master/api/Binary.h

Jason2866 commented 7 months ago

Why not open a PR and replace the current with the one you linked?

insunaa commented 7 months ago

Because I don't know if it will break things. I don't usually develop for Arduino. It doesn't break things in my own project, because in my own project I can just delete the #include "binary.h" line from Arduino.h and everything works fine, but I have no idea what the impact would be on projects that actually use these

Jason2866 commented 7 months ago

There is a reference from Arduino. This is an Arduino implementation. So the change is getting closer to the API :-) And this will go in v3.0.0 which has many breaking changes...