| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| Hi, I've a question about preprocessing of code. Let's say I've these three files: // File: b.h #ifndef __MY_B_H #define __MY_B_H typedef int int_least8_t; typedef unsigned int uint_least8_t; #endif ///////////// // File b.c #include "b.h" int main() { return 0; } ///////////// // File a.c #include "b.h" int foo() { return; } ///////////// I would like to get the preprocessed file where all includes and macros are resolved. My idea was to use "gcc -E b.c a.c". I get: # 1 "b.c" # 1 "<built-in>" # 1 "<command line>" # 1 "b.c" # 1 "b.h" 1 typedef int int_least8_t; typedef unsigned int uint_least8_t; # 2 "b.c" 2 int main() { return 0; } # 1 "a.c" # 1 "<built-in>" # 1 "<command line>" # 1 "a.c" # 1 "b.h" 1 typedef int int_least8_t; typedef unsigned int uint_least8_t; # 2 "a.c" 2 int foo() { return; } What I don't understand is why the header file was included twice. Since __MY_B_H was defined while including b.h from b.c I would assume that a.c would not include b.h any more. However, this happens. Why? And how can I achieve to have b.h included exactly once? Regards, Tim |
|
#2
| |||
| |||
| Tim Frink said: <snip> > I would like to get the preprocessed file where all includes and > macros are resolved. My idea was to use "gcc -E b.c a.c". <snip> > What I don't understand is why the header file was included twice. You'd have to ask the gcc folks to be absolutely sure, but I presume gcc is translating the input files *one at a time*, just as it would if you hadn't specified -E. > Since __MY_B_H was defined while including b.h from b.c I would assume > that a.c would not include b.h any more. However, this happens. > Why? > And how can I achieve to have b.h included exactly once? I don't think there's a satisfactory answer. /* a.c */ #include "b.c" int foo() { return; } gcc -E a.c is not what I would call satisfactory. :-) -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ Google users: <http://www.cpax.org.uk/prg/writings/googly.php> "Usenet is a strange place" - dmr 29 July 1999 |
|
#3
| |||
| |||
| On 27 Aug, 17:21, Tim Frink <plfr...@yahoo.de> wrote: > Hi, > > I've a question about preprocessing of code. Let's say I've these > three files: > > // File: b.h > #ifndef __MY_B_H > #define __MY_B_H > > typedef int int_least8_t; > typedef unsigned int uint_least8_t; > > #endif > ///////////// > > // File b.c > #include "b.h" > > int main() > { > return 0;} > > ///////////// > > // File a.c > #include "b.h" > > int foo() > { > return;} > > ///////////// > > I would like to get the preprocessed file where all includes and > macros are resolved. My idea was to use "gcc -E b.c a.c". > > I get: > > # 1 "b.c" > # 1 "<built-in>" > # 1 "<command line>" > # 1 "b.c" > # 1 "b.h" 1 > > typedef int int_least8_t; > typedef unsigned int uint_least8_t; > # 2 "b.c" 2 > > int main() > { > return 0;} > > # 1 "a.c" > # 1 "<built-in>" > # 1 "<command line>" > # 1 "a.c" > # 1 "b.h" 1 > > typedef int int_least8_t; > typedef unsigned int uint_least8_t; > # 2 "a.c" 2 > > int foo() > { > return; > > } > > What I don't understand is why the header file was included twice. > Since __MY_B_H was defined while including b.h from b.c I would assume > that a.c would not include b.h any more. However, this happens. > Why? Didn't you tell each file to include b.h? If you compiled them together maybe the preprocessor just did as it was told. If you compile a source file that has #include "b.h" #include "b.h" #include "b.h" then b.h _should_ be included three times, should it not? Given this, if b.h has at the top #ifndef X #define X then only the first b.h will have its definitions recognised. The definitions other two will be ignored even though they appear in the source. Maybe you meant to put the #if... around the #include rather than within the included file? I've no idea if this is supported. Maybe a C expert will correct me as needed. They usually do. :-) BTW comp.lang.c may be a better NG if you are talking about the C preprocessor. |
|
#4
| |||
| |||
| On 27 Aug, 17:45, Richard Heathfield <r...@see.sig.invalid> wrote: > Tim Frink said: > > <snip> > > > I would like to get the preprocessed file where all includes and > > macros are resolved. My idea was to use "gcc -E b.c a.c". > > <snip> > > > What I don't understand is why the header file was included twice. > > You'd have to ask the gcc folks to be absolutely sure, but I presume gcc is > translating the input files *one at a time*, just as it would if you > hadn't specified -E. > > > Since __MY_B_H was defined while including b.h from b.c I would assume > > that a.c would not include b.h any more. However, this happens. > > Why? > > And how can I achieve to have b.h included exactly once? > > I don't think there's a satisfactory answer. > > /* a.c */ > #include "b.c" > int foo() > { > return; > > } > > gcc -E a.c > > is not what I would call satisfactory. :-) Indeed. I think you called this a lousy, lousy idea: http://groups.google.co.uk/group/com...e26ac91b?hl=en Shame on you for suggesting it. :-) Unless, of course, someone is still masquerading as you.... |
|
#5
| |||
| |||
| James Harris said: > On 27 Aug, 17:45, Richard Heathfield <r...@see.sig.invalid> wrote: >> Tim Frink said: >> <snip> >> > And how can I achieve to have b.h included exactly once? >> >> I don't think there's a satisfactory answer. >> >> /* a.c */ >> #include "b.c" >> int foo() >> { >> return; >> >> } >> >> gcc -E a.c >> >> is not what I would call satisfactory. :-) > > Indeed. I think you called this a lousy, lousy idea: I did, yes. > Shame on you for suggesting it. :-) I mentioned it only for completeness. I think it's clear from my tone that I wasn't recommending it! :-) > Unless, of course, someone is still masquerading as you.... If a chap like you can't recognise my absolutely unique style by now, I can only suppose that someone is masquerading as *you*. :-) -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ Google users: <http://www.cpax.org.uk/prg/writings/googly.php> "Usenet is a strange place" - dmr 29 July 1999 |
|
#6
| |||
| |||
| "Tim Frink" <plfriko@yahoo.de> wrote in message news:g93uu7$6ht$2@janice.cs.uni-dortmund.de... > Hi, > > I've a question about preprocessing of code. Let's say I've these > three files: > <snip> > > What I don't understand is why the header file was included twice. > Since __MY_B_H was defined while including b.h from b.c I would assume > that a.c would not include b.h any more. However, this happens. > Why? > And how can I achieve to have b.h included exactly once? > one possible idea worth trying: make a single big "super" source file, which serves to #include the other source files (#include is textual, so it can include source files as well as headers). this way, you can preprocess/compile this single file, and it may well give you what you want in this case. this is also an approach used in some smaller libraries and tools as well, namely rather than having a bunch of separate compilation and linking, all the contents are included into a single super source file which is compiled all at once, often producing a single object file. > Regards, > Tim |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.