ç未能编译:找不到math.h中的功能 [英] C Failing to compile: Can't find math.h functions

查看:196
本文介绍了ç未能编译:找不到math.h中的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个素数发现者。在数学上,会更快,而不是做为(无符号​​长I = 2; I<数/ 2;我++)这是快了很多,仍然同样有效,做为(无符号​​长我= 2; I<开方(号码);我++)

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++)

但它不工作。以下是我的code。

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;
}

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

And then below is the error I get from GCC.

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

更改号码的类型的双重原因的问题%操作。它转换为双的的sqrt()调用不会改变任何东西。

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.

任何意见?

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

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

    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.

推荐答案

-lm code>的main.c 所需的数学库,那么你可以使用:

-lm needs to added to the command line after the file that requires that library for example if main.c required the math library then you would use:

 gcc -x c main.c -lm 

您可以看到一个活生生的例子这里,也有可用的三个命令行例。一个没有 -lm ,一个具有 -lm 之前需要它的一个需要它的文件后,该文件。

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

为了完整起见,如果我们参考 GCC文档的链接它说以下 -l <​​/ code>:

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

[...]这使得其中的命令,你写这个选项是有所不同;链接器搜索和处理库和目标文件中它们被指定的顺序。因此,foo.o的-lz文件bar.o'文件foo.o的之后,但之前bar.o.库中搜索'Z'如果bar.o指'Z'功能,这些功能可能不会被加载。 [...]

[...]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. [...]

这篇关于ç未能编译:找不到math.h中的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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