WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: undefined reference to in time.h functions  (Read 4948 times)

Offline blackdiamond

  • Newbie
  • *
  • Posts: 9
undefined reference to in time.h functions
« on: October 02, 2015, 01:14:16 PM »
Hi !!

I have tried to compile the next example under tiny core , 4.6.2 version under INTEL Pentium Desk top board.


My compilation has "undefined reference to XXXX" errors for clock_settime, clock_getres and clock_gettime functions...

The function nanosleep works fine with no errors on compilation when it is the only instruction enabled...

I wonder what is going wrong as the time.h extern references are under the same nanosleep section of the header.


Any hint ????

Thanks

Sal...

#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/io.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>
#include "rs232.h"
#include "timer.h"
#include "time.h"

#define BILLION 1000000000L

uint64_t diff;


int main(void)
/****************************/
{

   // USING TINY CORE VERSION 4.6.2 GCC COMPILER , STRAIGHT

    struct timespec tim,tim2;

    tim.tv_sec=1;
    tim.tv_nsec=500;
    clock_settime(CLOCK_MONOTONIC,&tim);     <--- this function call has "undefined reference to clock_settime
    clock_getres(CLOCK_MONOTONIC,&tim);   <--- this function call has "undefined reference to clock_getres
    nanosleep(&tim,&tim2);   <<<--- this function has no problems...
    clock_gettime(CLOCK_MONOTONIC,&tim);   <--- this function call has "undefined reference to clock_gettime
    sleep(1);
    clock_gettime(CLOCK_MONOTONIC,&tim2);   <--- this function call has "undefined reference to clock_gettime
    diff=BILLION*(tim2.tv_sec-tim.tv_sec)+(tim2.tv_nsec-tim.tv_nsec);
    printf(" tiempo transcurrido = %11u nanosegundos\n",(long long unsigned int diff));

    if ( nanosleep(&tim,&tim2) < 0)
    {
        printf("nano sleep fallo\n");
    }
    printf("exito !!!\n");

}


the time.h has the next

#iddef __USE_POSIX199309

extern int nanosleep(__const struct timespec *__requested_time, struct timespec *__remaining);

extern int clock_getres( clockid_t__clock_id, struct timespec *__res)__THROW;

extern int clock_getime( clockid_t__clock_id, struct timespec *__tp)__THROW;

extern int clock_settime( clockid_t__clock_id, struct timespec *__tp)__THROW;

#ifdef __USE_XOPEN2K
.....
#endif

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11694
Re: undefined reference to in time.h functions
« Reply #1 on: October 02, 2015, 02:34:45 PM »
Hi blackdiamond
What happens if you do this:
Code: [Select]
#define __USE_POSIX199309

#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/io.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>
#include "rs232.h"
#include "timer.h"
#include "time.h"

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11694
Re: undefined reference to in time.h functions
« Reply #2 on: October 02, 2015, 03:12:17 PM »
Hi blackdiamond
I think my previous answer may be the wrong way to do this. Try:
Code: [Select]
#define _POSIX_C_SOURCE 199309L

#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/io.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>
#include "rs232.h"
#include "timer.h"
#include "time.h"

You might also need to add  -lrt  to the end of your  gcc  command.

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 11049
Re: undefined reference to in time.h functions
« Reply #3 on: October 02, 2015, 03:24:45 PM »
As Rich says, the manpage for those functions tells you you need to link with -lrt.
The only barriers that can stop you are the ones you create yourself.

Offline blackdiamond

  • Newbie
  • *
  • Posts: 9
Re: undefined reference to in time.h functions
« Reply #4 on: October 03, 2015, 12:58:49 PM »
Thanks !!!

I will test it next monday, we use tiny core for magnetic ticket encoding using a shaft encoder, the resolution of the encoder is well to record the ticket, but for reading we want to use time comparision with nanoseconds measuring.

Tiny core is the platform we use on Automatic Fare Collection Equipment we renew in my job ....

LARGA VIDA A TINY CORE !!!!!