C语言 C 编译失败:找不到 math.h 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19372317/
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 07:42:32  来源:igfitidea点击:

C Failing to compile: Can't find math.h functions

cgcc

提问by Aviator45003

I'm writing a prime number finder. Mathematically, it is faster to, instead of doing for (unsigned long i = 2; i < number/2; i++)it's a lot faster, and still just as effective, to do for (unsigned long i = 2; i < sqrt(number); i++)

我正在写一个质数查找器。从数学上讲,这样做更快,而不是for (unsigned long i = 2; i < number/2; i++)更快,而且仍然同样有效for (unsigned long i = 2; i < sqrt(number); i++)

But it's not working. The below is my code.

但它不起作用。下面是我的代码。

// Stuff goes up here, including a function prototype and:
#include <math.h>
char isPrime (unsigned long number)
{
    if (number <= 1) {
    return 0;
    }
    long double sqrtOfNumber = sqrt(number); // Calculate once.
    for (unsigned long i = 2; i < sqrtOfNumber; i++) {
        if (number%i == 0) { // It has a divisor.
            return 0;
        }
    }
    // Nothing broke us yet....
    return 1;
}

And then below is the error I get from GCC.

然后下面是我从 GCC 得到的错误。

/tmp/ccFBlUz5.o: In function `isPrime':
main.c:(.text+0xb3): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status

Changing the type of "number" to a double causes problems with the % operator. And casting it to a double for the sqrt() call doesn't change anything.

将“数字”的类型更改为双精度会导致 % 运算符出现问题。并且为 sqrt() 调用将其转换为 double 不会改变任何内容。

Any advice?

有什么建议吗?

Oh, and my math.h IS being imported, if I comment out that line, I get warned that there is an implicit declaration there.

哦,我的 math.h 正在导入,如果我注释掉那行,我会收到警告说那里有一个隐式声明。

    main.c: In function 'isPrime':
    main.c:28:2: warning: implicit declaration of function 'sqrt' [-Wimplicit-function-declaration]
    long double sqrtOfNumber = sqrt(number); // Calculate once.
    ^
    main.c:28:29: warning: incompatible implicit declaration of built-in function 'sqrt' [enabled by default]
    long double sqrtOfNumber = sqrt(number); // Calculate once.
                         ^

plus the other warning under that.

加上其他警告。

回答by Shafik Yaghmour

-lmneeds to added to the command line after the file that requires that library for example if main.crequired the math library then you would use:

-lm需要在需要该库的文件之后添加到命令行,例如,如果main.c需要数学库,那么您将使用:

 gcc -x c main.c -lm 

You can see a live example here, there are three command lines available in the example. One without -lm, one with -lmbefore the file that needs it one after the files that needs it.

您可以在此处查看实时示例,示例中提供了三个命令行。一个没有-lm,一个-lm在需要它的文件之前,一个在需要它的文件之后。

For completeness sake if we refer to the gcc docs on options for Linkingit says the following for -l:

为了完整起见,如果我们参考gcc docs on options for Linking它说以下内容-l

[...]It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o' searches library ‘z' after file foo.o but before bar.o. If bar.o refers to functions in ‘z', those functions may not be loaded. [...]

[...]在命令中编写此选项的位置有所不同;链接器按照指定的顺序搜索和处理库和目标文件。因此,'foo.o -lz bar.o' 在文件 foo.o 之后但在 bar.o 之前搜索库 'z'。如果 bar.o 引用 'z' 中的函数,则可能不会加载这些函数。[...]

回答by Karl Bielefeldt

You need to link the math library. Use the -lmoption on the command line.

您需要链接数学库。使用-lm命令行上的选项。