-
Macro problem
Hi, I'm having a bit of trouble with a macro. Is it possible that having a macro that refers to another macro which in turn referes to another macro and so on, too many times, can cause problems for the compiler? I'm cross compiling using AVR GCC, so I guess the compiler is gcc or some port of it. Anyway, here is the macros I got:
Code:
typedef uint8_t servo_time;
#define NUM_SERVO_TIME_BYTES (sizeof servo_time)
...
#define SERVO_DATA_MSG_LEN (1 + NUM_SERVOS * NUM_SERVO_TIME_BYTES)
...
#define MAX_MSG_LEN SERVO_DATA_MSG_LEN
...
#define I2C_MAX_DATA_LEN MAX_MSG_LEN
NUM_SERVOS is an enum. When the program gets down to these rows:
Code:
if (read_buf->data_len < I2C_MIN_DATA_LEN ||
read_buf->data_len > I2C_MAX_DATA_LEN)
{
I get: "error: expected expression before 'servo_time'" at the second rows. However, when I replace the definitions with what they are defined as, i.e.:
Code:
if (read_buf->data_len < I2C_MIN_DATA_LEN ||
read_buf->data_len > (1 + NUM_SERVOS * (sizeof servo_time)))
{
the problem ceases to exist. Anybody has any idea of why? :)
-
There should be a switch in your compiler (in gcc it's -e) that says "run only the preprocessor" -- you can then see what the compiler is actually seeing. (You do have to be careful since macro substitution only goes forward, but at first glance you appear to have that order correct.)
-
I will have to check that up tomorrow when I'm back to school and the same compiler; could be fun to know why it does this.
-
>the problem ceases to exist
I really have no idea why does it cease to exist. As to my knowledge, using sizeof with a typename always requires putting it in parentheses.
-
Yeah, GL.Sam is correct. Put parentheses around "servo_time" to fix the compile error. You are allowed to leave off the parentheses when sizeof takes an expression, but not when it takes the name of a type.
-
I did put parenthesis around the type, but it didn't matter. I still got the same error message. But now I'm not sure about whether I actually saved the file or not after I made the changes ... I'm used to programming in Visual Studio, which always saves all the files you have been working on before you use them in a compilation (AVR Studio does this to, but for some reason only for source files), or in Emacs which asks you for every file you have changed but not saved if you want to save it or not. Great, now I'm going to try that to tomorrow! :)
-
GL.Sam, you were right. When I put parenthesis around servo_time and just saved it worked. Thanks!