Problems when using #ifdef in header file

I want to be able to change parameters in my library without using a constructor, so I am trying to use #define in my .ino to control the .h. Here is my code :

#if defined(SSD1306) 
#include <Adafruit_SSD1306.h>
typedef Adafruit_SSD1306 DisplayDriver;  // Alias for uniform handling
#define DEFAULT_OLED_ADDRESS 0x3D

#elif defined(SH1106)
#include <Adafruit_SH110X.h>
typedef Adafruit_SH1106G DisplayDriver;  // Alias for uniform handling
#define DEFAULT_OLED_ADDRESS 0x3C

#else
#error "Le type d'écran utiliser doit être défini."
#endif

and the code in my .ino

#define SH1106  // Define the macro only if it isn't already defined
#include "Technoshield_ui.cpp"

Technoshield_ui ui;

void setup() {
  ui.begin();
}

void loop() {
  // Main code here
}

I always get the error, so my #define isnt reaching the header file at all. Is my code wrong, or is it just not possible to do it like that. Thanks for your help!

The latter.

First try renaming "Technoshield_ui.cpp" to .h. In most build systems, cpp files get compiled, which this file isn't supposed to be.

Why? It looks like both of those classes inherit from Adafruit_GFX. So, define the one you want in the .ino and pass it to your library's constructor as a Reference to a Adafruit_GFX object.

The logic behind what it is you are trying to do is unclear to me.

If you have a separate Arduino library with .cpp files, they're compiled separetely from your .ino file. Nothing you define in the .ino file will ever be seen by the library's .cpp files.

I see no .h file anywhere. Is that your unlabeled first bit of code? If so, you're not including it in your .ino file. I see your code try to include a .cpp file that is local to your sketch . That will not likely work.

Which ".h" are you referring to? If it's it in the library, then no. If it's in the sketch folder, I think you can do it.

The .cpp is supposed to be a .h, but it still doesnt work with a .h . What other method could I use to achieve this? I could use a constructor but it's less pratctical, since I have to include both libraries.

Hi @lolmanetcie.

Please provide a detailed description of what you mean by "doesnt work".

Make sure to provide the full and exact text of any errors or warnings you encountered.

Please also provide the full updated code.

Hard to tell what you mean by "it doesn't work" because you have not provided a complete example.

When I try

#define SH1106  // Define the macro only if it isn't already defined
//#define SSD1306

#include "Technoshield_ui.h"

Technoshield_ui ui;

void setup()
{
  Serial.begin (115200);
  ui.begin();
}

void loop()
{
  // Main code here

}

with Technoshield_ui.h as

#if defined(SSD1306) 
#include <Adafruit_SSD1306.h>
typedef Adafruit_SSD1306 DisplayDriver;  // Alias for uniform handling
#define DEFAULT_OLED_ADDRESS 0x3D
#define NAME "SSD1306"

#elif defined(SH1106)
#include <Adafruit_SH110X.h>
typedef Adafruit_SH1106G DisplayDriver;  // Alias for uniform handling
#define DEFAULT_OLED_ADDRESS 0x3C

#define NAME "SH1106"

#else
#error "Le type d'écran utiliser doit être défini."
#endif

class Technoshield_ui
{
  public:
  void begin() {
    Serial.print ("Display is ");
    Serial.println (NAME);
  }

};

It works as I expect (which may be different to what you expect).

I don't think you really understand how #include works.
Including both .h files does not have a down side in this case. Only the functions actually used will be included in the output object code.

You really need to provide move information about what you're trying to do. Meaning .... post a complete code!!! What is your Technoshield_ui class supposed to do?

Based on your current, inadequate description, the proper way to do it may be:

Main .ino file:

#include "Technoshield_ui.h"
#include <Adafruit_SSD1306.h>
#include <Adafruit_SH110X.h>

//#define SSD1306
#define SH1106

#if defined(SSD1306)
Adafruit_SSD1306 sh(16, 2);

#elif defined(SH1106)
Adafruit_SH1106G sh(16, 2);

#else
#error Undefined Shield
#endif

Technoshield_ui shield(sh);

void setup() {
}

void loop() {
}

Technoshield_ui.h:

#ifndef SHIELD_UI
#define SHIELD_UI

#include <Arduino.h>
#include <Adafruit_GFX.h>

class Technoshield_ui {
  public:
    Technoshield_ui(Adafruit_GFX &sh) : shield(sh) {}

  private:
    Adafruit_GFX &shield;
};
#endif

You would probably also want a Technoshield_ui.cpp file for longer method implementations. But, again, your intentions are too unclear to give better advise.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.