Question: I am trying to use `LD_PRELOAD` on linux to wrap calls to `system` function to add some preprocessing to the argument. Here's my `system.cpp`:

```
#define _GNU_SOURCE
#include <dlfcn.h>
#include <string>
#include <iostream>

typedef int (*orig_system_type)(const char *command);

int system(const char *command)
{
    std::string new_cmd = std::string("set -f;") + command;
    // next line is for debuggin only
    std::cout << new_cmd << std::endl;

    orig_system_type orig_system;
    orig_system = (orig_system_type)dlsym(RTLD_NEXT,"system");
    return orig_system(new_cmd.c_str());
}

```

I build it with

```
g++ -shared -fPIC -ldl -o libsystem.so system.cpp

```

which produces the .so object. I then run my program with

```
$ LD_PRELOAD=/path/to/libsystem.so ./myprogram

```

I do not get any errors - but seemingly my `system` function is not called. Running with `LD_DEBUG=libs`, I can see that my .so is being loaded, however my `system` function is not being called and the one from the standard library is called instead.

What do I need to change in code/build to get it to work?

Answer: 
You need

```
extern "C" int system ...

```

because it is called by a C function. The C++ version has its name mangled so it is not recognizable.
The code should work perfectly fine. Assuming the driver program is something like this:

```
#include <cstdlib>

int main() {
    system("find -name *.cpp");
}

```

Then `env LD_PRELOAD=$PWD/libsystem.so ./a.out` gives me this output:

```
set -f;find -name *.cpp
./system.cpp
./test.cpp

```

Which shows that not only is your debug statement appearing, but that glob is disabled for that command.