Linking with gcc and -lm doesn't define ceil() on Ubuntu

烂漫一生 提交于 2019-12-17 15:58:09

问题


I am currently using gcc to compile and I need to use <math.h>. 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 in a different computer and it work perfectly.

Does anyone know how to solve this problem?


I did include <math.h>. Also, the command I used was:

gcc -lm -o fb file.c

回答1:


Take this code and put it in a file ceil.c:

#include <math.h>
#include <stdio.h>
int main(void)
{
    printf("%f\n", ceil(1.2));
    return 0;
}

Compile it with:

$ gcc -o ceil ceil.c
$ gcc -o ceil ceil.c -lm

One of those two should work. If neither works, show the complete error message for each compilation. Note that -lm appears after the name of the source file (or the object file if you compile the source to object before linking).

Notes:

  1. A modern compiler might well optimize the code to pass 2.0 directly to printf() without calling ceil() at all at runtime, so there'd be no need for the maths library at all.

  2. Rule of Thumb: list object files and source files on the command line before the libraries. This answer shows that in use: the -lm comes after the source file ceil.c. If you're building with make etc, then you typically use ceil.o on 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.




回答2:


Don't have enough reputation to comment on @Jonathan Leffler's answer. Just wanted to mention that Peter van der Linden's book Expert C Programming has a good treatment on this subject in chapter 5 Thinking of Linking.

Archives (static libraries) are acted upon differently than are shared objects (dynamic libraries). With dynamic libraries, all the library symbols go into the virtual address space of the output file, and all the symbols are available to all the other files in the link. In contrast, static linking only looks through the archive for the undefined symbols presently known to the loader at the time the archive is processed.

If you specify the math library (which is usually a static one) before your object files, then the linker won't add any symbols.




回答3:


Try compiling like that:

gcc -Wall -g file.c -lm -o file

I had the same problem and it was solved using this command. Also if you installed your Ubuntu the same day you had the problem it might be an update problem.



来源:https://stackoverflow.com/questions/8266183/linking-with-gcc-and-lm-doesnt-define-ceil-on-ubuntu

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!