包括在glibc的数学库现在? [英] Is maths library included in the glibc now?

查看:150
本文介绍了包括在glibc的数学库现在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试编译从终端这个简单的code:

when I try to compile this simple code from terminal:

#include<stdio.h>

int main(void)
{

    printf("%f\n",sqrt(10));

    return 0;
}

使用

GCC的main.c

gcc main.c

命令,它就会被编译和a.out的给出了正确的答案。这意味着,数学函数都在其中被自动链接C标准库补充说。

command, it gets compiled and a.out gives correct answer. It means that that maths functions are added in C standard library which gets linked automatically.

但是,如果编译Ec​​lipse IDE中相同的code,不添加任何库的属性,它给未定义的引用错误。这意味着数学函数不是C标准库的一部分。

But if compile the same code in Eclipse IDE without adding any library to properties, it gives undefined reference error. It means maths functions are not part of C standard library.

真相是什么?

推荐答案

您可能会看到常量折叠在这里,其中数学函数调用中使用常数将导致编译器来计算功能,省略调用数学库都在一起。

You may be seeing constant folding here, where using a constant in the math function call will cause the compiler to calculate the function and omit the call to the math library all together.

如果我们检查出其他内置功能海湾合作委员会说提供(重点煤矿的):

If we check out the docs for Other Built-in Functions Provided by GCC says(emphasis mine):

GCC包括内置的许多标准C库函数的版本。与_ pfixed版本$ P $的内置的总是被视为具有意义,即使你指定-fno-内置选项C库函数相同。 (请参阅C方言的选项)其中的许多功能在某些情况下只进行了优化; 如果他们没有在特定情况下进行了优化,以库函数的调用被发射。

GCC includes built-in versions of many of the functions in the standard C library. The versions prefixed with _builtin are always treated as having the same meaning as the C library function even if you specify the -fno-builtin option. (see C Dialect Options) Many of these functions are only optimized in certain cases; if they are not optimized in a particular case, a call to the library function is emitted.

如果我们来看一下这款稍微修改活生生的例子它采用以下code:

If we look at this slightly modified live example which uses the following code:

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

int main(void)
{

    printf("%d\n",(int)sqrt(25));

    return 0;
}

我们看到 GCC产生下面的汇编

movl    $5, %esi        
movq    %rax, %rdi
movl    $0, %eax
call    printf

让我们看到 5 移动到 ESI 这在的 64 ABI 是第二个参数来调用函数和的结果是的sqrt(25)还有就是开方可言。

so we see 5 is moved into esi which in the x64 abi is the second argument to to the calling function and is the result of sqrt(25) there is no call to sqrt at all.

请注意你缺少:

#include <math.h>

更新

内置插件是一个 GCC扩展的内置上述连结说明哪些是在哪种模式中使用,它们都应该具有的含义与标准功能相同。

Built-ins are a gcc extension the built-in link above explains which ones are used in which mode and they should all have the same meaning as the standard functions.

如果您担心您的code是符合标准的,那么你可以检查出的选项控制ç方言手册的部分。您可以使用 -std 来指定要符合哪些标准和 -pedantic 当您使用启用警告不符合标准或 -pedantic-错误符合一个功能,使警告错误。因此,例如使用

If you are concerned about your code being standards compliant then you can check out the Options Controlling C Dialect section of the manual. You can use -std to specify which standard you want to comply with and -pedantic to enable warnings when you are using a feature that does not conform with the standard or -pedantic-errors to make the warnings an error. So for example using

gcc -std=c99 -pedantic

当你使用一项功能,是符合的的 C99 的标准,例如零长度数组的。

would generate a warning when you used a feature that was compliant with the C99 standard for example zero length arrays.

我们也可以使用 -fno-内置禁用一些内置命令,文件说:

We can also use -fno-builtin to disable some builtins, the documents says:

不识别的内置函数不以_开头的内置的为preFIX。见GCC提供的其他内置功能,为受影响的功能,包括那些在-ansi或严格的ISO C标准-std选项是因为它们不具有ISO标准含义,没有内置函数的详细信息。 [...]

Don't recognize built-in functions that do not begin with _builtin as prefix. See Other built-in functions provided by GCC, for details of the functions affected, including those which are not built-in functions when -ansi or -std options for strict ISO C conformance are used because they do not have an ISO standard meaning. [...]

这篇关于包括在glibc的数学库现在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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