-2

I've been using gcc in windows cmd to compile my c codes.

Here's my cmd command which work normally:

gcc test.c -o myprogram

When there are more than one errors, like this code:

#include <stdio.h>
#include <foo.h>
int main()
{
    printf("bar")
}

It displays just this one error

error.c:2:19: fatal error: foo.h: No such file or directory

compilation terminated.

instead of 3 errors:

  1. no "foo.h" header file
  2. printf("bar") has no semicolon
  3. no return value for int main

How can I improve my cmd command to make gcc show all of its errors?

lvlack
  • 61
  • 2
  • 6
  • 3
    GCC cant continue past a fatal error. You need to fix it before moving on. [This](http://stackoverflow.com/questions/6860568/how-to-get-gcc-to-skip-errors-but-still-output-them) might help. – IKavanagh Sep 15 '15 at 11:39
  • Thank you for explaining that to me. – lvlack Sep 15 '15 at 11:51

1 Answers1

3

The better command to invoke gcc in your case would be

  gcc -Wall -Wextra -g test.c -o myprogram

it is asking for [nearly] all warnings, some extra warnings, and debugging information (to use gdb)

However, GCC (or any reasonable compiler) cannot do much about a missing included file (since that missing file would probably define a lot of names needed to compile the rest of your code).

Read much more about the C preprocessor, i.e. cpp ...

Imagine that foo.h contained #define printf(...) (that would be a bad idea, since coding
#define printf(...) do{}while(0) would be perhaps better -but not that much, since printf(3) is returning an integer- and would have caught your error#2), then your code would be correct.

BTW, you probably should improve your habits and compile very often (e.g. every few minutes) your source code. Also, use a version control system like git and commit your source code several times a day (when it does compile).

If you use a good editor (e.g. GNU emacs) you can easily configure it to build your project (e.g. run make) thru M-x compile with one keypress. For a small project (less than 100 thousands lines of C source code) suitably organized in several translation units and using a good builder like GNU make recompiling it incrementally should take a few seconds at most, and you can afford doing that often.

Regarding missing return in main (specifically for main) (your error#3), this is (strangely) conforming to the C11 standard §5.1.2.2.3 (of n1570):

reaching the } that terminates the main function returns a value of 0

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547