tags:

views:

321

answers:

2

hello,

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++

+1  A: 

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
JayM
thanks, I know about that. However I cannot specify full path without rewriting autoconf
aaa
See http://www.gnu.org/software/hello/manual/autoconf/Particular-Headers.html . What do you mean by "without rewriting autoconf" ?
Yktula
@Yktula when you compile with autoconf/automake, relative file paths are used.
aaa
As long as you can get the whole relative path, why do you need the absolute path?
JayM
@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.
aaa
Have a look at http://stackoverflow.com/questions/1275476/gcc-gdb-how-to-embed-absolute-path-to-source-file-in-debug-information
Yktula
+5  A: 

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
You can still redefine the macro `__FILE__` instead of `__ABSFILE__`. I don't recommend it though (ery ugly)
Phong
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
@adam: +1 You are right, I found a other way to do it (cf edit part). It should be fine now.
Phong