tags:

views:

238

answers:

2

How do I compile the following code for make it work with LD_PRELOAD?. I managed to compile it w/o errors with 'gcc -m32 -shared code.c' but when I open the program that I'm trying to 'inject' the code into, it says this: 'symbol lookup error: ./fps.so: undefined symbol: clock_gettime'

#include<unistd.h>
#include<time.h>
#define BUSY_WAIT       3000
/* We use clock_gettime because it's better than gettimeofday */

unsigned long long int fetch_clock()
{
        struct timespec ts;
        clock_gettime(CLOCK_REALTIME, &ts);
        return (unsigned long long int)ts.tv_nsec + 1000000*ts.tv_sec;
}

int high_pres_usleep_untill(unsigned long long int end)
{
        unsigned long long int busywait, start;
        int sleep, delay;

        start = fetch_clock();
        delay = end - start;

        sleep = (delay / BUSY_WAIT) - 1;
        if(sleep > 0)
                if(usleep(sleep*BUSY_WAIT))
                        return -1;
        while(fetch_clock() < end)
                ;

        return 0;
}
+4  A: 

You must link your shared library with librt to use clock_gettime.

gcc -m32 -shared code.c -lrt
Pierre Bourdon
works great! thanks :)
A: 

Please note that your fetch_clock function is broken.

FakeName
Are you not going to explain why? You won't gain reputation if you just leave negative remarks like that. (You're correct - but you should say why!)
Jonathan Leffler