Thread: conflict in FILE and stdio

  1. #16
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    That's matsp's point, you can't find where __FILE is defined because it maybe defined in that manor. Ie, '__FILE' is never actually written in the file.

    You could try to search for __FILE in all the files in /usr/include using grep.
    Code:
    grep -r "__FILE" /usr/include/*.h
    I found '/usr/include/stdio.h:typedef struct _IO_FILE __FILE;' on my machine, but I guess it could be different to yours.
    Last edited by zacs7; 10-09-2007 at 10:17 PM.

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Exactly my point. There are several macro definitions that are used by gcc's header files that make it very difficult to find the actual name used. The whole thing is quite convoluted, and that is because the GCC header files are intended to work with many different versions of gcc, different OS's and different C-libraries, each of which may be slightly different in the way things need to be defined.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks zacs7,


    Quote Originally Posted by zacs7 View Post
    That's matsp's point, you can't find where __FILE is defined because it maybe defined in that manor. Ie, '__FILE' is never actually written in the file.

    You could try to search for __FILE in all the files in /usr/include using grep.
    Code:
    grep -r "__FILE" /usr/include/*.h
    I found '/usr/include/stdio.h:typedef struct _IO_FILE __FILE;' on my machine, but I guess it could be different to yours.
    Do you know why

    #define __FILE something
    typedef struct _IO_FILE __FILE

    works, but

    typedef struct _IO_FILE __FILE
    #define __FILE something

    Does not work?


    regards,
    George
    Last edited by George2; 10-11-2007 at 07:15 AM.

  4. #19
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    Quote Originally Posted by matsp View Post
    Exactly my point. There are several macro definitions that are used by gcc's header files that make it very difficult to find the actual name used. The whole thing is quite convoluted, and that is because the GCC header files are intended to work with many different versions of gcc, different OS's and different C-libraries, each of which may be slightly different in the way things need to be defined.

    --
    Mats
    Do you know why

    #define __FILE something
    typedef struct _IO_FILE __FILE

    works, but

    typedef struct _IO_FILE __FILE
    #define __FILE something

    Does not work?


    regards,
    George

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Presumably, based on the error message you have given, because "something" has already been defined elsewhere...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #21
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    You could also use gcc options to output all the macros define in your code. In fact, you can do quite a lot of stuff...

    Code:
    gcc -E -o ListDef -dM -P YourSrcCode.c
    will generate a list of `#define' directives for all the macros defined during the execution of the preprocessor, including predefined macros. You might not find __FILE in here because, from what i saw from the result of

    Code:
    gcc -E -o ListDef -dD -P YourSrcCode.c
    well, __FILE is used in a type definition and used one or two lines after that. So, having a macro expansion for __FILE as something else than a type will result in an error here.

    Note that i'm a poor Windows user (yeah, i'll have to install one of those Linux distribution one day heh), thus i don't have a really young version of gcc (3.4.4).

    But you may be able to investigate further using the gcc options controlling the preprocessor.

    If you have the latest version of gcc, you'll find here some lovely informations.

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Foxman has the solution, assuming you are able to read that output.

    I would, however, recommend that you just write it down as "One of those things".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #23
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks foxman,


    Quote Originally Posted by foxman View Post
    You could also use gcc options to output all the macros define in your code. In fact, you can do quite a lot of stuff...

    Code:
    gcc -E -o ListDef -dM -P YourSrcCode.c
    will generate a list of `#define' directives for all the macros defined during the execution of the preprocessor, including predefined macros. You might not find __FILE in here because, from what i saw from the result of

    Code:
    gcc -E -o ListDef -dD -P YourSrcCode.c
    well, __FILE is used in a type definition and used one or two lines after that. So, having a macro expansion for __FILE as something else than a type will result in an error here.

    Note that i'm a poor Windows user (yeah, i'll have to install one of those Linux distribution one day heh), thus i don't have a really young version of gcc (3.4.4).

    But you may be able to investigate further using the gcc options controlling the preprocessor.

    If you have the latest version of gcc, you'll find here some lovely informations.
    What is the differences between #define and typedef in internal operation of compiler? I find redefine a typedef type is error, but re-typedef a typedef type is ok.


    regards,
    George

  9. #24
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    Quote Originally Posted by matsp View Post
    Foxman has the solution, assuming you are able to read that output.

    I would, however, recommend that you just write it down as "One of those things".

    --
    Mats
    I agree with all of your guys' reply. I am interested to learn what is the differences between #define and typedef. Could you see my last post? Any ideas?


    regards,
    George

  10. #25
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by George2 View Post
    Thanks foxman,




    What is the differences between #define and typedef in internal operation of compiler? I find redefine a typedef type is error, but re-typedef a typedef type is ok.


    regards,
    George
    typedef tells the compiler "This is a new name for the type I've just given", e.g. "typedef int blah;" makes the type blah a valid use for everywhere you can use int.

    #define is a "text replacement" mechanism. "#define blah int" will replace every place that uses blah into int BEFORE the actual compile takes place. This is called the "preprocessor stage". The preprocessor will take the source file, combine it with any includes into one large file, and as part of that process, replace any #define stuff. [And sort out any #if/#ifdef/#else/#endif sections by removing anything that is "false".].

    Sasy now we do:
    Code:
    #define blah int
    typedef int blah;
    This will compile as:
    Code:
    typedef int int;
    Which is probably not a good thing to do.


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #26
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Guess you have to understand one thing. The compiler never sees any #define's. All the #define'd symbols have already been replaced by the preprocessor before the compiler sees the code.
    Kurt

  12. #27
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ZuK View Post
    Guess you have to understand one thing. The compiler never sees any #define's. All the #define'd symbols have already been replaced by the preprocessor before the compiler sees the code.
    Kurt
    Exactly what I tried to say, but used a few too many words.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #28
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    Quote Originally Posted by matsp View Post
    typedef tells the compiler "This is a new name for the type I've just given", e.g. "typedef int blah;" makes the type blah a valid use for everywhere you can use int.

    #define is a "text replacement" mechanism. "#define blah int" will replace every place that uses blah into int BEFORE the actual compile takes place. This is called the "preprocessor stage". The preprocessor will take the source file, combine it with any includes into one large file, and as part of that process, replace any #define stuff. [And sort out any #if/#ifdef/#else/#endif sections by removing anything that is "false".].

    Sasy now we do:
    Code:
    #define blah int
    typedef int blah;
    This will compile as:
    Code:
    typedef int int;
    Which is probably not a good thing to do.


    --
    Mats
    I think #define only affects the code after it, and no impact to code before it. So, it is important where (order and sequence) #define is used, and if used in different places, there will be different effect, right?


    regards,
    George

  14. #29
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks foxman,


    Quote Originally Posted by foxman View Post
    Code:
    gcc -E -o ListDef -dM -P YourSrcCode.c
    Code:
    gcc -E -o ListDef -dD -P YourSrcCode.c
    I have tried the above two commands. And they generate a lot of information in ListDef, several hundred lines. :-)

    Could you tell me what is the function of gcc -E -o ListDef -dM -P and gcc -E -o ListDef -dD -P please?


    regards,
    George

  15. #30
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stdio differences windows vs linux
    By keira in forum C Programming
    Replies: 6
    Last Post: 09-14-2008, 04:42 PM
  2. Permanently deleting a file
    By civix in forum C++ Programming
    Replies: 8
    Last Post: 02-24-2008, 05:02 AM
  3. Reading integer from file.
    By esben in forum C Programming
    Replies: 4
    Last Post: 03-04-2006, 12:39 PM
  4. Binary files
    By Lionmane in forum C Programming
    Replies: 35
    Last Post: 08-25-2005, 01:56 PM
  5. Read Bytes From File Into Integer
    By ChadJohnson in forum C Programming
    Replies: 28
    Last Post: 08-16-2004, 11:57 AM