9

I like the GNU linker functionality to wrap functions a lot. I normally use it mock e.g. nondeterministic function calls like rand(). Consider the following example where I would like to write a unit test for giveMeANumber:

//number.cpp
int giveMeANumber() {
  return rand() % 6 + 1;
}

I can wrap the call to rand with the GNU linker functionality wrap like this:

//test.cpp
extern "C" int __wrap_rand(void) {
return 4;
}

void unitTest() {
  assert giveMeANumber() == 5;
}

$ g++ test.cpp -o test number.o -Xlinker --wrap=rand

Is there any way to do the same with normal C++ functions? The following does not work, I guess it is because of name mangling. But even when I try it with the mangled name it does not work.

//number.cpp
int foo() {
  //some complex calculations I would like to mock
}
int giveMeANumber() {
  return foo() % 6 + 1;
}

//test.cpp
extern "C" int __wrap_foo(void) {
return 4;
}

$ g++ test.cpp -o test number.o -Xlinker --wrap=foo
3
  • Actually, rand is a deterministic function. It returns a pseudo-random number sequence. Calls the rand will return the same sequence of numbers for any given seed used with srand.
    – TJD
    Commented Dec 6, 2011 at 15:39
  • 2
    Okay, bravo, that's true. But to keep the example short and readable, I didn't use a seed.
    – Mike
    Commented Dec 6, 2011 at 15:46
  • You need to rename __wrap_foo to getRandomNumber. (xkcd.com/221)
    – Jim Buck
    Commented Dec 6, 2011 at 18:43

2 Answers 2

11

You need to either also extern "C" the function you want to wrap (if that's possible) or you need to wrap the mangled name, e.g., __wrap__Z3foov and then pass --wrap=_Z3foov to the linker.

Getting the underscores right is a little tricky. This works for me:

$ cat x.cc
#include <iostream>
using namespace std;

int giveMeANumber();

int main() {
    cerr << giveMeANumber() << endl;
    return 0;
}

$ cat y.cc
int giveMeANumber() {
    return 0;
}

extern "C" int __wrap__Z13giveMeANumberv() {
    return 10;
}

$ g++ -c x.cc y.cc && g++ x.o y.o -Wl,--wrap=_Z13giveMeANumberv && ./a.out
10
4
  • C linkage for the to be wrapped C++ function is not an option because this function could be overloaded and it should still be possible to call it from other C++ functions. I tried to wrap the mangled name, but it does not work. What I did is this: extern "C" int __wrap__Z3foov(void) { return 4; } void thisIsATest() { assert 5 == giveMeANumber(); } together with the linker option -Xlinker --wrap=_Z3foov. Unfortunately, the real foo function is still being called. Any ideas?
    – Mike
    Commented Dec 6, 2011 at 22:35
  • I added an example. Took me a while to get all the underscores right.
    – smparkes
    Commented Dec 6, 2011 at 23:04
  • And thanks for the question. I didn't even know about --wrap. Very nice.
    – smparkes
    Commented Dec 6, 2011 at 23:05
  • if we change the name of function "giveMeANumber" it call the real function and note the _wrap__Z13....v are their a relation between the name of function and the prefix ??
    – Amirouche
    Commented Oct 8, 2019 at 10:48
0

It seems like you are trying to mock functions and classes for testing. Have you consider using Google Mock

2
  • 1
    Using Google mock is not an option for me. I want to accomplish this solely with the help of the GNU linker.
    – Mike
    Commented Dec 6, 2011 at 16:21
  • Google Mock relies on virtual methods. The wrap option is extremely useful for mocking non-virtual methods.
    – Leonardo
    Commented Nov 9, 2022 at 4:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.