This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
user:mgough:compiler_errors [2010/05/08 22:12] – mgough | user:mgough:compiler_errors [2010/05/08 22:49] (current) – mgough | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== GCC Compiler Errors - Programming in C FAQ ====== | ||
+ | When writing code we are often moving fast, this can sometimes lead to problems where we make a simple (and sometimes, not so simple) mistake. | ||
+ | |||
+ | We will begin with some of the basics, using a simple hello world program. | ||
+ | |||
+ | |||
+ | =?==== Q: error: Why do I am I getting this error?? " | ||
+ | !!!This is the most common syntax error. | ||
+ | |||
+ | < | ||
+ | 1 #include < | ||
+ | 2 | ||
+ | 3 int main() | ||
+ | 4 { | ||
+ | 5 | ||
+ | 6 | ||
+ | 7 | ||
+ | 8 } | ||
+ | 9 | ||
+ | </ | ||
+ | |||
+ | When compiling the code, you recieve the following output. | ||
+ | :~$ gcc -o test1 test1.c | ||
+ | test1.c: In function ' | ||
+ | test1.c:8: error: expected ';' | ||
+ | |||
+ | Our error states pretty clearly that in the main function, the compiler expects a semicolon before the close bracket. | ||
+ | |||
+ | =?==== Q: Great, now I get this? ' | ||
+ | < | ||
+ | 1 #include < | ||
+ | 2 | ||
+ | 3 int main() | ||
+ | 4 { | ||
+ | 5 | ||
+ | 6 for(x = 1; x < 5; x++) | ||
+ | 7 { | ||
+ | 8 | ||
+ | 9 } | ||
+ | | ||
+ | 11 } | ||
+ | 12 | ||
+ | </ | ||
+ | Using the following compilation line: | ||
+ | < | ||
+ | :~$ gcc -o test1 test1.c | ||
+ | test1.c: In function ' | ||
+ | test1.c:6: error: ' | ||
+ | test1.c:6: error: (Each undeclared identifier is reported only once | ||
+ | test1.c:6: error: for each function it appears in.) | ||
+ | </ | ||
+ | That's quite a bit of error spam! Not to worry, its pretty simple. | ||
+ | |||
+ | Wait, x is set to = 0 right? Yes, it is, but in C (and most other languages) you must declare x as a variable with a type. The following nugget of code solves the problem. | ||
+ | |||
+ | < | ||
+ | 1 #include < | ||
+ | 2 | ||
+ | 3 int main() | ||
+ | 4 { | ||
+ | 5 int x; | ||
+ | 6 for(x = 1; x < 5; x++) | ||
+ | 7 { | ||
+ | 8 | ||
+ | 9 } | ||
+ | | ||
+ | 11 } | ||
+ | </ | ||
+ | We've added "int x;" to line 5, the program will happily compile now as you say hello to the world multiple times. | ||
+ | |||
+ | =?==== Q: I get this warning.. But it still runs? | ||
+ | The GCC compiler is smart enough to figure out where you are going wrong SOMETIMES. | ||
+ | |||
+ | < | ||
+ | 1 int main() | ||
+ | 2 { | ||
+ | 3 | ||
+ | 4 | ||
+ | 5 } | ||
+ | </ | ||
+ | It may not be obvious to the novice programmer, but we are missing a header file, stdio.h to be precise. | ||
+ | < | ||
+ | 1 #include < | ||
+ | </ | ||
+ | Header files can sometimes be a bit tricky, you really need to know where a function is defined, but that is what the interwebs are for, research is your buddy. | ||
+ | |||
+ | =?==== Q: How do I print text in quotes without getting this error? error: expected ' | ||
+ | < | ||
+ | 1 #include < | ||
+ | 2 | ||
+ | 3 int main() | ||
+ | 4 { | ||
+ | 5 | ||
+ | 6 | ||
+ | 7 } | ||
+ | </ | ||
+ | Results in the error: | ||
+ | < | ||
+ | ~$ gcc -o test1 test1.c | ||
+ | test1.c: In function ' | ||
+ | test1.c:5: error: expected ' | ||
+ | </ | ||
+ | Another sticky wicket for the novice. | ||
+ | |||
+ | Here is the fix. | ||
+ | |||
+ | < | ||
+ | 1 #include < | ||
+ | 2 | ||
+ | 3 int main() | ||
+ | 4 { | ||
+ | 5 | ||
+ | 6 | ||
+ | 7 } | ||
+ | </ | ||
+ | |||
+ | Which will output " | ||
+ | |||
+ | There are many other escape sequences as well, here's a list of the more common ones. I highly recommend using \a in long loops. | ||
+ | |||
+ | < | ||
+ | \a | ||
+ | Bell (alert) | ||
+ | |||
+ | \b | ||
+ | Backspace | ||
+ | |||
+ | \f | ||
+ | Formfeed | ||
+ | |||
+ | \n | ||
+ | New line | ||
+ | |||
+ | \r | ||
+ | Carriage return | ||
+ | |||
+ | \t | ||
+ | Horizontal tab | ||
+ | |||
+ | \v | ||
+ | Vertical tab | ||
+ | |||
+ | \' | ||
+ | Single quotation mark | ||
+ | |||
+ | \" | ||
+ | Double quotation mark | ||
+ | |||
+ | \\ | ||
+ | Backslash | ||
+ | |||
+ | \? | ||
+ | Literal question mark | ||
+ | |||
+ | \ooo | ||
+ | ASCII character in octal notation | ||
+ | |||
+ | \xhh | ||
+ | ASCII character in hexadecimal notation | ||
+ | </ | ||
+ | |||
+ | =?==== Q: MASSIVE ERRORS, help?? ===== | ||
+ | < | ||
+ | ~$ gcc -o test1.c test1 | ||
+ | test1: | ||
+ | / | ||
+ | test1: In function `__data_start': | ||
+ | (.data+0x4): | ||
+ | / | ||
+ | test1: In function `_init': | ||
+ | / | ||
+ | / | ||
+ | test1: In function `_start': | ||
+ | ../ | ||
+ | / | ||
+ | test1: In function `_fini': | ||
+ | / | ||
+ | / | ||
+ | test1: | ||
+ | / | ||
+ | test1: In function `__data_start': | ||
+ | (.data+0x0): | ||
+ | / | ||
+ | collect2: ld returned 1 exit status | ||
+ | </ | ||
+ | Oh noez! You are going to fail your class!!! | ||
+ | .. Actually, this is a simple one. | ||
+ | |||
+ | Instead of typing this to compile your program.. | ||
+ | < | ||
+ | ~$ gcc -o test1.c test1 | ||
+ | </ | ||
+ | Try putting it the right way :D (destination file name first, followed by the source file) | ||
+ | < | ||
+ | ~$ gcc -o test1 test1.c | ||
+ | </ | ||
+ | |||
+ | What what?? Yes, this was a simple solution, but with an overwhelming error output. |