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 09-14-2011, 09:24 AM   #1
javierhernandez
LQ Newbie
 
Registered: Sep 2011
Posts: 9

Rep: Reputation: Disabled
Getting error with time.h


Hello,

I have a requirement to change the time on the embedded system in my program I am using:

#include <sys/time.h>

clock_settime(CLOCK_REALTIME, time->currTime);

I get error undefined reference to 'clock_settime'

So I guess 'clock_settime' is not define in the time.h I am using?
 
Old 09-14-2011, 11:16 AM   #2
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
I'm not entirely sure that I know in what context you are working. Some additional
details from you could be helpful, such a which distro. you are using, etc.

But if your program is otherwise correct, and you are using gcc to process your
program, you should be able to add an option such as:

-lrt

to gcc to be passed to ld, to tell ld in which library to find the function
clock_settime(). The proper option can be found in the man page for the function
clock_settime() on some distros.
 
1 members found this post helpful.
Old 09-14-2011, 11:31 AM   #3
javierhernandez
LQ Newbie
 
Registered: Sep 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
I am using Linux 2.6.X Kernel
 
Old 09-14-2011, 03:11 PM   #4
javierhernandez
LQ Newbie
 
Registered: Sep 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
Looks like stime() works.
 
0 members found this post helpful.
Old 09-14-2011, 05:33 PM   #5
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
Hi javierhernandez,

Even though you got your program to build using stime() you might want to take a look at this to
understand what's happening the next time you run into a similar problem.

I am also using the Linux 2.6.X Kernel.

However, the structure of a given environment can be determined by choices made when the so called "distro."
was created. By "distro.", short for distribution, I mean whether the particular form of Linux you are
using is, for example, Gentoo, Mandrake, RedHat, Slackware, SuSE, Ubuntu, etc.

In my environment, sys/time.h is not used with clock_settime(). Instead plain old time.h is used.

gcc is what is sometimes referred to as a compiler driver, it "drives" the procedure of creating an executable file,
by running a series of programs.

gcc might run a C pre-processor cpp to handle the lines beginning with "#", such as "# include" or "# define".
The C compiler itself can be run on an intermediate file that is produced by the pre-processor.
The output of the C compiler tends to be an assembly language file.
The assembler as is then run on the assembly language file to produce an object file.
The final executable file put together by the linkage editor, A.K.A., the linker ld, can be either complete,
with all external routines, such as clock_settime() or stime() put inside the executable file, or the file
can be produced so that those external functions will be loaded into memory only when the executable file is
run.

If you are using gcc and you wish to see the intermediate files created by the various steps in the procedure
of producing an executable file, in the distro. I'm using, the option --save-temps can be supplied to gcc.

The external functions are kept in so called libraries. Typically when ld tries to find the external functions,
only a small subset of all libraries available are searched by default.

If I create a file named cs.c containing this C code:

Code:
#include <time.h>

int main(  int argc ,  char *argv[]  )
{
        struct timespec         s_ts ;

        clock_settime(  CLOCK_REALTIME ,  &s_ts  ) ;
}
and compile it with this command:

Code:
gcc --save-temps   cs.c
I get these messages:

Quote:
cs.o: In function `main':
cs.c.text+0x1c): undefined reference to `clock_settime'
collect2: ld returned 1 exit status
I also see these additional files created:

Quote:
cs.i cs.o cs.s
The cs.i file is the output of the C preprocessor.
The cs.s file is the assembly language output of the C compiler.
The cs.o file is the object module output of the assembler, as.

The error message "undefined reference to `clock_settime'" does not mean that
clock_settime() is not defined in a header file. It means that the linker ld
cannot find the function clock_settime() in a list of some "standard" libraries
through which it searches for functions not present in your C source code, that are outside/
external to your C code. Some external functions are kept in other libraries that you have to
tell ld about, by giving command line option(s) to gcc which gcc passes
to ld.

In my environment, the external function clock_settime() is in this library:

Quote:
/lib/librt.so.1
By compiling the program with this command:

Code:
gcc  -lrt cs.c
the option -lrt says to look for a version of a library with a name such as librt.

With that option, which is shown in the man page for clock_settime() the program
compiles just fine even if the clock_settime() is used. The function stime() is
in the standard GLibC library, which ld would usually search by default without
any special options.

Hope this helps.
 
2 members found this post helpful.
Old 09-15-2011, 07:13 AM   #6
javierhernandez
LQ Newbie
 
Registered: Sep 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
Wow, that is what I call an explanation. Thank you for taking the time.
 
  


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
[SOLVED] Showing "Invalid Time" error for a particular time in SuSE Linux satya123 Linux - Newbie 3 06-15-2011 05:33 AM
how to understand user time, sys time, wait time, idle time of CPU guixingyi Linux - Server 1 08-24-2010 10:10 AM
rpm compile time error:make error ashmita04 Red Hat 1 07-09-2007 03:44 AM
LogWatch error: aacraid:ID(0:03:0); Mechanical Positioning Error ...: 1 Time(s) tor_user Linux - Hardware 1 07-26-2006 05:00 PM
Time out error elainepearl Linux - Newbie 0 03-06-2002 10:45 PM

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

All times are GMT -5. The time now is 12:21 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