Re: disabling parse error in gcc



I think I need to explain a little more.First and foremost , my code is
written in plain and simple "C" language and nothing else.
Gordon , you have hit the nail on the head, my problem is exactly as
you have told. Let me explain -
my code goes like this -

#include "header1.h"
#include "header2.h"
int main()
{
//code follows
return 0;
}

Now, the problem is with headers and nowhere else.Suppose a int has
been typedefed in header2.h as -

// in header2.h
typedef int D_TYPE

But let's say D_TYPE is used as a parameter of a structure while
declaring the structure in header1.h.

// in header1.h
typedef struct MYSTRUCT{
//other parameters
D_TYPE a;
//other parameters
}MYSTRUCT;

My rules for dealing with this, which might get messy if it wasn't
coded that way in the first place:

1. A header file shall (unconditionally) include its prerequesite
header files (for example, needed typedefs, structure definitions,
etc.) so that a source file containing only a #include of
it will compile correctly. (Note: some compilers reject empty
source files or source files that don't contain any function or
variable declarations, which I think is allowed by ANSI C but I
have yet to encounter a compiler that actually does this. In any case,
you could add the line int main(){} to the end of the source file.)
2. A header file shall permit multiple inclusion of itself.
In practice, this means bracketing the guts of the include file
with:
#ifndef H_FOO_H
#define H_FOO_H 1
... guts of include file ...
#endif
so that a source file containing only TWO #includes of it will compile,
even if the guts of the include file includes typedefs.
3. The "guard symbol" used for an include file shall not be referenced
in any other file.
4. The "guard symbol" used for an include file shall not be referenced
in any other file for the purpose of speeding up compilation.
5. The "guard symbol" used for an include file shall not be referenced
in any other file for the purpose of even talking about speed of
compilation.
6. A source file consisting only of any number of #includes of header
files in any order and any number of times shall compile correctly.

One problem this style does have: if a source file requires stuff
from header1.h and header2.h, and header1.h includes header2.h and
header2.h includes header1.h (with the include guards this is NOT
a problem), then it's easy to forget to include BOTH header1.h and
header2.h in foo.c, but you might not figure this out until a
big re-organization of what's in header1.h and header2.h so they
don't include each other.

You can use something like "grep -v" to not see the errors but there
are still fundamental problems about getting a compiler to resume
parsing after such an error.

Gordon L. Burditt
.



Relevant Pages

  • Re: Compiling multiple C files with S-Function
    ... is this way of compiling multiple C files officially supported? ... I'm doing my main programming work in a source file MyCustomBlock.c. ... I compile this on the MATLAB command line as follows: ... I also have a header file, ...
    (comp.soft-sys.matlab)
  • Utility to ensure appropriate headers were included
    ... compiled fine on the majority of systems. ... failed to compile. ... a header file that's included directly in the source file? ...
    (comp.lang.c)
  • Compiling multiple C files with S-Function
    ... I'm doing my main programming work in a source file MyCustomBlock.c. ... I compile this on the MATLAB command line as follows: ... I also have a header file, ...
    (comp.soft-sys.matlab)
  • Re: Thoughts on file organisation
    ... Any header file should compile fine if it ... is included at the top of a source file. ... actually read header files with guards twice. ...
    (comp.lang.c)
  • Re: managing header files
    ... hundred by commenting out some external declarations, ... complaints) but the complaints about redundant declarations and the complaints ... to simplify the problem I decided to just try to compile ... approach of, in effect, finding one header file that all the .c files ...
    (comp.lang.c.moderated)