如何编译此代码以在Linux上使用LD_PRELOAD?

时间:2009-09-28 01:16:02

标签: linux gcc preload

如何编译以下代码以使其与LD_PRELOAD一起使用?我设法用'gcc -m32 -shared code.c'编译它没有错误,但是当我打开我试图'注入'代码的程序时,它说:'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;
}

2 个答案:

答案 0 :(得分:4)

您必须将您的共享库与librt链接才能使用clock_gettime。

gcc -m32 -shared code.c -lrt

答案 1 :(得分:-1)

请注意你的fetch_clock功能已损坏。