CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Mar 2009
    Posts
    12

    Question how to disable __FILE__ ?

    I have this C++ library that uses __LINE__ and _FILE__ preprocessor directives all over the place,
    and I would like to disable it or turn it off without having to explicitly remove them myself.

    They insert source code directories into my compiled files, making them much larger than they have to be.
    Even in release mode, they're in there.

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: how to disable __FILE__ ?

    Are you sure that this is really a problem? /GF compiler option allows to have only one copy of every string in executable file. So, total number of strings added to executable by this library is equal to number of source files in the library.
    __FILE__ and __LINE__ macros are very useful for error handling and diagnostics. It is a good idea to learn from library writers, how to use this feature, instead of removing it.
    http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx

  3. #3
    Join Date
    Mar 2009
    Posts
    12

    Unhappy Re: how to disable __FILE__ ?

    Quote Originally Posted by Alex F View Post
    Are you sure that this is really a problem? /GF compiler option allows to have only one copy of every string in executable file. So, total number of strings added to executable by this library is equal to number of source files in the library.
    __FILE__ and __LINE__ macros are very useful for error handling and diagnostics. It is a good idea to learn from library writers, how to use this feature, instead of removing it.
    http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx
    Thanks, I understand their purpose, but that doesn't diminish the fact I want them removed from my release build.
    I take it from your response, there's no way to turn them off.

  4. #4
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: how to disable __FILE__ ?

    That's strange.
    I just wrote a small test and __FILE__ resolves to a full path in debug builds but just to the filename in release builds.
    Which version of VC++ are you using?
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  5. #5
    Join Date
    Mar 2009
    Posts
    12

    Re: how to disable __FILE__ ?

    Quote Originally Posted by Marc G View Post
    That's strange.
    I just wrote a small test and __FILE__ resolves to a full path in debug builds but just to the filename in release builds.
    Which version of VC++ are you using?
    MSVC++ 2008. I can see the full filepaths in my hex editor. I'm compiling a dynamic library (.dll) if that matters.

    I thought I just ask to see if there's an easy way to get rid of them, before I start digging into the source code
    and remove them by hand.

    Typically, I see most people use a #define switch and set them to empty values, but in some cases, I guess they're being lazy and
    forget about it.
    Last edited by sinclaire; February 19th, 2011 at 06:11 PM.

  6. #6
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: how to disable __FILE__ ?

    Quote Originally Posted by sinclaire View Post
    Typically, I see most people use a #define switch and set them to empty values, but in some cases, I guess they're being lazy and
    forget about it.
    Never guess and forget before being sure.
    Quote Originally Posted by sinclaire View Post
    I thought I just ask to see if there's an easy way to get rid of them, before I start digging into the source code
    and remove them by hand.
    Be sure that "digging into the source code and remove them by hand" is "lazier" than using a #define preprocessor directive.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  7. #7
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: how to disable __FILE__ ?

    Quote Originally Posted by sinclaire View Post
    Typically, I see most people use a #define switch and set them to empty values, but in some cases, I guess they're being lazy and
    forget about it.
    That will not work on some compilers.
    For example:
    Code:
    #undef __FILE__
    or even
    Code:
    #define __FILE__ ""
    If you compile this with VC++2010, the compiler will issue a warning saying that __FILE__ is reserved and that it will simply ignore the #define or #undef.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  8. #8
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: how to disable __FILE__ ?

    Quote Originally Posted by Marc G View Post
    If you compile this with VC++2010, the compiler will issue a warning saying that __FILE__ is reserved and that it will simply ignore the #define or #undef.
    Good catch, Marc!
    Even the old VC6.0 gives a C4117 warning then ignores #define __FILE__.

    The standard states:
    • The values of the predefined macros (except for_ _LINE_ _ and_ _FILE_ _) remain constant throughout the translation unit.
    • If any of the predefined macro names in this subclause, or the identifier defined, is the subject of a #define or a #undef preprocessing directive, the behavior is undefined.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: how to disable __FILE__ ?

    Quote Originally Posted by sinclaire View Post
    MSVC++ 2008. I can see the full filepaths in my hex editor. I'm compiling a dynamic library (.dll) if that matters.

    I thought I just ask to see if there's an easy way to get rid of them,
    The easy way is to create your own define, and then do a global search and replace.
    Code:
    #ifdef SHOW_FILE_LINE
        #define MY_FILE_MACRO   __FILE__
        #define MY_LINE_MACRO  __LINE__
    #else
        #define MY_FILE_MACRO  
        #define MY_LINE_MACRO  
    #endif
    And then replace __FILE_ with MY_FILE_MACRO, and __LINE__ with MY_LINE_MACRO.
    Typically, I see most people use a #define switch and set them to empty values,
    I have never seen this used. Who are these "most people"?

    You can't undefine a compiler preprocessor symbol such as __FILE__ and __LINE__ without invoking undefined behaviour. The __FILE__ and __LINE__ macros are not only compiler macros, they are mandated by the ANSI C++ rules to perform as described -- you can't change them.

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Mar 2009
    Posts
    12

    Re: how to disable __FILE__ ?

    I meant the macro or function that uses __FILE__ is set to a "do nothing" state. I didn't give an example, so you assumed wrong.

    Be sure that "digging into the source code and remove them by hand" is "lazier" than using a #define preprocessor directive.
    On the contrary, it's not my code. I'm not obligated to fix it. I'll hack and slash as I wish.

    Thanks.

  11. #11
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: how to disable __FILE__ ?

    Quote Originally Posted by sinclaire View Post
    I meant the macro or function that uses __FILE__ is set to a "do nothing" state. I didn't give an example, so you assumed wrong.
    If you have a problem, it's good to provide as much as possible details.
    If you post a quiz, it's nice to specify that.
    // Here is a discussion forum and not an interview.
    Quote Originally Posted by sinclaire View Post
    On the contrary, it's not my code. I'm not obligated to fix it.
    Well then, don't do it!
    Quote Originally Posted by sinclaire View Post
    I'll hack and slash as I wish.
    Not so fair...
    Thanks.
    You are welcome!
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  12. #12
    Join Date
    Mar 2009
    Posts
    12

    Re: how to disable __FILE__ ?

    I mentioned a "define switch". This is a define switch:

    Code:
    #ifdef DEBUG
    // do something
    #else
    // do nothing
    #endif
    I didn't say a thing about redefining __FILE___. You guys brought that up.
    Thanks.

  13. #13
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: how to disable __FILE__ ?

    Quote Originally Posted by sinclaire View Post
    I mentioned a "define switch". This is a define switch:

    Code:
    #ifdef DEBUG
    // do something
    #else
    // do nothing
    #endif
    I didn't say a thing about redefining __FILE___. You guys brought that up.
    Thanks.
    Mmm, but you wrote "I have this C++ library that uses __LINE__ and _FILE__ preprocessor directives all over the place,
    and I would like to disable it or turn it off without having to explicitly remove them myself." in your first post...
    Disabling or turning it off -> redefine.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured