Ç - 未定义参考开方(或其他数学函数) [英] C - undefined reference to sqrt (or other mathematical functions)

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

问题描述

我有这个简单的code:

I have this simple code:

max = (int) sqrt (number);

和在头我有:

#include <math.h>

但应用程序仍然说未定义的引用开方。你在这里看不出什么问题?看起来一切都应该没问题。

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.

包括标题让编译器知道函数的声明,但它确实的的必然自动链接到执行该功能所需的code。

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

如果做不到这一点,你需要向我们展示你的code,你的编译命令和你所运行的平台(操作系统,编译器等)。

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

以下code编译和链接罚款:

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.

因此​​,例如,给定的命令:

So, for example, given the commands:

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 未解决的符号的的出现,一切都太迟了。

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

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

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