Reputation: 309
So I was wondering why I am getting the following error:
Error 1 undefined reference to `MIN_COUNTER' C:\Users\Wyatt Crosby\Dropbox\Atmel Studio\ReflowController\ReflowController\ReflowController\Debug/.././ReflowController.c 146 1 ReflowController
When in PID.h I have:
#ifndef PID_H
#define PID_H
#define LCD_SCREEN_SIZE 16
#define MAX_COUNTS 180
#define OVEN_MAX_TEMP 260
#define OVEN_MIN_TEMP 0
#define MIN_COUNTER(x,a) tempLookupArray[x] < a ? x : MIN_COUNTER(x+1,a)
#define FIND_COUNTER(a) a <= OVEN_MIN_TEMP ? MAX_COUNTS : MIN_COUNTER(0,a)
const float tempLookupArray[MAX_COUNTS];
...
Where tempLookupArray is define further in PID.c:
const float tempLookupArray[MAX_COUNTS] = {
260.00,
260.00,
260.00,
259.99,
259.98,
259.96,
...
And in ReflowController.c I include PID.h and write:
TriggerCounter = FIND_COUNTER(PIDgain);
Where PIDgain is local and of type 'float', and TriggerCounter is global and of type 'volatile int'
It seems to be the fact that I am trying to call MIN_COUNTER from inside MIN_COUNTER and doesn't have any sort of prototype for it yet (if it were a function) . . .
Any thoughts from you smart guys out there?
Thanks!
Upvotes: 0
Views: 71
Reputation: 2106
After the preprocessor has gone through your code,
TriggerCounter = FIND_COUNTER(PIDgain);
becomes
TriggerCounter = PIDgain <= 0 ? 180 : tempLookupArray[0] < PIDgain ? 0 : MIN_COUNTER(0+1,PIDgain);
At that point, the preprocessor hands the code to the compiler and linker who look for a method called MIN_COUNTER
but can't find one. The preprocessor doesn't work recursively in a case like that. Imagine if it did: The code you wrote would send the compiler in an infinite loop; it would need to expand MIN_COUNTER
and to do that, it needs to expand another MIN_COUNTER
and so on...
Try using actual functions instead of macros
int min_counter(int x, float a) {
return tempLookupArray[x] < a ? x : min_counter(x+1,a);
}
int find_counter(float a) {
return a <= OVEN_MIN_TEMP ? MAX_COUNTS : min_counter(0,a);
}
In addition to recursing properly, it will also avoid some of the caveats of macros (For example, FIND_COUNTER(i++)
would increment i multiple times, whereas find_counter(i++)
wouldn't)
Upvotes: 2