__FILE__ shows complete path.

This is a discussion on __FILE__ shows complete path. within the Linux Programming forums, part of the Platform Specific Boards category; Hello guys, For some debugging info I need __FILE__, but the problem is, it shows also the full pathname. I ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    37

    __FILE__ shows complete path.

    Hello guys,

    For some debugging info I need __FILE__, but the problem is, it shows also the full pathname. I rather don't want to do some stringmanipulation to cut off only the source name. So is there another way to get the current filename without calling a function (a MACRO is ofcourse much faster)? Maybe a setting to GCC...?

    I googled and found someone says in *nix systems __FILE__ shows only the filename, while DOS shows the complete path, I guess he was mistaken.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    Oh my god,

    this is ridiculous man! Hahahaha, it shows full pathname in debug-mode and just the filename in release mode, can you believe that? The following question rises to me, how can I turn off the full pathname anyway?
    "Build the program in release mode, duh!" No really, is there a sneaky switch I need to pass GCC?

    Thank you.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    On the other hand, it could not take more than 5 minutes to write a function that will use strrchr() to eliminate the path IF there is one.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by MK27 View Post
    On the other hand, it could not take more than 5 minutes to write a function that will use strrchr() to eliminate the path IF there is one.
    There are all kinds of scenarios where having to do that just sucks.

    If the OP would like to explain what he's using __FILE__ for, maybe we can find some alternate solution, because gcc is just broken in this regard.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by brewbuck View Post
    There are all kinds of scenarios where having to do that just sucks.


    Considering the task is always identical, you should not have to write such a function more than once in your life. And considering it's about 4 lines, it makes sense no one has bothered to include fullpath2filename() in a standard library as a convenient wrapper for strrchr().

    Sounds more like you mean, "There are all kinds of scenarios where having to write code in C just sucks". Maybe.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    Quote Originally Posted by brewbuck View Post
    -- because gcc is just broken in this regard.
    I'd like to hear what standard says about this. I checked committee draft linked in sticky in C board and some C89 draft but they didn't say what should __FILE__ include. Could someone with a copy of standard tell me what does it says about __FILE__?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by fronty
    Could someone with a copy of standard tell me what does it says about __FILE__?
    C99 and C++03 say nothing about __FILE__.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by MK27 View Post


    Considering the task is always identical, you should not have to write such a function more than once in your life. And considering it's about 4 lines, it makes sense no one has bothered to include fullpath2filename() in a standard library as a convenient wrapper for strrchr().

    Sounds more like you mean, "There are all kinds of scenarios where having to write code in C just sucks". Maybe.
    Writing the function isn't what sucks. Having to call it at runtime sucks.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by brewbuck View Post
    Writing the function isn't what sucks. Having to call it at runtime sucks.
    Yes, for such speed critical applications as:

    Quote Originally Posted by Andaluz View Post
    For some debugging info I need __FILE__,
    May the whole thing could be #ifdef'd in this case.

    BTW WRT the standard I found this here:
    http://gcc.gnu.org/onlinedocs/gcc-3....ed-Macros.html

    The standard predefined macros are specified by the C and/or C++ language standards, so they are available with all compilers that implement those standards. Older compilers may not provide all of them. Their names all start with double underscores.
    __FILE__
    This macro expands to the name of the current input file, in the form of a C string constant. This is the path by which the preprocessor opened the file, not the short name specified in #include or as the input file name argument. For example, "/usr/local/include/myheader.h" is a possible expansion of this macro.
    Last edited by MK27; 12-10-2009 at 01:11 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by MK27
    BTW WRT the standard I found this here:
    http://gcc.gnu.org/onlinedocs/gcc-3....ed-Macros.html
    *sigh* I was searching for the wrong thing. For readability, the text of C99 inserts a space between consecutive underscores. Anyway, C99 itself only states that it is "the presumed name of the current source file (a character string literal)." Naturally, the format of the name is left unspecified.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by MK27 View Post
    Yes, for such speed critical applications as:
    It has nothing to do with performance. The reason you have debug messages is for debugging. If you're printing a debug message, something has potentially gone very wrong. In a world where things are wrong, I don't want to call any function that isn't absolutely necessary. I just want to get my debug message into a log file/onto the screen and move on.

    It's like the old argument about why the Linux kernel doesn't "dump core" when it panics. The problem is, if the kernel is panicking, how can you be sure that the kernel is in a sane enough state to safely write to disk? Your core dumping routine might end up wiping out the entire FS. Thus, no kernel core dumps (it may have changed since I was involved in that debate, I don't know).
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    You won't have this issue if you're compiling in the same directory as the source file.

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by brewbuck View Post
    It has nothing to do with performance. The reason you have debug messages is for debugging. If you're printing a debug message, something has potentially gone very wrong. In a world where things are wrong, I don't want to call any function that isn't absolutely necessary. I just want to get my debug message into a log file/onto the screen and move on.
    Well, you're the pro, but it seems to me like you are saying "how can I use any code to create debugging output when the code I've already written has bugs in it?"* Maybe that's true, I just see Zeno's arrow in here somehow.

    Hard also to imagine a situation in which a full pathname is more useless than a relative one.

    * going with the core dump analogy, I guess you should just leave that in the quiver. Hard to actually fix the bugs as a developer this way tho...
    Last edited by MK27; 12-10-2009 at 02:56 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by MK27 View Post
    Well, you're the pro, but it seems to me like you are saying "how can I use any code to create debugging output when the code I've already written has bugs in it?"* Maybe that's true, I just see Zeno's arrow in here somehow.
    It's just a personal heuristic.

    Hard also to imagine a situation in which a full pathname is more useless than a relative one.
    The problem is that it CHANGES depending whether you're compiling for debug or not. The lack of consistency is the issue.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  15. #15
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by laserlight View Post
    *sigh* I was searching for the wrong thing. For readability, the text of C99 inserts a space between consecutive underscores. Anyway, C99 itself only states that it is "the presumed name of the current source file (a character string literal)." Naturally, the format of the name is left unspecified.
    Wow. . . this is a monumental day. . . laserlight made a mistake.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 09:36 AM
  3. Shortest Path Maze Solver (Breadth Search Help)
    By Raskalnikov in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 07:41 PM
  4. Can't figure out what keeps hanging up my program
    By shays in forum C Programming
    Replies: 7
    Last Post: 11-12-2007, 01:59 PM
  5. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21