#ifdef not working

Post here first, or if you can't find a relevant section!
Post Reply
geologic
Posts: 10
Joined: Thu Dec 15, 2022 10:12 am
Answers: 1

#ifdef not working

Post 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?
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: #ifdef not working

Post 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.
User avatar
Vassilis
Posts: 23
Joined: Wed Dec 18, 2019 3:04 pm
Location: Thessaloniki, Greece
Contact:

Re: #ifdef not working

Post 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 */
Vassilis Serasidis
https://www.serasidis.gr
ag123
Posts: 1653
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: #ifdef not working

Post by ag123 »

it may work if you #include "todayLib.c" instead ( of "todayLib.h" )
Post Reply

Return to “General discussion”