Previous | Next
One of the things you'll be doing the most when programming will invariably be trying to track down coding mistakes the compiler complains about. Here's a list of some common error messages that GCC 4.1 (as included in Mac OS X 10.4.7) will spit out, plus a detailed explanation of the most likely cause of these errors.

As mentioned earlier in the
Masters of the Void tutorial, GCC will valiantly struggle on and try to make sense of your program even when it encounters an error. Many of the errors GCC will show you will simply be caused by GCC having misunderstood you on the first error, and then misunderstanding the rest of the program. Thus, the closer you get to the bottom of the list of errors, the less the error messages may make sense. It's a good idea to keep this in mind when GCC throws 500 error messages at you. Fix the first error, then compile again. Chances are, half of the subsequent errors will be gone.
Conflicting types ... previous implicit declaration
main14.c:46: warning: conflicting types for 'DoNewCommand'
main14.c:33: warning: previous implicit declaration of 'DoNewCommand' was here
or
Warning: Incompatible implicit declaration of built-in-function 'printf'
What does it mean? GCC reads your source files from top to bottom. When it finds a function it doesn't know about, it usually assumes that it's a function that returns an int and takes ... as its parameters (use Xcode's menu command Help -> Open man Page... and type in stdarg for details on ... parameters). This assumption is called an implicit declaration, because GCC just assumes that's what you wanted and you were too lazy to write out a declaration. It is also usually a completely wrong guess, though it makes the code compile. When GCC later encounters your actual function definition, it will notice that your parameters don't have the type ... and maybe that it also doesn't return an int and warn you about it in the above fashion.
Solution: To avoid this error, either reorder your functions so the called function is above the function calling it and GCC has seen the function before it's first used, or add/include a forward declaration (see the chapter on headers).
operands ... have illegal types ... and 'void'
operands of = have illegal types 'int' and 'void'
What does it mean? The things to the left and right of an operator (i.e. +, -, *, / etc.) are called operands, meaning "things to be operated on". For most maths statements, the operands would be of type int on both sides, or maybe int on one and float on the other. But in this case, the second operand is of type void, and as we know void essentially means ignore me. You shouldn't be using a value that's to be ignored in an expression. Since you can't really declare a variable of type void (how large would a void be, anyway?), it can't be one of your variables, so what is it? Well, functions that don't have return values are declared as void. So, very likely, you've got a line like the following somewhere in your code:
foo = MyFunction( 123 );
where MyFunction is declared as:
void MyFunction( int n );
Or you have some function like in Chapter 5, where we have:
void GetArgument( bool isLeft, int* vFirstArg );
which returns its value
by reference through the address passed as
vFirstArg, and you're calling it like:
vFirstArg = GetArgument(true, &vFirstArg );
Solution: Check the types of all variables and the return types of all functions you're using in expressions on the line with the error. Very likely you forgot to specify int or float as the return type from a function, or you wanted a different function. In a case like GetArgument() in Chapter 5, the solution would be to simply get rid of the vFirstArg = part, as the &vFirstArg part combined with the code in the function itself already takes care of assigning the value to vFirstArg.
error: ... undeclared (first use in this function)
error: 'stdin' undeclared (first use in this function)
What does it mean? You are using a word the compiler has never heard of. Apart from a few little things built into the compiler (like if, for, int, void, return and a few others), everything you use in C is actually written in a C source code file provided by the system (or rather, in a "library"). Since the C compiler itself only knows the language, it can't know where these files are.
Solution: Add a #include statement to tell your compiler about the header file that tells it about the word you are using. Either that's a header file created by you, or one of the system header files.
Previous | Next
Fabio writes: Hi Uli
I have always liked your articles, and this tutorial is no different, I really appreciate your style of writing and very clear explanations, I was looking a for a concise way of refreshing my C skills and your tutorial does indeed resolve that plus showing some features I didn't know about XCode. Thanks!
One more thing, I would like to download this tutorial as a pdf and take it with me when I cannot connect to the internet, I am quite happy to create the document if I can have your permission?
Keep up the great work!
|
Uli Kusterer replies: ★ Fabio, I'm planning to eventually do a downloadable version. Not sure yet whether it will be a huge HTML file zipped for download, or a PDF or an RTFD, I'm still thinking about how to do it the easiest. I also want to do at least one more sweep over the text to make sure I've caught all typos etc.
However, if you want to do a PDF right now, send it to me and I'll put it up here as a stop-gap measure until I get around to it. |