我们怎样才能知道调用函数的名字? [英] How can we know the caller function's name?

查看:134
本文介绍了我们怎样才能知道调用函数的名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C语言中,我们可以使用 __ __功能来获得 电流函数的名称。
但是,如果我定义了一个名为(功能)和B()调用一个()象下面这样:

  B()
{
    一个();
}

现在在源$ C ​​$ C也有许多像B()的调用(),例如功能C(),D(),E()...

是否有可能为内()添加一些code,以检测其目前的执行()是由函数调用?

更多:


  1. 对不起,使日错字误导你。我已经改正。

  2. 我试图找出函数调用()用于调试的目的。一世
    不知道你在同样的情况时怎么办?

  3. 我的code是在VxWorks中,但我不知道是否它关系到C99或
    别的东西。


解决方案

有什么可以做的只能在

然而,一个简单的宏窍门,就可以实现你想要什么,IIUC显示来电者的姓名。

 作废()
{
    / *您code * /
}无效a_special(字符常量* caller_name)
{
    的printf(一个从%叫做caller_name);
    一个();
}#定义一个()a_special(__func__)空穴B()
{
    一个();
}

In C language, we can use __FUNCTION__ to get the current function's name. But if I define a function named a() and in b() it calls a() like below:

b()
{
    a();
}

Now in the source code there are lots of functions like b() which call a(), e.g. c(), d(), e()...

Is it possible for that within a() add some code to detect its current execution of a() is called by which function?

Further:

  1. Sorry to make th typo to misleading you. I correct it already.
  2. I try to find out which function calls a() for debug purpose. I don't know how you do when in the same situation?
  3. And my code is under vxWorks, but I am not sure whether it is related to C99 or something else.

解决方案

There's nothing you can do only in a.

However, with a simple macro trick, you can achieve what you want, IIUC showing the name of the caller.

void a()
{
    /* Your code */
}

void a_special( char const * caller_name )
{
    printf( "a was called from %s", caller_name );
    a();
}

#define a() a_special( __FUNC__)

void b()
{
    a();
}

这篇关于我们怎样才能知道调用函数的名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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