greiman / SdFat

Arduino FAT16/FAT32 exFAT Library
MIT License
1.08k stars 505 forks source link

Solution for the "definition of macro 'F' #define F(str) (str)" error in the file SysCall.h #411

Open FrizzaFava opened 1 year ago

FrizzaFava commented 1 year ago

I was using the Arduino Nano RP2040 Connect and trying to use the SdFat library with IrRemote. This problem was continuously popping in my output as the #define F in the SysCall.h file conflicted with the Mbed library necessary to rub the board I'm using (I think that this will happen on all Arduino's boards if you use even the IrRemote library). The solution i found was simply renaming the F macro with 'F_MACRO' or anything else. So, this is what i got:

Lines 50-53 in common/SysCall.h

#ifndef F_MACRO
/** Define macro for strings stored in flash. */
#define F_MACRO(str) (str)
#endif  // F_MACRO

and replacing all the F macro declarations in SdFat.h with F_MACRO, so for example:

Lines 126-140 in SdFat.h

  /** %Print error info and halt.
   *
   * \param[in] pr Print destination.
   */
  void errorHalt(print_t* pr) {
    if (sdErrorCode()) {
      pr->print(F_MACRO("SdError: 0X"));
      pr->print(sdErrorCode(), HEX);
      pr->print(F_MACRO(",0X"));
      pr->println(sdErrorData(), HEX);
    } else if (!Vol::fatType()) {
      pr->println(F_MACRO("Check SD format."));
    }
    while (true) {}
  }
greiman commented 1 year ago

The F macro is defined for all standard Arduino boards so I can't change it.

I define it for non-standard boards. I plan to remove use of F() except for AVR boards in the future.

Here are a few of Arduino compatible boards that define F:

Search "#define F(" (16 hits in 15 files of 31827 searched)
  C:\Users\Bill\AppData\Local\Arduino15\packages\adafruit\hardware\samd\1.7.10\cores\arduino\WString.h (1 hit)
    Line  38: #define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
  C:\Users\Bill\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\cores\arduino\WString.h (1 hit)
    Line  38: #define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
  C:\Users\Bill\AppData\Local\Arduino15\packages\arduino\hardware\mbed_nano\3.1.1\cores\arduino\api\String.h (1 hit)
    Line  45: #define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
  C:\Users\Bill\AppData\Local\Arduino15\packages\arduino\hardware\mbed_portenta\3.1.1\cores\arduino\api\String.h (1 hit)
    Line  45: #define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
  C:\Users\Bill\AppData\Local\Arduino15\packages\arduino\hardware\mbed_rp2040\3.1.1\cores\arduino\api\String.h (1 hit)
    Line  45: #define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
  C:\Users\Bill\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.7\cores\arduino\api\String.h (1 hit)
    Line  45: #define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
  C:\Users\Bill\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.12\cores\arduino\WString.h (1 hit)
    Line  38: #define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
  C:\Users\Bill\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.13\cores\arduino\api\String.h (1 hit)
    Line  45: #define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
  C:\Users\Bill\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.4\cores\esp32\WString.h (1 hit)
    Line  40: #define F(string_literal) (FPSTR(PSTR(string_literal)))
  C:\Users\Bill\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.0.2\cores\esp8266\WString.h (1 hit)
    Line  40: #define F(string_literal) (FPSTR(PSTR(string_literal)))
  C:\Users\Bill\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.0.2\libraries\ESP8266mDNS\src\LEAmDNS_Priv.h (1 hit)
    Line 170: #define F(A)    A
  C:\Users\Bill\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.0.2\libraries\ESP8266SdFat\src\common\PrintBasic.h (2 hits)
    Line  49: #define F(str) (reinterpret_cast<const __FlashStringHelper *>(PSTR(str)))
    Line  51: #define F(str) (str)
  C:\Users\Bill\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.0.2\libraries\ESP8266SdFat\src\common\SysCall.h (1 hit)
    Line  77: #define F(str) (str)
  C:\Users\Bill\AppData\Local\Arduino15\packages\SparkFun\hardware\apollo3\1.2.1\cores\arduino\ard_sup\ard_supers\WString.h (1 hit)
    Line  38: #define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
  C:\Users\Bill\AppData\Local\Arduino15\packages\STMicroelectronics\hardware\stm32\2.3.0\cores\arduino\WString.h (1 hit)
    Line  38: #define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
FrizzaFava commented 1 year ago

ok thanks 👍