__FILE__ macro shows full path

__line__
gcc __base_file__
__func__ macro
__date__
where is __line__ defined
printf __line__
filename
__file__ php

The standard predefined macro __FILE__ available in C shows the full path to the file. Is there any way to short the path? I mean instead of

/full/path/to/file.c

I see

to/file.c

or

file.c

Try

#include <string.h>

#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)

For Windows use '\\' instead of '/'.

Bug #1716347 “Feature request: replace __FILE__ with file's base , to replace __FILE__ with the basename of a file, not the complete (https://​stackoverflow.com/questions/8487986/file-macro-shows-full-path)  /FC is also needed if you want to see the full path of a file name when using the __FILE__ macro. See Predefined Macros for more information on __FILE__. The /FC option is implied by /ZI. For more information about /ZI, see /Z7, /Zi, /ZI (Debug Information Format). /FC outputs full paths in lower case.

Here's a tip if you're using cmake. From: http://public.kitware.com/pipermail/cmake/2013-January/053117.html

I'm copying the tip so it's all on this page:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__FILENAME__='\"$(subst
  ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'")

If you're using GNU make, I see no reason you couldn't extend this to your own makefiles. For example, you might have a line like this:

CXX_FLAGS+=-D__FILENAME__='\"$(subst $(SOURCE_PREFIX)/,,$(abspath $<))\"'"

where $(SOURCE_PREFIX) is the prefix that you want to remove.

Then use __FILENAME__ in place of __FILE__.

extract filename from __FILE__ at COMPILE-TIME! - MSDN, __FILE__ macro contains filename with some path info. sometimes it still shows full-path, and i need this trick all over the files. Thursday  The overriden "new" uses the __FILE__ and __LINE__ macros. These behave as per documentation when used from a "C" source file, but when used from an inline function defined in a header file, the macro resolves incorrectly with the full path, instead of ".\myfile.h".

I have just thought of a great solution to this that works with both source and header files, is very efficient and works on compile time in all platforms without compiler-specific extensions. This solution also preserves the relative directory structure of your project, so you know in which folder the file is in, and only relative to the root of your project.

The idea is to get the size of the source directory with your build tool and just add it to the __FILE__ macro, removing the directory entirely and only showing the file name starting at your source directory.

The following example is implemented using CMake, but there's no reason it wouldn't work with any other build tools, because the trick is very simple.

On the CMakeLists.txt file, define a macro that has the length of the path to your project on CMake:

# The additional / is important to remove the last character from the path.
# Note that it does not matter if the OS uses / or \, because we are only
# saving the path size.
string(LENGTH "${CMAKE_SOURCE_DIR}/" SOURCE_PATH_SIZE)
add_definitions("-DSOURCE_PATH_SIZE=${SOURCE_PATH_SIZE}")

On your source code, define a __FILENAME__ macro that just adds the source path size to the __FILE__ macro:

#define __FILENAME__ (__FILE__ + SOURCE_PATH_SIZE)

Then just use this new macro instead of the __FILE__ macro. This works because the __FILE__ path will always start with the path to your CMake source dir. By removing it from the __FILE__ string the preprocessor will take care of specifying the correct file name and it will all be relative to the root of your CMake project.

If you care about the performance, this is as efficient as using __FILE__, because both __FILE__ and SOURCE_PATH_SIZE are known compile time constants, so it can be optimized away by the compiler.

The only place where this would fail is if you're using this on generated files and they're on a off-source build folder. Then you'll probably have to create another macro using the CMAKE_BUILD_DIR variable instead of CMAKE_SOURCE_DIR.

__FILE__ - C and C++ Syntax Reference, The __FILE__ macro expands to the full path of the current source file. __FILE​__ is a preprocessor macro that expands to full path to the current file. __FILE__​  The output file contain full paths to source files. This is not wanted behaviour. Comments. The typical reason for this problem is that the application uses the assert macro which in turn uses the macro __FILE__ which expands to the full path of the current file. Suggestions. Avoid Assert - By using conditional compilation, assert-messages can

Purely compile time solution here. It's based on the fact that sizeof() of a string literal returns its length+1.

#define STRIPPATH(s)\
    (sizeof(s) > 2 && (s)[sizeof(s)-2] == '/' ? (s) + sizeof(s) - 1 : \
    sizeof(s) > 3 && (s)[sizeof(s)-3] == '/' ? (s) + sizeof(s) - 2 : \
    sizeof(s) > 4 && (s)[sizeof(s)-4] == '/' ? (s) + sizeof(s) - 3 : \
    sizeof(s) > 5 && (s)[sizeof(s)-5] == '/' ? (s) + sizeof(s) - 4 : \
    sizeof(s) > 6 && (s)[sizeof(s)-6] == '/' ? (s) + sizeof(s) - 5 : \
    sizeof(s) > 7 && (s)[sizeof(s)-7] == '/' ? (s) + sizeof(s) - 6 : \
    sizeof(s) > 8 && (s)[sizeof(s)-8] == '/' ? (s) + sizeof(s) - 7 : \
    sizeof(s) > 9 && (s)[sizeof(s)-9] == '/' ? (s) + sizeof(s) - 8 : \
    sizeof(s) > 10 && (s)[sizeof(s)-10] == '/' ? (s) + sizeof(s) - 9 : \
    sizeof(s) > 11 && (s)[sizeof(s)-11] == '/' ? (s) + sizeof(s) - 10 : (s))

#define __JUSTFILE__ STRIPPATH(__FILE__)

Feel free to extend the conditional operator cascade to the maximum sensible file name in the project. Path length doesn't matter, as long as you check far enough from the end of the string.

