Question

I cannot understand why this throws undefined reference to `floor'":

double curr_time = (double)time(NULL);
return floor(curr_time);

Hasn't it been casted to double, which is what floor receives?

Was it helpful?

Solution

You possibly have run in to the infamous -lm problem: Compile as:

gcc yourfile.c -o out -lm 

This is C FAQ 14.3 item as well.

OTHER TIPS

Maybe because you don't link with the math library? The error has nothing to do with casts and data types, btw.

You probably have to link explicitly to the library. On an UNIX-like system this would typically be "/usr/lib/libm.a". The C standard library should be linked by default, but the math library is, depending on your system, not linked and you may have to link explicitly. (e.g. on Mac OS X it is also linked by default on my ubuntu system it is not).

Note that this has nothing to do with your include path. If you are on something UNIX-like you will probably find the header with the prototype declaration under "/usr/include/math.h", where your compiler will always look for headers.

If you are using gcc, you can either link directly with:

gcc yourfile.c /usr/lib/libm.a -o out

or with "-lnameroflibrary" like this:

gcc yourfile.c -lm -o out

this will look for a library in the same directory as the C standard library with the name "libnameoflibrary.a"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top