user47287: I have my library that uses Serial, Serial1 and Serial2 to establish various logging scenarios. Library user would define the config at class constructor, using simple byte constants like 0, 1 and 2.
Problem is, Serial1 and Serial2 are not available in all board types. So when I compile my code (that compiles fine on Mega) on Uno, I get error 
'Serial1' was not declared in this scope.
So I figure I need to adjust my code with preprocessor directive like #if defined, in order to include some code only if Serial1 is defined. But it wouldn't work, because Serial1 is defined later, at compile-time. So what is the accepted way to do this?

user20245: Consider the approach used here in the ArduinoBoardManager project at github.  The file ArduinoBoardManager.h uses conditional compiling to determine the correct code to compile based on processor type.  A similar approach can be used to asses if certain hardware peripherals such as serial ports were available based on processor type.  
#if defined(__AVR_ATmega328P__) // uno, fi
  // Code compatible with an Arduino Uno
#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  // Code compatible with an Arduino Mega
#endif


user37622: HardwareSerial.h in Arduino AVR boards core defines HAVE_HWSERIALX
example
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif


47287: <p>I have my library that uses <code>Serial</code>, <code>Serial1</code> and <code>Serial2</code> to establish various logging scenarios. Library user would define the config at class constructor, using simple byte constants like 0, 1 and 2.</p>

<p>Problem is, <code>Serial1</code> and <code>Serial2</code> are not available in all board types. So when I compile my code (that compiles fine on Mega) on Uno, I get error <code>
'Serial1' was not declared in this scope</code>.</p>

<p>So I figure I need to adjust my code with preprocessor directive like <code>#if defined</code>, in order to include some code only if Serial1 is defined. But it wouldn't work, because Serial1 is defined later, at compile-time. So what is the accepted way to do this?</p>
 20245: <p>Consider the approach used <a href="https://github.com/backupbrain/ArduinoBoardManager" rel="nofollow noreferrer">here in the ArduinoBoardManager project at github</a>.  The file <a href="https://github.com/backupbrain/ArduinoBoardManager/blob/master/ArduinoBoardManager.h" rel="nofollow noreferrer">ArduinoBoardManager.h</a> uses conditional compiling to determine the correct code to compile based on processor type.  A similar approach can be used to asses if certain hardware peripherals such as serial ports were available based on processor type.  </p>

<pre><code>#if defined(__AVR_ATmega328P__) // uno, fi
  // Code compatible with an Arduino Uno
#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  // Code compatible with an Arduino Mega
#endif
</code></pre>
 37622: <p><a href="https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/HardwareSerial.h" rel="nofollow noreferrer">HardwareSerial.h</a> in Arduino AVR boards core defines HAVE_HWSERIALX</p>

<p>example</p>

<pre><code>#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif
</code></pre>
