Page 1 of 1

#ifdef not working

Posted: Tue Dec 05, 2023 5:17 pm
by geologic
Hi all

In Arduino IDE i have this at my main code:

Code: Select all

#define TODAY

#include "todayLib.h"

and this on my todayLib.c library file:

Code: Select all

#ifdef TODAY
	Serial.print("Now is Today");
#else
	Serial.print("Now is not Today");
#endif
If i run this code i see "Now is not Today", so it seems my #define TODAY is not being recognized inside my library.
If i move the #define TODAY into todayLib.h, it works as expected.

Is this a normal behavior?
I want to adapt my library to work with two different boards, so how do i define something on my main code that runs different parts of my library?

Re: #ifdef not working

Posted: Wed Dec 06, 2023 9:08 am
by fpiSTM
That is normal, when the c file include the h file, your #define is not present.
To get your define available globally you should pass it as a global definition in your commande line -DTODAY or something like that.

Re: #ifdef not working

Posted: Wed Dec 06, 2023 10:56 am
by Vassilis
geologic wrote: Tue Dec 05, 2023 5:17 pm ...
I want to adapt my library to work with two different boards, so how do i define something on my main code that runs different parts of my library?
Maybe you can pass your board type as a parameter to your library constructor.

todayLib.h

Code: Select all

...

    void begin(uint8_t myboard = 1); /* By default, board 1 is been selected. */
...
todayLib.c

Code: Select all

void todaylib::begin(uint8_t myboard){
    
    switch(myboard ){
        case 1:
            <board 1 is selected>
        break;
        
        case 2:
            <board 2 is selected>
        break;
    }
    
....
main sketch

Code: Select all

todayLib mylib;

....

mylib.begin(); /* board 1 is selected */
mylib.begin(1); /* board 1 is also selected */
or

mylib.begin(2); /* board 2 is selected */

Re: #ifdef not working

Posted: Wed Dec 06, 2023 11:38 am
by ag123
it may work if you #include "todayLib.c" instead ( of "todayLib.h" )