Symbolic annoyance, preprocessor hints and tips

One of the guys in the office was having a compilation problem (hey tim!). Neither gcc nor sun cc were being (too) helpful in their error messages.
The piece of code in particular was a function declaration like:
void foo(food_t \*);
When it was compiled both sun cc said:"foo.c", line 4: syntax error before or at: \*, and gcc said: foo.c:4: error: syntax error before '\*' token which is not a particularly useful error message. Even when the code is compiled with full warnings (-v for sun cc, -Wall for gcc) we get the same useless error message.
Experience tells us that this means that the data type food_t has not been declared, as a result the food_t \* parses as a syntax error.
This was part of a (moderately) complicated piece of code, we knew the data type was being declared in one header file, but didn't know if that file was being included in this particular piece of code. In cases like this the preprocessor is a boon. We knew that a header file foo.h was needed so send in the preprocessor. Using the -E option to gcc (or sun cc), it only preprocesses the file, producing a stream of output.
Interpreting the output is a quite easy. In this case we were looking for a typedef unsigned int food_t;, which would tell us that the variable datatype had been defined. In this case it had not, meaning, in our case that the header file foo.h had not been included as part of the foo.c file. We #included the file, and lo and behold we made it to the next step.
I've grossly simplified the example to illustrate a point, but invariably when you encounter syntax errors in your c files, and are stumped for the reason why, it's only one of the many tools in your arsenal for finding out the reason why. After all what kind of ill effects are being induced in your code as a result of using complicated #define macros.
Comments:

Hey Pete - thanks for that ! Serves me right for writing java all the time, I'm completely spoilt by useful error messages and meaningful warnings !

Posted by Tim Foster on February 02, 2005 at 10:18 PM GMT #

Post a Comment:
Comments are closed for this entry.
About

petesh

Search

Archives
« April 2014
MonTueWedThuFriSatSun
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today