Wrapping of function in the same file

混江龙づ霸主 提交于 2019-12-08 06:58:49

问题


I need your suggestion to wrap my existing function.

I am from testing team I need to write unit test cases, so I don't want to depend on original definition so trying to write my own definiton.

Following is the source code which should not be changed.

source.c:

#include <stdio.h>
const char *getObjectName (int *anObject);

void func()
{
    int *p;
    getObjectName(p);
}

const char *getObjectName (int *anObject)
{
    printf("i am in original\n");
}

From the above code I want to wrap getObjectName() function so that I can give my own definition.

I have googled a lot and tried following methods but didn't work out:

  • Method 1. using ld --wrap method
  • Method 2. using -DINTERCEPT
  • Method 3. using function pointer

I cannot use above 3 methods because calling function and called function are in same file.

So please suggest me any other methods to write my own defintion for getObjectName().

来源:https://stackoverflow.com/questions/43203723/wrapping-of-function-in-the-same-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!