views:

520

answers:

2

Hello,

I have a bit of code which used pthread_cond_wait which looks like this:

struct timespec ts;
clock_getttime(CLOCK_REALTIME, &timS);
ts.tv_sec += delay;

pthread_mutex_lock(&a_mutex);
     pthread_cond_timedwait(&thread_cond, &a_mutex,&timS);
pthread_mutex_unlock(&a_mutex);

But I get a linker error on compilation,

undefined symbol clock_gettime ... first referenced in (the file with that code)

This is the only linker error I get; if I comment out this block of code it compiles, so the pthread library is loading. I read somewhere that I need the -lc flag set, which I have done but it appears that I need to set something else too.

Does anybody know what?

This is on Solaris 10, using Sun's 5.8 compiler.

+2  A: 

The -lc answer is wrong. You need to add -lrt (presumably real time..?)

Chris Huang-Leaver
+1  A: 

On the command line try "man clock_getttime" or "man -k clock_getttime". This will give you the library to link to. Then, include this line in your g++ -L/path/to/lib -lNameOfLib (or in the makefile as link flags)

Solaris Unix APIs are sometimes different than standard Unix functions.

Fox
Thanks, but I figured it out myself, by typing "clock_gettime solaris" into Google. I missed it the first time because I was too specific. The '-lrt' bit is mentioned in the Sun's man page under Synopsis. It doesn't say 'requires rt library' etc. But now I know :-)
Chris Huang-Leaver
Actually, POSIX 1003.1b mandates that `clock_gettime` is provided `-lrt`, so this should be the same regardless of which UNIX you're on, unless it's non-compliant.
ephemient