linker problem with --wrap option

Hi,

I have successfully written the wrapper function. When I try to compile that it gave a linker error.

Please consider the program:
main.cpp
--------

#include "Test.h"
#include <stdio.h>

extern "C"
{
void __wrap__ZN4Test5printEP8Unneeded(Unneeded* test)
{
printf("This is wrapper function\n");
}
}

int main(int argc, void** argv)
{
Test a;
Unneeded u;
a.print(&u);
}

Test.h
------
#ifndef TEST_H_
#define TEST_H_

#include "Unneeded.h"

class Test
{
public:
void print(Unneeded *junk);
};

Test.cpp
--------
#include <stdio.h>
#include "Test.h"

void Test::print(Unneeded *junk)
{
printf("This is real function\n");
junk->sprint();
}

Unneeded.h
----------
#ifndef UNNEEDED_H_
#define UNNEEDED_H_

class Unneeded
{
private:
int i;

public:
void sprint();
};

Unneeded.cpp
------------
#include "Unneeded.h"

void Unneeded::sprint()
{

}


i have given the linker option as below:
linkopt -Wl,--wrap=_ZN4Test5printEP8Unneeded

If i don't include Test.o and Unneeded.o to create the executable, it worked fine. If i include only the Test.o, the executable asks for the definition of sprint() function.
"Test.cpp:(.text+0x19): undefined reference to `Unneeded::sprint()'"

I am wondering that why it asks for the definition of sprint() function, as it won't be called any time (please see that it worked fine, if i don't include test.o and unneeded.o). I am building only the static binary. Please help me to resolve this issue
You are callings junk->sprint(); from Test which you are linking which is why you are receiving the error. If you removed that call from Test you would be ok.
Topic archived. No new replies allowed.