C语言 C 编程 - “文件中引用的未定义符号”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13263716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:20:20  来源:igfitidea点击:

C programming - "Undefined symbol referenced in file"

clinker-errorsundefined-reference

提问by Michael Foukarakis

I am trying to write a program to approximate pi. It basically takes random points between 0.00 and 1.00 and compares them to the bound of a circle, and the ratio of points inside the circle to total points should approach pi (A very quick explanation, the specification goes in depth much more).

我正在尝试编写一个程序来近似 pi。它基本上取 0.00 到 1.00 之间的随机点并将它们与圆的边界进行比较,并且圆内的点与总点的比率应该接近 pi(一个非常快速的解释,规范更深入)。

However, I am getting the following error when compiling with gcc:

但是,使用 gcc 编译时出现以下错误:

Undefined                       first referenced
symbol                          in file
pow                             /var/tmp//cc6gSbfE.o
ld: fatal: symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

What is happening with this? I've never seen this error before, and I don't know why it's coming up. Here is my code (though I haven't fully tested it since I can't get past the error):

这是怎么回事?我以前从未见过这个错误,我不知道为什么会出现这个错误。这是我的代码(虽然我没有完全测试它,因为我无法克服错误):

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void) {
    float x, y;
    float coordSquared;
    float coordRoot;
    float ratio;
    int n;
    int count;
    int i;

    printf("Enter number of points: ");
    scanf("%d", &n);

    srand(time(0));

    for (i = 0; i < n; i++) {
        x = rand();
        y = rand();

        coordSquared = pow(x, 2) + pow(y, 2);
        coordRoot = pow(coordSquared, 0.5);

        if ((x < coordRoot) && (y < coordRoot)) {
            count++;
        }
    }

    ratio = count / n;
    ratio = ratio * 4;

    printf("Pi is approximately %f", ratio);

    return 0;
}

回答by Aniket Inge

use -lmduring compilation(or linking) to include math library.

-lm在编译(或链接)期间使用以包含数学库。

Like this: gcc yourFile.c -o yourfile -lm

像这样: gcc yourFile.c -o yourfile -lm

回答by justinyu

need to Link with -lm. gcc test.c -o test -lm

需要与 -lm 链接。gcc test.c -o test -lm

回答by Michael Foukarakis

The error is produced by the linker, ld. It is telling you that the symbol powcannot be found (is undefined in all the object files handled by the linker). The solution is to include the library which includes the implementation of the pow()function, libm (m for math). [1] Add the -lmswitch to your compiler command line invocation (after all the source file specifications) to do so, e.g.

错误是由链接器产生的,ld。它告诉您pow找不到符号(在链接器处理的所有目标文件中都未定义)。解决方案是包含包含pow()函数实现的库,libm(m 表示数学)。[1] 将-lm开关添加到您的编译器命令行调用(在所有源文件规范之后)以这样做,例如

gcc -o a.out source.c -lm

[1] Alternatively, you could have your own implementation of pow()in a separate translation unit or a library, but you would still have to tell the compiler/linker where to find it.

[1] 或者,您可以pow()在单独的翻译单元或库中拥有自己的实现,但您仍然必须告诉编译器/链接器在哪里找到它。