LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-14-2010, 12:11 PM   #1
hiteurecomupf
LQ Newbie
 
Registered: Oct 2010
Location: Barcelona
Distribution: Linux Ubuntu 9.10
Posts: 13

Rep: Reputation: 0
Issue with undefined reference to `clock_gettime'


Hi ,

I am slowly getting accustomed with linux issues and its different programming compilation stuffs. and i must say am truly liking it.

Now i am facing this typical error , "undefined reference to `clock_gettime'" while i am compiling a code, which is not mine as it required to install a simulator. Now i have read about this type for the last 2 days and i have included "-lrt" in the make file and included <time.h> in the header files which was missing but still i am getting that error.

Now matter how many times i run the make file with the required changes its still giving me this error, which i have comprehended as mainly linking problem rather than a compilation issue.

Can anyone help me where it's still getting wrong.

Cheers ,

Sougata
 
Old 10-14-2010, 12:22 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by hiteurecomupf View Post
Hi ,

I am slowly getting accustomed with linux issues and its different programming compilation stuffs. and i must say am truly liking it.

Now i am facing this typical error , "undefined reference to `clock_gettime'" while i am compiling a code, which is not mine as it required to install a simulator. Now i have read about this type for the last 2 days and i have included "-lrt" in the make file and included <time.h> in the header files which was missing but still i am getting that error.

Now matter how many times i run the make file with the required changes its still giving me this error, which i have comprehended as mainly linking problem rather than a compilation issue.

Can anyone help me where it's still getting wrong.

Cheers ,

Sougata
Did you read 'man 3 clock_gettime' ? If yes, how many lines ?
 
Old 10-15-2010, 11:48 AM   #3
hiteurecomupf
LQ Newbie
 
Registered: Oct 2010
Location: Barcelona
Distribution: Linux Ubuntu 9.10
Posts: 13

Original Poster
Rep: Reputation: 0
Dear Sergei,

Thanks a lot for pointing me the manual page for clock_gettime function. Its really helped me to understand what exactly this function means.I have tried to do some changes to the code but I am still having issues with this clock_gettime function, now i am having errors "error: expected ‘;’, ‘,’ or ‘)’ before numeric constant". This error is coming at the line "int clock_gettime(clockid_t CLOCK_REALTIME, struct timespec *tp);"

The code is attached below. When am running the makefile , i am having the above mentioned error.

#include "../ptpd.h"
#include <time.h>

int clock_gettime(clockid_t CLOCK_REALTIME, struct timespec *tp);


int
isTimeInternalNegative(const TimeInternal * p)
{
return (p->seconds < 0) || (p->nanoseconds < 0);
}


int
snprint_TimeInternal(char *s, int max_len, const TimeInternal * p)
{
int len = 0;

if (isTimeInternalNegative(p))
len += snprintf(&s[len], max_len - len, "-");

len += snprintf(&s[len], max_len - len, "%d.%09d",
abs(p->seconds), abs(p->nanoseconds));

return len;
}



