2

I am wondering if it's possible to display full file path using the assert macro? I cannot specify full file path in compilation command, is there still a way to do it?

My debug environment is linux/g++

Anycorn
  • 50,217
  • 42
  • 167
  • 261
  • Do you mean the full path for the file the assertion that failed is contained in? –  Mar 15 '10 at 18:48
  • @Neil yes, as far as I can tell assert.h uses __FILE__. Ideally, would like some portable way to print full file path when assertion failed – Anycorn Mar 15 '10 at 18:50
  • 2
    Portably, I think you are out of luck. –  Mar 15 '10 at 18:52

2 Answers2

4

You can add the following macro option into your compilation line (Can be easily modify for your environment)

%.o: %.cpp
    $(CC) $(CFLAGS) -D__ABSFILE__='"$(realpath $<)"' -c $< -o $@

then you just have to this to have your full path:

#include <stdio.h>
int main()
{
  printf(__ABSFILE__);
  // will be expanded as: printf("/tmp/foo.c")
}

EDIT

Even better than this: The following rules is enough !!!

%.o: %.cpp
    $(CC) $(CFLAGS) -c $(realpath $<) -o $@

And you can now use __FILE__ macro:

#include <stdio.h>
int main()
{
  printf(__FILE__);
  // will be expanded as: printf("/tmp/foo.c")
}
Phong
  • 6,600
  • 4
  • 32
  • 61
  • You can still redefine the macro `__FILE__` instead of `__ABSFILE__`. I don't recommend it though (ery ugly) – Phong Mar 16 '10 at 02:18
  • 1
    This will work, however it will give confusing results for code in header files -- it will expand to the name of the source file that includes the header, not the header file itself. – Adam Rosenfield Mar 16 '10 at 02:20
  • @adam: +1 You are right, I found a other way to do it (cf edit part). It should be fine now. – Phong Mar 16 '10 at 02:40
0

assert uses the __FILE__ macro to get the filename. It will expand to the path used during compilation. For example, using this program:

#include <stdio.h>
int main(void)
{
    printf("%s\n", __FILE__);
    return 0;
}

If I compile with 'gcc -o foo foo.c' and run, I'll get this output:

foo.c

But if I compile with 'gcc -o foo /tmp/foo.c' and run, I'll get this output instead:

/tmp/foo.c
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
JayM
  • 4,798
  • 1
  • 21
  • 15
  • thanks, I know about that. However I cannot specify full path without rewriting autoconf – Anycorn Mar 15 '10 at 19:32
  • See http://www.gnu.org/software/hello/manual/autoconf/Particular-Headers.html . What do you mean by "without rewriting autoconf" ? – Yktula Mar 15 '10 at 20:10
  • @Yktula when you compile with autoconf/automake, relative file paths are used. – Anycorn Mar 15 '10 at 20:46
  • As long as you can get the whole relative path, why do you need the absolute path? – JayM Mar 15 '10 at 22:01
  • @Jaym I am trying to integrate running some program under emacs, and jumping to a file with the problem using shortcut. It's problematic when there is no full path. – Anycorn Mar 15 '10 at 22:24
  • Have a look at http://stackoverflow.com/questions/1275476/gcc-gdb-how-to-embed-absolute-path-to-source-file-in-debug-information – Yktula Mar 16 '10 at 02:18