0

When trying to wrap a system call in C++, such as lseek, the compiled code would call the actual system call instead of my mock.

Here's an example:

/* Mock the file operation lseek. */
off_t lseek(int fd, off_t offset, int whence)
{
    printf("%s \n", __PRETTY_FUNCTION__);
}

How to get the compiler to call my mocked system call rather than the one in unistd.h?

I tried using the -Wl,--wrap=lseek flag, but this failed as the linker could not find the __wrap_lseek symbol.

GNU gcc/ld - wrapping a call to symbol with caller and callee defined in the same object file

1 Answers1

0

I compiled with the g++ -c option then used the nm utility to look at the names of the symbols generated.

It turns out that the mocked system call names were being mangled by the C++ compiler. Thus, the linker would use the normal system call.

The solution was to wrap the mocked system calls with extern "C".

Here's a discussion on this keyword:

How to correctly use the extern keyword in C