Search code examples
c++gcccompilationg++ld-preload

How to compile LD_PRELOAD for most systems


I have a LD_PRELOAD file. On what OS and conditions should I compile this preload to work on most systems (Unix/Linux)? The most wanted are FreeBSD, Ubuntu, CenstOS, Solaris.


Solution

  • You need to compile it into a shared library. Here's how I typically compile mine:

    libt.so: t.lo
            g++ -fPIC -O3 -W -Wall -shared -Wl,-export-dynamic -o $@ $^ -lc
    
    t.lo: t.cc
            g++ -c -fPIC -O3 -W -Wall $^ -o $@
    

    Then to use it, you add the library to the LD_PRELOAD environment variable before launching that file. If you do it this way from the bash command line:

    LD_PRELOAD=libt.so executable_name -and args
    

    then, it will only set it for that command line run, and not affect any other programs you launch afterwards.