Want to overwrite #define of library

Hello. I use a library which in one header defines a TEMPLATE_PLACEHOLDER. (with check ifndef). I want to change this placeholder to another value. I’ve tried:

  • using #define TEMPLATE_PLACEHOLDER ‘|’ in main.cpp before the import
  • using #define TEMPLATE_PLACEHOLDER ‘|’ in main.cpp after the import (gives an overwrite warning)
  • using #ifdef TEMPLATE_PLACEHOLDER #undef TEMPLATE_PLACEHOLDER #define TEMPLATE_PLACEHOLDER
  • usind #undef TEMPLATE_PLACEHOLDER #define TEMPLATE_PLACEHOLDER

All versions do not change the actual placeholder.
only working version is if i change directly the library header file.
What can i do?

This also depends on the library code. E.g., if the library does in the header file

#define MACRO 123

without checking if it’s previously defined (#ifndef MACRO …), you can’t overwrite it from my understanding, except for changing the code.

Note that when the macro is used in the library’s implementation .cpp file, it won’t be affected by whatever you put in main.cpp, since it’s a different compilation unit.

So, you should define the flag globally using the define build_flags option, with e.g.

build_flags =
   -D TEMPLATE_PLACEHOLDER='|'

in the platformio.ini.

I’ve tried that, but this leads to an error “The command “-DESP32” is either wrong or could not be found”.

Processing esp-wrover-kit (platform: espressif32; board: esp-wrover-kit; framework: arduino)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION:...
PLATFORM: Espressif 32 (3.2.0) > Espressif ESP-WROVER-KIT
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (ftdi) On-board (ftdi) External (esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES:
 - framework-arduinoespressif32 3.10006.210326 (1.0.6)
 - tool-esptoolpy 1.30000.201119 (3.0.0)
 - toolchain-xtensa32 2.50200.97 (5.2.0)
LDF: Library Dependency Finder -> ...
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 32 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <WiFi> 1.0
|-- <ESP Async WebServer> 1.2.3
|   |-- <AsyncTCP> 1.1.1
|   |-- <FS> 1.0
|   |-- <WiFi> 1.0
|-- <AsyncElegantOTA> 2.2.5
|   |-- <AsyncTCP> 1.1.1
|   |-- <ESP Async WebServer> 1.2.3
|   |   |-- <AsyncTCP> 1.1.1
|   |   |-- <FS> 1.0
|   |   |-- <WiFi> 1.0
|   |-- <FS> 1.0
|   |-- <Update> 1.0
|   |-- <WiFi> 1.0
|-- <AsyncTCP> 1.1.1
Building in release mode
Compiling .pio\build\esp-wrover-kit\src\main.cpp.o
Generating partitions .pio\build\esp-wrover-kit\partitions.bin
Der Befehl "-DESP32" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.
Compiling .pio\build\esp-wrover-kit\lib79f\WiFi\ETH.cpp.o
Compiling .pio\build\esp-wrover-kit\lib79f\WiFi\WiFi.cpp.o
Der Befehl "-DESP32" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.
Compiling .pio\build\esp-wrover-kit\lib79f\WiFi\WiFiAP.cpp.o
Der Befehl "-DESP32" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.
Compiling .pio\build\esp-wrover-kit\lib79f\WiFi\WiFiClient.cpp.o
Der Befehl "-DESP32" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.
Compiling .pio\build\esp-wrover-kit\lib79f\WiFi\WiFiGeneric.cpp.o
Der Befehl "-DESP32" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.
Compiling .pio\build\esp-wrover-kit\lib79f\WiFi\WiFiMulti.cpp.o
Der Befehl "-DESP32" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.
Compiling .pio\build\esp-wrover-kit\lib79f\WiFi\WiFiSTA.cpp.o
Der Befehl "-DESP32" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.
Compiling .pio\build\esp-wrover-kit\lib79f\WiFi\WiFiScan.cpp.o
Compiling .pio\build\esp-wrover-kit\lib79f\WiFi\WiFiServer.cpp.o
Der Befehl "-DESP32" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.
Compiling .pio\build\esp-wrover-kit\lib79f\WiFi\WiFiUdp.cpp.o
*** [.pio\build\esp-wrover-kit\src\main.cpp.o] Error 255
Der Befehl "-DESP32" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.
*** [.pio\build\esp-wrover-kit\lib79f\WiFi\ETH.cpp.o] Error 255
*** [.pio\build\esp-wrover-kit\lib79f\WiFi\WiFi.cpp.o] Error 255
*** [.pio\build\esp-wrover-kit\lib79f\WiFi\WiFiAP.cpp.o] Error 255
*** [.pio\build\esp-wrover-kit\lib79f\WiFi\WiFiClient.cpp.o] Error 255
*** [.pio\build\esp-wrover-kit\lib79f\WiFi\WiFiGeneric.cpp.o] Error 255
*** [.pio\build\esp-wrover-kit\lib79f\WiFi\WiFiMulti.cpp.o] Error 255
*** [.pio\build\esp-wrover-kit\lib79f\WiFi\WiFiSTA.cpp.o] Error 255
*** [.pio\build\esp-wrover-kit\lib79f\WiFi\WiFiScan.cpp.o] Error 255
Der Befehl "-DESP32" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.
*** [.pio\build\esp-wrover-kit\lib79f\WiFi\WiFiServer.cpp.o] Error 255
Der Befehl "-DESP32" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.
*** [.pio\build\esp-wrover-kit\lib79f\WiFi\WiFiUdp.cpp.o] Error 255

I’ve used the following platformio.ini:

[env:esp-wrover-kit]
platform = espressif32
board = esp-wrover-kit
framework = arduino
upload_port = COM7
board_build.partitions = min_spiffs.csv
monitor_port = COM7
monitor_speed = 115200
build_flags =
    ; -U TEMPLATE_PLACEHOLDER
    -D TEMPLATE_PLACEHOLDER='|'
    
;lib_ldf_mode = chain+
lib_deps = 
    WiFi 
    ESP Async WebServer
    ayushsharma82/AsyncElegantOTA @ ^2.2.5

Library ESP Async Webserver makes the problem.

#include "Arduino.h"
// #include "SPI.h"
// #include "WiFiMulti.h"
// #include <SD.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>
AsyncWebServer server(80);

The rest is like the example at ESP32 Async Web Server – Control Outputs | Random Nerd Tutorials (sorry i can’t post the link) only with a css code that contaisn size 100% etc. Since % is the default template placeholder, i wanted to change this.
The library uses

#ifndef TEMPLATE_PLACEHOLDER

#define TEMPLATE_PLACEHOLDER ‘|’

#endif
so that should be ok…

Oh yeah there is some weird transformation going on so it needs escaping. When trying this on the commandline for normal GCC, the invocation

gcc -D"TEMPLATE_PLACEHOLDER='|'"  -o temp temp.c

does the trick, but not in PlatformIO, because of shell escaping… somehow screwing up with the quotes. The workaround is to represent chars by their ASCII numeral value and a cast to char.

build_flags =
    -D"TEMPLATE_PLACEHOLDER=\(char\)124"

should work.

1 Like

Worked without the escape characters:

build_flags =
    -D"TEMPLATE_PLACEHOLDER=(char)124"