Can you use something like #ifdef to check if a header file is included?

Is there any way to check if a header file is included?

I’m working on a library that I’d like to assign different functions based off which I2C library is installed.

for instance some thing along these lines:

#ifdef <Wire.h>
  Wire.begin();
#elseif <I2C.h>
  I2c.begin();
#endif

Yes, if the header file defines something unique. (Most do so in their guard code.)

In other words, most headers will have the form:

#ifndef __SOME_UNIQUE_THING_H
#define __SOME_UNIQUE_THING_H
...
#endif

So you would put the following in your code:

#ifdef __SOME_UNIQUE_THING_H
// Must be using SomeUniqueThing library
#endif

If they don't have that, you can look through the header for a #define that is "pretty unique" to that header and test for that. Failing that, I don't know any other easy way. Be wary of different versions of the same libraries, too.

Er, how will that work exactly? Say you include the library, eg.

#include <Wire.h>

Then you know you included it, right? But if you don’t include it, there is no point in testing for some define inside it.

AndyCC:
Yes, if the header file defines something unique. (Most do so in their guard code.)

In other words, most headers will have the form:

#ifndef __SOME_UNIQUE_THING_H

#define __SOME_UNIQUE_THING_H

#endif




So you would put the following in your code:



#ifdef __SOME_UNIQUE_THING_H
// Must be using SomeUniqueThing library
#endif




If they don't have that, you can look through the header for a #define that is "pretty unique" to that header and test for that. Failing that, I don't know any other easy way. Be wary of different versions of the same libraries, too.

Doh!! Wire library uses TwoWire_h no wonder it wasn’t working. Thanks AndyCC.

Er, how will that work exactly? Say you include the library, eg.

Code:

#include <Wire.h>

Then you know you included it, right? But if you don’t include it, there is no point in testing for some define inside it.

I probably didn’t explain it well enough. I’m making a library for an I2C device so when it’s included it will check to see if the person is using Wire or my I2C library. The functions are completely different between the two. I also need a check in place to make sure they didn’t include both libraries because if they somehow did the I2C wouldn’t function right because I shut off interrupts inside it.

wayneft:
I’m making a library for an I2C device so when it’s included it will check to see if the person is using Wire or my I2C library.

Well perhaps I don’t understand, but I don’t see how C code can check what a person is doing.

If you are trying to find if, inside their project, a different file uses a certain include, I don’t see how you can do that. And if you use the include, you know you did it.

If you put the #include for your library, under the #include for the other library, in the main sketch, perhaps your .h file can see what is defined in the other .h file.

But that won't affect things inside your .cpp file. Maybe you can work around it with #define inside your .h file, which changes behaviour based on what things are defined when the .h file is included. But even then, I don't quite visualize the sequence.

If it helps, here is how I worked around this issue when doing a RS485 protocol:

#if defined(ARDUINO) && ARDUINO >= 100
  #include "Arduino.h"
#else
  #include "WConstants.h"
#endif

typedef void (*WriteCallback)  (const byte what);    // send a byte to serial port
typedef int  (*AvailableCallback)  ();    // return number of bytes available
typedef int  (*ReadCallback)  ();    // read a byte from serial port

void sendMsg (WriteCallback fSend, 
              const byte * data, const byte length);
byte recvMsg (AvailableCallback fAvailable, ReadCallback fRead, 
              byte * data, const byte length, 
              unsigned long timeout = 500);

When you want to use the function sendMsg you pass a function address (the WriteCallback function). This could be Serial.write, or Wire.send, or Serial1.write, or whatever. As long as it takes a byte and sends it, I am happy. So you could do something similar for the begin code.

Here’s a rough example of what I’m trying to do:
Case Using Wire library in the main sketch:

Main Sketch

#include <Wire.h>
#include <myLib.h>

void setup(){}

void loop()
{
  getI2CData();
}


myLib

#ifdef TwoWire_h
  ... //other functions
  #define GETDATA Wire.endTransmission();
#endif
#ifdef I2C_h
  #define GETDATA I2c.read(1,1,1);
#endif

void getI2CData(){
  GETDATA;
}

So if they are using the Wire library it would use Wire’s functions to retrieve data and they were using the I2C library it would use the other functions.

Is it working? My point is that if mylib has a .cpp component, then when the .cpp file is compiled it will only include what you tell it to, eg.

#include <myLib.h>

It won’t know that, in the main sketch, another file was included first.

I’m going to test it all later, just got home from work.

Doesn't look like this is going to work. I guess I'll have to have an option in the library header to choose which library is being used. I had hoped there could be a way to do this, then I could make just one library for sensors that use either I2C or SPI. Depending on if they included Wire or SPI in the main sketch the library would know which functions to use to get the data from the sensor. Oh well...thanks for the help.