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:
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:
By compiling the program with this command:
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.