int
snprint_ClockIdentity(char *s, int max_len, const Octet uuid[PTP_UUID_LENGTH], const char *info)
{
int len = 0;
int i;

if (info)
len += snprintf(&s[len], max_len - len, "%s", info);

for (i = 0; {
len += snprintf(&s[len], max_len - len, "%02x", (unsigned char) uuid[i]);

if (++i >= PTP_UUID_LENGTH)
break;

// uncomment the line below to print a separator after each byte except the last one
// len += snprintf(&s[len], max_len - len, "%s", "-");
}

return len;
}


int
snprint_PortIdentity(char *s, int max_len, const Octet uuid[PTP_UUID_LENGTH],
UInteger16 portId, const char *info)
{
int len = 0;

if (info)
len += snprintf(&s[len], max_len - len, "%s", info);

len += snprint_ClockIdentity(&s[len], max_len - len, uuid, NULL);
len += snprintf(&s[len], max_len - len, ":%02x", portId);
return len;
}


void
message(int priority, const char *format, ...)
{
extern RunTimeOpts rtOpts;
va_list ap;
va_start(ap, format);
if(rtOpts.useSysLog) {
static Boolean logOpened;
if(!logOpened) {
openlog("ptpd", 0, LOG_USER);
logOpened = TRUE;
}
vsyslog(priority, format, ap);
} else {
fprintf(stderr, "(ptpd %s) ",
priority == LOG_EMERG ? "emergency" :
priority == LOG_ALERT ? "alert" :
priority == LOG_CRIT ? "critical" :
priority == LOG_ERR ? "error" :
priority == LOG_WARNING ? "warning" :
priority == LOG_NOTICE ? "notice" :
priority == LOG_INFO ? "info" :
priority == LOG_DEBUG ? "debug" :
"???");
vfprintf(stderr, format, ap);
}
va_end(ap);
}

char *
translatePortState(PtpClock *ptpClock)
{
char *s;
switch(ptpClock->port_state) {
case PTP_INITIALIZING: s = "init"; break;
case PTP_FAULTY: s = "flt"; break;
case PTP_LISTENING: s = "lstn"; break;
case PTP_PASSIVE: s = "pass"; break;
case PTP_UNCALIBRATED: s = "uncl"; break;
case PTP_SLAVE: s = "slv"; break;
case PTP_PRE_MASTER: s = "pmst"; break;
case PTP_MASTER: s = "mst"; break;
case PTP_DISABLED: s = "dsbl"; break;
default: s = "?"; break;
}
return s;
}

void
displayStats(RunTimeOpts * rtOpts, PtpClock * ptpClock)
{
static int start = 1;
static char sbuf[SCREEN_BUFSZ];
int len = 0;
struct timeval now;
char time_str[MAXTIMESTR];

if (start && rtOpts->csvStats) {
start = 0;
printf("timestamp, state, one way delay, offset from master, "
"slave to master, master to slave, drift, variance");
fflush(stdout);
}
memset(sbuf, ' ', sizeof(sbuf));

gettimeofday(&now, 0);
strftime(time_str, MAXTIMESTR, "%Y-%m-%d %X", localtime(&now.tv_sec));

len += snprintf(sbuf + len, sizeof(sbuf) - len, "%s%s:%06d, %s",
rtOpts->csvStats ? "\n" : "\rstate: ",
time_str, (int)now.tv_usec,
translatePortState(ptpClock));

if (ptpClock->port_state == PTP_SLAVE) {
len += snprint_PortIdentity(sbuf + len, sizeof(sbuf) - len,
ptpClock->parent_uuid, ptpClock->parent_port_id, " ");

len += snprintf(sbuf + len, sizeof(sbuf) - len,
", %s%d.%09d" ", %s%d.%09d",
rtOpts->csvStats ? "" : "stm: ",
ptpClock->slave_to_master_delay.seconds,
abs(ptpClock->slave_to_master_delay.nanoseconds),
rtOpts->csvStats ? "" : "mts: ",
ptpClock->master_to_slave_delay.seconds,
abs(ptpClock->master_to_slave_delay.nanoseconds));

/*
* if grandmaster ID differs from parent port ID then also
* print GM ID
*/
if (memcmp(ptpClock->grandmaster_uuid_field,
ptpClock->parent_uuid, PTP_UUID_LENGTH)) {
len += snprint_ClockIdentity(sbuf + len,
sizeof(sbuf) - len,
ptpClock->grandmaster_uuid_field,
" GM:");
}

len += snprintf(sbuf + len, sizeof(sbuf) - len, ", ");

if (!rtOpts->csvStats)
len += snprintf(sbuf + len,
sizeof(sbuf) - len, "owd: ");

len += snprint_TimeInternal(sbuf + len, sizeof(sbuf) - len,
&ptpClock->one_way_delay);

len += snprintf(sbuf + len, sizeof(sbuf) - len, ", ");

if (!rtOpts->csvStats)
len += snprintf(sbuf + len, sizeof(sbuf) - len,
"ofm: ");

len += snprint_TimeInternal(sbuf + len, sizeof(sbuf) - len,
&ptpClock->offset_from_master);

len += sprintf(sbuf + len, ", %s%d",
rtOpts->csvStats ? "" : "drift: ",
ptpClock->observed_drift);
}
else {
if (ptpClock->port_state == PTP_MASTER) {
len += snprint_ClockIdentity(sbuf + len, sizeof(sbuf) - len,
ptpClock->clock_uuid_field, " (ID:");
len += snprintf(sbuf + len, sizeof(sbuf) - len, ")");
}
}
write(1, sbuf, rtOpts->csvStats ? len : SCREEN_MAXSZ + 1);
}

Boolean
nanoSleep(TimeInternal * t)
{
struct timespec ts, tr;

ts.tv_sec = t->seconds;
ts.tv_nsec = t->nanoseconds;

if (nanosleep(&ts, &tr) < 0) {
t->seconds = tr.tv_sec;
t->nanoseconds = tr.tv_nsec;
return FALSE;
}
return TRUE;
}

void
getTime(TimeInternal * time)
{
struct timespec tp;
if (clock_gettime(CLOCK_REALTIME, &tp) < 0) {
PERROR("clock_gettime() failed, exiting.");
exit(0);
}
time->seconds = tp.tv_sec;
time->nanoseconds = tp.tv_nsec;

}

void
setTime(TimeInternal * time)
{
struct timeval tv;

tv.tv_sec = time->seconds;
tv.tv_usec = time->nanoseconds / 1000;
settimeofday(&tv, 0);

NOTIFY("resetting system clock to %ds %dns\n",
time->seconds, time->nanoseconds);
}

UInteger16
getRand(UInteger32 * seed)
{
return rand_r((unsigned int *)seed);
}

Boolean
adjFreq(Integer32 adj)
{
struct timex t;

if (adj > ADJ_FREQ_MAX)
adj = ADJ_FREQ_MAX;
else if (adj < -ADJ_FREQ_MAX)
adj = -ADJ_FREQ_MAX;

t.modes = MOD_FREQUENCY;
t.freq = adj * ((1 << 16) / 1000);

return !adjtimex(&t);
}

Where am i missing again and again to fix this bug?

Regards ,

Sougata
 
Old 10-15-2010, 12:05 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Please place [code][/code] tags around your code as that is almost impossible to read and follow without formatting.
 
Old 10-15-2010, 12:12 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I do have two questions:

1. Did you read the following lines in the man page and do you have the correct priveleges?
Code:
CLOCK_REALTIME
              System-wide real-time clock.  Setting this clock requires appropriate privileges.
2. Why is the following line at the start of your code?
Code:
int clock_gettime(clockid_t CLOCK_REALTIME, struct timespec *tp);
I ask as this looks like you are trying to retype / overload this particular function yet it is already defined in time.h
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ion SlackBuild fails with undefined reference to `clock_gettime'" xflow7 Slackware 1 08-09-2009 07:52 AM
C++ Issue: "undefined reference" Esoteric Programming 11 05-30-2008 05:12 PM
linker issue - undefined reference error asif.kalim Programming 3 04-19-2008 05:18 AM
undefined reference to... dimah Programming 3 12-27-2006 09:57 AM
clock_gettime() issue AndreiCiprian Programming 0 03-28-2006 06:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration