断言宏中的C / C ++完整文件路径 [英] C/C++ full file path in assert macro

查看:147
本文介绍了断言宏中的C / C ++完整文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用assert宏显示完整的文件路径?
我无法在编译命令中指定完整的文件路径,还有一种方法吗?



我的调试环境是linux / g ++

解决方案

您可以将以下宏选项添加到编译行中(可以轻松地修改您的环境)

 %。o:%.cpp 
$(CC)$(CFLAGS)-D__ABSFILE __ ='$(realpath $<)'-c $ < -o $ @

那么你只需要这个具有你的完整路径:

  #include< stdio.h> 
int main()
{
printf(__ ABSFILE__);
//将扩展为:printf(/ tmp / foo.c)
}


b $ b

EDIT



甚至比这更好:以下规则就足够了!!!

 %。o:%.cpp 
$(CC)$(CFLAGS)-c $(realpath $ <)-o $ @ $ b现在你可以使用 __ FILE __ 宏: $

p>

  #include< stdio.h> 
int main()
{
printf(__ FILE__);
//将展开为:printf(/ tmp / foo.c)
}


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

解决方案

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")
}

这篇关于断言宏中的C / C ++完整文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