Hi all,
It is my understanding that macros are done by the pre-processor, so if you declare a macro in the main() it should be useable in other header files. I have a program which exhibits some strange behaviour that seems to contradict this. I've stripped it down to the bare basics. The program is below:
In main.c:
#include "mbed.h"
//#define USE_RTC
#include "DataLog.h"
DataLog d;
int main() {
d.StartLogging();
while(1) {
__wfi();
}
}
In DataLog.h:
#include "mbed.h"
#ifndef DATA_LOG_H
#define DATA_LOG_H
class DataLog
{
public:
void StartLogging(void);
protected:
void Log(void);
char FileName[256];
#ifndef USE_RTC
Ticker SampleTimer;
#endif
void CheckIfDefined(void)
{
#ifndef USE_RTC
fprintf(stderr, "USE_RTC not defined in h\n");
#else
fprintf(stderr, "USE_RTC is defined in h\n");
#endif
}
};
#endif
In DataLog.cpp
#include "mbed.h"
#include "DataLog.h"
void DataLog::StartLogging(void)
{
#ifndef USE_RTC
SampleTimer.attach(this, &DataLog::Log, 1);
fprintf(stderr, "USE_RTC not defined in cpp\n");
#else
fprintf(stderr, "USE_RTC is defined in cpp\n");
#endif
CheckIfDefined();
};
void DataLog::Log(void)
{
fprintf(stderr, "Logging\n");
};
When USE_RTC isn't defined in the main, the program writes the following to the terminal:
Quote:
USE_RTC not defined in cpp
USE_RTC not defined in h
and then starts calling DataLog::Log() every second. This is what I would expect to see. If however, USE_RTC is defined in main (comments removed), the program writes the same message to the terminal, i.e.:
Quote:
USE_RTC not defined in cpp
USE_RTC not defined in h
but doesn't start logging. So it is doing the fprintfs like USE_RTC is not defined, but behaving like USE_RTC is defined.
This is adding highly unpredictable behaviour to the code. For example, if you comment out the definition of FileName and run the code with USE_RTC defined, nothing gets written to the terminal; the program just hangs.
Please could someone explain what is going on?
Thanks
Tim
Hi all,
It is my understanding that macros are done by the pre-processor, so if you declare a macro in the main() it should be useable in other header files. I have a program which exhibits some strange behaviour that seems to contradict this. I've stripped it down to the bare basics. The program is below:
In main.c:
In DataLog.h:
In DataLog.cpp
When USE_RTC isn't defined in the main, the program writes the following to the terminal:
Quote:
USE_RTC not defined in cpp
USE_RTC not defined in h
and then starts calling DataLog::Log() every second. This is what I would expect to see. If however, USE_RTC is defined in main (comments removed), the program writes the same message to the terminal, i.e.:
Quote:
USE_RTC not defined in cpp
USE_RTC not defined in h
but doesn't start logging. So it is doing the fprintfs like USE_RTC is not defined, but behaving like USE_RTC is defined.
This is adding highly unpredictable behaviour to the code. For example, if you comment out the definition of FileName and run the code with USE_RTC defined, nothing gets written to the terminal; the program just hangs.
Please could someone explain what is going on?
Thanks Tim