未定义对sqrt的引用(或其他数学函数) [英] undefined reference to sqrt (or other mathematical functions)

查看:308
本文介绍了未定义对sqrt的引用(或其他数学函数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的代码:

max = (int) sqrt (number);

并且在标题中我有:

#include <math.h>

但是应用程序仍然说未定义的对sqrt的引用.您在这里看到任何问题吗?看来一切都应该没事.

But application still says undefined reference to sqrt. Do you see any problem here? It looks like everything should be okay.

推荐答案

您可能会发现必须在所使用的任何系统上链接数学库,例如:

You may find that you have to link with the math libraries on whatever system you're using, something like:

gcc -o myprog myprog.c -L/path/to/libs -lm
                                       ^^^ - this bit here.

包含标头可让编译器知道函数声明,但不一定不一定会自动链接到执行该函数所需的代码.

Including headers lets a compiler know about function declarations but it does not necessarily automatically link to the code required to perform that function.

否则,您需要向我们展示您的代码,编译命令和所运行的平台(操作系统,编译器等).

Failing that, you'll need to show us your code, your compile command and the platform you're running on (operating system, compiler, etc).

以下代码可以编译并很好地链接:

The following code compiles and links fine:

#include <math.h>
int main (void) {
    int max = sqrt (9);
    return 0;
}


请注意,某些编译系统取决于在命令行上给出库的顺序.通过这种方式,我的意思是它们可以按顺序处理这些库,并且只能使用它们来满足序列中该点上未解析的符号.


Just be aware that some compilation systems depend on the order in which libraries are given on the command line. By that, I mean they may process the libraries in sequence and only use them to satisfy unresolved symbols at that point in the sequence.

例如,给定命令:

gcc -o plugh plugh.o -lxyzzy
gcc -o plugh -lxyzzy plugh.o

plugh.o需要xyzzy库中的某些内容,第二个库可能无法按您预期的那样工作.在列出库的那一刻,没有任何未解决的符号可以满足.

and plugh.o requires something from the xyzzy library, the second may not work as you expect. At the point where you list the library, there are no unresolved symbols to satisfy.

出现plugh.o do 中未解析的符号时,为时已晚.

And when the unresolved symbols from plugh.o do appear, it's too late.

这篇关于未定义对sqrt的引用(或其他数学函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