Including Libraries on an Optional Basis

I have several variations of a sketch.
I started with a basic sketch and added options.
So I have a copy of each step with different options.
It is becoming a hassle to maintain all of them.

I would now like to amalgamate all the options into a single sketch and invoke the desired runtime options with a variable.

The problem I am facing is that some options require different libraries.

I am therefore trying to include libraries on an optional basis.

Something like this...

int optSD = 1; // 0 = No, 1 = Yes.

if (optSD == 1){
  #include <SD.h>  
}
void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

The above method does not compile.

sketch_aug16c:3: error: expected unqualified-id before 'if'
expected unqualified-id before 'if'

My research shows I cannot have "free standing code" where I have it and need to wrap it in a function.
I moved the if statement into the void loop but it still does not work.

Some help would be appreciated.

Have a look at the #ifdef and associated preprocessor commands
Something like this, maybe

#define optServo

#ifdef optServo
  #include <Servo.h>
  Servo myServo;
#endif

void setup() 
{
  #ifdef optServo
    myServo.attach(9);
  #endif
}

void loop() 
{
}

Comment out the #define line to control which parts of the code are compiled.

Unfortunately, these conditional includes never worked with the Arduino IDE. Any lines starting with #include are processed and the file is always included... :frowning:

guix:
Unfortunately, these conditional includes never worked with the Arduino IDE. Any lines starting with #include are processed and the file is always included... :frowning:

Hmm, you are correct.

#define sd_SD

#ifdef sd_SD
  #include <SD.h>
  #include <SPI.h>
#endif

void setup() 
{
  // Start Serial
  Serial.begin(9600);  
  while (!Serial) {;}              // wait till port connects
  Serial.println(F("Serial started.")); // test print
  
  #ifdef sd_SD
    Serial.println(F("SD Card Available.")); // test print
    #else
    Serial.println(F("No SD Card Available.")); // test print
  #endif
}

void loop() 
{
}

Defined result.

Sketch uses 6,252 bytes (19%) of program storage space. Maximum is 32,256 bytes.
Global variables use 818 bytes (39%) of dynamic memory, leaving 1,230 bytes for local variables. Maximum is 2,048 bytes.

// #define sd_SD

#ifdef sd_SD
  #include <SD.h>
  #include <SPI.h>
#endif

void setup() 
{
  // Start Serial
  Serial.begin(9600);  
  while (!Serial) {;}              // wait till port connects
  Serial.println(F("Serial started.")); // test print
  
  #ifdef sd_SD
    Serial.println(F("SD Card Available.")); // test print
    #else
    Serial.println(F("No SD Card Available.")); // test print
  #endif
}

void loop() 
{
}

Not defined result.

Sketch uses 6,254 bytes (19%) of program storage space. Maximum is 32,256 bytes.
Global variables use 818 bytes (39%) of dynamic memory, leaving 1,230 bytes for local variables. Maximum is 2,048 bytes.

However, since I can use #ifdef / #ifndef in the top of the sketch, that should allow me set conditions for everything else except the libraries themselves, which I could do manually.

Testing my theory on the sketch itself now...

guix:
Unfortunately, these conditional includes never worked with the Arduino IDE. Any lines starting with #include are processed and the file is always included... :frowning:

Oh dear, but true now that I have tested it in the IDE....

Few workarounds here: IDE doesn't honor preprocessor conditionals when including libraries · Issue #1841 · arduino/Arduino · GitHub

guix:
Unfortunately, these conditional includes never worked with the Arduino IDE. Any lines starting with #include are processed and the file is always included... :frowning:

If that is true, then the IDE needs to be fixed. This behaviour is simply wrong.

guix:
Few workarounds here: IDE doesn't honor preprocessor conditionals when including libraries · Issue #1841 · arduino/Arduino · GitHub

I prefer the KISS method, so will for the moment stick with #ifdef.

How can I use #ifdef in conjunction with or?
I want to have the equivalent of this :

if (x == 0 || y == 0) {
......... }

But assume x is a define statement and y is a define statement.
So how would I correctly write an or statement?

#define x
#define y

#ifdef x or #ifdef y

That would be:

#if defined(x) || defined(y)

See If (The C Preprocessor)

Thanks guix.

Looked up the link - really minimal :slight_smile:

  1. From the linked page : #if defined (vax) || defined (ns16000)
    What is the significance of the underscores if any?

  2. So I have to ask when to use #ifdef vs #if defined.
    Are they equivalent?
    Is there some sort of guideline of when to use one or the other?

FWIW at the moment I am using only #ifdef throughout my code i.e. in the top, in setup and in the loop.

My current solution is to manually edit a series of "defines" and "includes".

I would like to implement a work around by using 2 sketches similar to the suggestions in Post #5.

I have tried this, and it did not work for me - but maybe I did something wrong.

So is this possible...?

I would like to have my main sketch with the defines listed and the full sketch - but no libraries.
From the the main sketch I will include a separate file say "librarylist".

Librarylist will contain the conditional includes.

So main sketch will look something like this:

define lan_ethernet;

include <librarylist.h>

void setup() {
  // put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly:
}

Librarylist will look something like this:

#ifdef lan_ethernet;
#include <Ethernet.h>
#endif

I have advanced and placed librarylist.h in a folder under libraries.
No problem to include librarylist.h, but the IDE does not find optional libraries automatically.

The code compiles if I place the libraries directly in the librarylist folder.
However, I want to place the libraries in subfolders under librarylist.h

How does one include a library and specify a path to the library?

How does one include a library and specify a path to the library?

#include "completePath/headerName.h"

PaulS:
#include "completePath/headerName.h"

It was the "completePath" I was having trouble with initially.
I since figured that, but that is only part of the problem because even if u supply the complete path, it won't compile because u run into linker problems.

Seems there is no way to do it except place all the library files in the same folder as the referring library.
This could also be a problem because some libraries have sub folders with the same names, so u will have conflicts.

I have given up for now.

Alternative is to migrate to another IDE, which is on the cards but I need more to build up more experience.