When pre-compiling the code for an Uno, the value of the const global variable is recorded in the archive as "uno", and then when uploading the code on a Nano, the board is identified as "uno".
It's like you are saying I've const byte x =3;
in my code and I would want x
to be 7 if I run that compiled binary on a nano versus an uno...
This has nothing to do with "pre-compiled" libraries (or libraries at all), you compiled specific hardwired code for one machine and uploaded to a different one and expect to see differences. Why would you expect this to work?
==> Build a binary for Uno and another one for the Nano and all will be fine.
I just tried by creating a very small library:
** **MyArduino.h** **
#ifndef __MYARDUINO_H__
#define __MYARDUINO_H__
void printName();
#endif
** **MyArduino.cpp** **
#include <Arduino.h>
#include "MyArduino.h"
#if defined(ARDUINO_AVR_UNO)
const char* boardname = "AVR UNO";
#elif defined(ARDUINO_AVR_NANO)
const char* boardname = "AVR NANO";
#elif defined(ARDUINO_AVR_MEGA2560)
const char* boardname = "AVR MEGA2560";
#elif defined(ARDUINO_AVR_MICRO)
const char* boardname = "AVR MICRO";
#else
const char* boardname = "UNKNOWN BOARD";
#endif
void printName()
{
Serial.println(boardname);
}
I put those two files in a MyArduino
folder inside my custom Library folder.
then I built a very minimal sketch
#include <MyArduino.h>
void setup() {
Serial.begin(115200);
printName();
}
void loop() {}
when I compile and upload for a UNO Serial Monitor (@ 115200 bauds) will show
[color=purple]AVR UNO[/color]
and when I switch platform and compile/upload for a NANO I see in the monitor
[color=purple] AVR NANO[/color]
So all seems to work fine, the IDE will recompile the library if I switch the target platform and so I can have two binaries, one for Nano and one for Uno.
--- as a side note for documentation ---
you can see all the specific #define that are added at compile time based on the target board by running the command line(unix systems)
grep board= ~/Library/Arduino15/packages/*/hardware/*/*/boards.txt | cut -f2 -d= | sort -fu
on my system I get a long list
ALKS
AMPERKA_WIFI_SLOT
AVR_ADK
AVR_BT
AVR_CIRCUITPLAY
AVR_DUEMILANOVE
AVR_ESPLORA
AVR_ETHERNET
AVR_FIO
AVR_GEMMA
AVR_INDUSTRIAL101
AVR_LEONARDO
AVR_LEONARDO_ETH
AVR_LILYPAD
AVR_LILYPAD_USB
AVR_LININO_ONE
AVR_MEGA
AVR_MEGA2560
AVR_MICRO
AVR_MINI
AVR_NANO
AVR_NG
AVR_PRO
AVR_ROBOT_CONTROL
AVR_ROBOT_MOTOR
AVR_UNO
AVR_UNO_WIFI_DEV_ED
AVR_YUN
AVR_YUNMINI
BPI-BIT
CoreESP32
D-duino-32
D1_MINI32
ESP320
esp32vn_iot_uno
ESP32_DEV
ESP32_DEVKIT_LIPO
ESP32_EVB
ESP32_GATEWAY
ESP32_PICO
ESP32_POE
ESP32_POE_ISO
ESP32_THING
ESP8266_ARDUINO
ESP8266_ARDUINO_PRIMO
ESP8266_ARDUINO_STAR_OTTO
ESP8266_ARDUINO_UNOWIFI
ESP8266_ESP01
ESP8266_ESP07
ESP8266_ESP12
ESP8266_ESP13
ESP8266_ESP210
ESP8266_ESPECTRO_CORE
ESP8266_ESPRESSO_LITE_V1
ESP8266_ESPRESSO_LITE_V2
ESP8266_GENERIC
ESP8266_NODEMCU
ESP8266_OAK
ESP8266_PHOENIX_V1
ESP8266_PHOENIX_V2
ESP8266_SCHIRMILABS_EDUINO_WIFI
ESP8266_SONOFF_BASIC
ESP8266_SONOFF_S20
ESP8266_SONOFF_SV
ESP8266_SONOFF_TH
ESP8266_THING
ESP8266_THING_DEV
ESP8266_WEMOS_D1MINI
ESP8266_WEMOS_D1MINILITE
ESP8266_WEMOS_D1MINIPRO
ESP8266_WEMOS_D1R1
ESP8266_WIO_LINK
ESPea32
ESPECTRO32
ESPino32
FEATHER_ESP32
fm-devkit
FROG_ESP32
GEN4_IOD
HELTEC_WIFI_KIT_32
HELTEC_WIFI_LORA_32
HELTEC_WIFI_LORA_32_V2
HELTEC_WIRELESS_STICK
HORNBILL_ESP32_DEV
HORNBILL_ESP32_MINIMA
INTOROBOT_ESP32_DEV
LOLIN32
LOLIN_D32
LOLIN_D32_PRO
LoPy
LoPy4
M5Stack_Core_ESP32
M5STACK_FIRE
M5Stick_C
MH_ET_LIVE_ESP32DEVKIT
MH_ET_LIVE_ESP32MINIKIT
MOD_WIFI_ESP8266
NANO32
Node32s
NodeMCU_32S
ODROID_ESP32
ONEHORSE_ESP32_DEV
OROCA_EDUBOT
Pocket32
PYCOM_GPY
QUANTUM
SAMD_CIRCUITPLAYGROUND_EXPRESS
SAMD_MKR1000
SAMD_MKRFox1200
SAMD_MKRGSM1400
SAMD_MKRNB1500
SAMD_MKRVIDOR4000
SAMD_MKRWAN1300
SAMD_MKRWAN1310
SAMD_MKRWIFI1010
SAMD_MKRZERO
SAMD_NANO_33_IOT
SAMD_TIAN
SAMD_ZERO
SAM_DUE
SAM_ZERO
T-Beam
T-Watch
TTGO_LoRa32_V1
TTGO_T1
UBLOX_NINA_W10
WESP32
WIDORA_AIR
WIFIDUINO_ESP8266
WIFINFO
WIPY3
the real #define will have a leading [color=blue]ARDUINO_[/color]
so that's how you get [color=blue]ARDUINO_[/color][color=purple]AVR_UNO[/color]
==> As #define, they are taken into account at pre-processing time, even before the compilation or link stage.