I am currently using GCC to compile and I need to use <math.h>.
The problem is that it won’t recognize the library.
I have also tried -lm and nothing.
The function I tried to use was ceil() and I get the following error:
: undefined reference to `ceil'
collect2: ld returned 1 exit status
I am using the latest Ubuntu and math.h is there.
I tried to use -lm on a different computer, and it worked perfectly.
How can I solve this problem?
I did include <math.h>. Also, the command I used was:
gcc -lm -o fb file.c
Take this code and put it in a file
ceil.c:Compile it with:
One of those two should work. If neither works, show the complete error message for each compilation. Note that
-lmappears after the name of the source file (or the object file if you compile the source to object before linking).Notes:
A modern compiler might well optimize the code to pass 2.0 directly to
printf()without callingceil()at all at runtime, so there’d be no need for the maths library at all.Rule of Thumb: list object files and source files on the command line before the libraries. This answer shows that in use: the
-lmcomes after the source fileceil.c. If you’re building withmakeetc, then you typically useceil.oon the command line (along with other object files); normally, you should list all the object files before any of the libraries.There are occasionally exceptions to the rule of thumb, but they are rare and would be documented for the particular cases where the exception is expected/required. In the absence of explicit documentation to the contrary, apply the rule of thumb.