I'll see if I can get a similar macro with no hard-coded length with macro recursion...

Come up with a way to shorten long lines that reference __FILE__ , lines using the __FILE__ macro, because this expands to the absolute. ://​stackoverflow.com/questions/8487986/file-macro-shows-full-path. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. __FILE__ macro shows full path. 2 'Reverse' a

At least for gcc, the value of __FILE__ is the file path as specified on the compiler's command line. If you compile file.c like this:

gcc -c /full/path/to/file.c

the __FILE__ will expand to "/full/path/to/file.c". If you instead do this:

cd /full/path/to
gcc -c file.c

then __FILE__ will expand to just "file.c".

This may or may not be practical.

The C standard does not require this behavior. All it says about __FILE__ is that it expands to "The presumed name of the current source file (a character string literal)".

An alternative is to use the #line directive. It overrides the current line number, and optionally the source file name. If you want to override the file name but leave the line number alone, use the __LINE__ macro.

For example, you can add this near the top of file.c:

#line __LINE__ "file.c"

The only problem with this is that it assigns the specified line number to the following line, and the first argument to #line has to be a digit-sequence so you can't do something like

#line (__LINE__-1) "file.c"  // This is invalid

Ensuring that the file name in the #line directive matches the actual name of the file is left as an exercise.

At least for gcc, this will also affect the file name reported in diagnostic messages.

__FILE__ macro shows full path, __FILE__ macro shows full path. __func__ macro __date__ where is __line__ defined iar file filename file type c++ __  The Path property in Excel VBA returns the complete, saved path to the workbook (Excel file). The FullName property in Excel VBA returns the complete, saved path, including the name of the workbook. Download path-fullname.xlsm and add it to "C:\test\" Place a command button on your worksheet and add the following code lines: 1.

__FILE__ macro expands to absolute path when used from included , The standard predefined MACRO __FILE__ available in C shows the full path to the file. Is there any way to short the path? I mean instead of __FILE__ expands to a character string literal. To ensure that the full path to the file is displayed, use /FC (Full Path of Source Code File in Diagnostics). This macro is always defined. __LINE__ Defined as the integer line number in the current source file. The value of the __LINE__ macro can be changed by using a #line directive. This macro

__FILE__ macro manipulation handling at compile time, __FILE__ macro expands to absolute path when used from included header to include an absolute path, when the macro is invoked by a header file included from the main source file being compiled. Show comments 2  The union allocates one member which is large enough to hold the full path minus extension and null terminator. And it allocates one member which is large enough to hold the full path minus extension, though with a null terminator. The member which is too small to hold the full __FILE__ is initialized with as much of __FILE__ as can fit. This

__FILE__ macro returns full path - solution, Since __FILE__ is constant and known at compile time this might make it easier. There is currently no way of doing full string processing at compile time (the constexpr int32_t basename_index (const char * const path, const int32_t index components and this should show up as -D__FILENAME__=\"MyFile.cpp\" in  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)?

Comments
  • It would be really really great to find a preprocessor-only solution. I'm afraid that the suggestions based on string operations will execute at runtime.
  • Since you're using gcc, I think you can change what __FILE__ contains by changing the filename you pass on the command line. So instead of gcc /full/path/to/file.c, try cd /full/path/to; gcc file.c; cd -;. Of course there's a bit more to it than that if you're relying on gcc's current directory for the include path or output file location. Edit: the gcc docs suggest that it's the full path, not the input file name argument, but that's not what I'm seeing for gcc 4.5.3 on Cygwin. So you may as well try it on Linux and see.
  • GCC 4.5.1 (built for arm-none-eabi specifically) uses the exact text of the file name on its command line. In my case it was the IDE's fault for invoking GCC with all file names fully qualified instead of putting the current directory somewhere sensible (location of the project file, perhaps?) or configurable and using relative paths from there. I suspect a lot of IDEs do that (especially on Windows) out of some sort of discomfort related to explaining where the "current" directory really is for a GUI app.
  • @SteveJessop - hope you read this comment. I have a situation where I see __FILE__ printed as ../../../../../../../../rtems/c/src/lib/libbsp/sparc/leon2/../../shared/bootcard.c and I want to know where gcc compiled the file such that this file is relatively located like it is shown.
  • This question is not a dupe of the linked one. For one, the linked one is about C++, and the answers consequently delves into C++ macro esoterica. Second, there is nothing in OP's question which mandates a macro solution. It only solemnly points out a problem and asks an open ended question.
  • / is a valid path separator in Windows.
  • / is a valid path separator in file names passed to CreateFile() and so forth. However, that doesn't always mean that you can use / just anywhere in Windows since there is a tradition (inherited from CPM) of using / as the argument lead in at the command prompt. But a quality tool would be careful to split file names at both slash and backslash characters to avoid any problems for folks that do manage to use /.
  • @AmigableClarkKant, no you can mix both separators in the same file name.
  • If your platform supports it char* fileName = basename(__FILE__); It's definitely there in Linux and OS X, don't know about Windows though.
  • This could be shortened to strrchr("/" __FILE__, '/') + 1. By prepending "/" to __FILE__, strrchr is be guaranteed to find something, and thus the conditional ?: is no longer needed.
  • I am afraid then this doesn't work for the FILE referenced in header file.
  • Agree with @BaiyanHuang but not sure that the comment is clear. __FILE__ is not a simple preprocessor symbol, it changes to the current file is often used for emitting the name of the current file (header or source module). This __FILENAME__ would only have the outermost source
  • This answer's solution is not portable since it uses Bourne shell escapes. Better to use CMake to implement it in a clean and portable way. See define_file_basename_for_sources macro answer here.