This is going to be a super short and super simple tutorial for beginners about LD_PRELOAD. If you're familiar with LD_PRELOAD, you'll learn nothing new. Otherwise keep reading!

Did you know you could override the C standard library's functions (such as printf, fopen, etc) with your own version of these functions in any program? In this article I'll teach you how this can be done through the LD_PRELOAD environment variable.

Let's start with a simple C program (prog.c):

#include <stdio.h>

int main(void) {
    printf("Calling the fopen() function...\n");

    FILE *fd = fopen("test.txt","r");
    if (!fd) {
        printf("fopen() returned NULL\n");
        return 1;

    printf("fopen() succeeded\n");

    return 0;

The code above simply makes a call to the standard fopen function and then checks its return value. Now, let's compile and execute it:

$ ls
prog.c  test.txt

$ gcc prog.c -o prog

$ ls
prog  prog.c  test.txt

$ ./prog
Calling the fopen() function...
fopen() succeeded

Now let's write our own version of fopen and compile it as a shared library:

#include <stdio.h>

FILE *fopen(const char *path, const char *mode) {
    printf("Always failing fopen\n");
    return NULL;

Let's call this file myfopen.c, and let's compile it as a shared library:

gcc -Wall -fPIC -shared -o myfopen.c

Now we can simply modify LD_PRELOAD:

$ LD_PRELOAD=./ ./prog
Calling the fopen() function...
Always failing fopen
fopen() returned NULL

As you can see the fopen got replaced with our own version that is always failing. This is really handy if you've to debug or replace certain parts of libc or any other shared library.

Next time I'll write about how the LD_PRELOAD works internally.


Bradley Meck Permalink
April 02, 2013, 17:22

Example with all the pragmas for Mac OS X, Solaris, and Linux C code we are using:

Example simple build script for the file:

April 02, 2013, 18:10

That is awesome!

December 15, 2014, 03:22

Your music is amazing. You have some very talented artists. I wish you the best of success.Thank you for presenting your points and providing this information. I have learned something about this topic.

July 26, 2013, 12:51

I still don't understand :(

July 26, 2013, 12:52

I still don't understand :(

July 28, 2013, 13:55

Okey, Well tutorial, assuming like the much comfortable for c programmer.

Nice Post !

July 29, 2013, 04:18

I am using C, but I really didn't know about that. Thanks, Nice Share.

are you programer? im too..... i very happy can found you

Grzeg Permalink
July 29, 2013, 20:12

now continuing with our example, is it possible to still call the standard libc function fopen from your own version?

August 01, 2013, 15:53

A Simple LD_PRELOAD Tutorial

August 07, 2013, 09:48

I really appreciate your professional approach. These are pieces of very useful information that will be of great use for me in future.

Albob Permalink
September 05, 2013, 13:45

Very cool :)
Any idea on how to make it work with Objective c++? My program is writing file on the disk and I can't find what line of code is doing it... Overriding fopen() would really solve my problem.

October 25, 2013, 05:28

Very well explained tutorial.

November 01, 2013, 17:55

Thanks so much for making all this easier with your tutorials. i like most.

November 01, 2013, 17:56

Thanks for sharing your imperative article. I think a lot of male detectives are also represented as being damaged in some way

November 11, 2013, 15:52

Thank you for the sharing the post.really nice tutorial.

November 28, 2013, 09:22

Here below some pictures from the event:

February 11, 2014, 13:48

I stopped my negligence and started reading your blog. It is so great inspiration to read your posting. I want to more tell you about fiverr. Lists offer like pancakes online. Especially if you know how to make useful details for online promotion, you will be able to generate a lot of product sales. Earn some analysis and discover out what's being marketed around and make useful details focusing on those customers in the IM. Earn some details and provide them as Jobs on gigbucks. If you get excellent visibility, you will be able to make reasonable residual earnings.

March 13, 2014, 07:52

It's a help for all I think. One can easily learn from here. I appreciate it.

sharath Permalink
March 26, 2014, 18:08

I have overridden open() which is provided by glibc in my library & I have set LD_PRELOAD with my library first, so when the process calls open(), the open which is defined in my library gets called.

THE PROBLEM:- There are several other functions within glibc which calls open() once such example is getpt(), when getpt() calls open(), the open() which is defined in glibc gets called, how would I make getpt() to invoke the open() which is defined in my library().

April 05, 2014, 09:43

Yes I thanks for help

April 06, 2014, 08:16

Yes thanks a lot for that help

April 09, 2014, 08:26


May 20, 2014, 09:49

Thanks for the nice post. I like your post very much.

It is very informative post so i like this post very much.

July 03, 2014, 04:38

Ok that will be truly awesome work

September 06, 2014, 08:29

Your blog is quite interesting i found here very interesting c/c++ Compiles for beginner programmers.this is very helpful for me. thank you.

October 16, 2014, 15:18

Thanks for the information.

markys Permalink
November 11, 2014, 05:58

I followed your example but something going wrong. Here is the output. Please explain me this.
"ERROR: object '' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored."

markys Permalink
November 11, 2014, 06:16

found my mistake. thank you for clear explain.

November 19, 2014, 09:45

amazing pub and look amazing...

November 19, 2014, 09:54

greats sir, keep spirit to be continieu publish

November 26, 2014, 16:56

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.

Kaur deep Permalink
December 04, 2014, 03:31

Excellent and knowledgeable links and post.thanks for sharing.
Now, The Time has come for the Happy New Year 2015. It wil bring a lot fo happinesin our life.
Happy New Year 2015
And I am sure, You wil anjoy this new year day with yoru fiends and wil send some images wishes,
New Year 2015

Happy New Year 2015 Wishes
Greetings to yoru close ones, Your friends, You lover, etc..
Happy New Year 2015 Images
Even I am waititng for that day, because it just changes our life.
Happy New Year 2015 Greetings

Happy New Year 2015 SMS
So, That's why I am commenting here to wish you a very happy new year.
Happy New Year 2015 Messages
I hope this New year 2015 wil bring success to your website. But before that Merry Christmas is coming.
Merry Christmas Quotes
It is also a big event for everyone.
Merry Christmas Pictures
Everyone knows that Christmas is a day when People seems to be very happy, they just leave all sadness behind.
Merry Christmas Wallpapers
And I am sure, you also enjoy this Merry Christmas day of 2014.
Christmas day Images

Christmas stocking
But if you are not looking out to enjoy this christmas, then you are missing out on a big thing, because it really brings happiness.
Merry Christmas Images

Merry Christmas 2014
So, what you are waiting for?
Merry Christmas 2015
Enjoy This Merry Christmas day. Cheers :).

December 09, 2014, 09:39


a href="">Lingaa Movie Torrent Downl


lingaa movie in mp4lingaa movie in hdlingaa movie in free downloadLingaa Movie download in torrentfree download lingaa movie
nice article

Leave a new comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type the word "linux_381": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.