相关疑难解决方法(0)

为什么必须在C中链接数学库?

如果我包含<stdlib.h><stdio.h>在C程序中,我不必在编译时链接这些,但我必须链接到<math.h>,使用-lmgcc,例如:

gcc test.c -o test -lm
Run Code Online (Sandbox Code Playgroud)

这是什么原因?为什么我必须显式链接数学库而不是其他库?

c compilation math.h

241
推荐指数
7
解决办法
11万
查看次数

gcc:为什么链接数学库需要-lm标志?

我刚刚发现-lmgcc需要这个标志,以便编译一个从数学库中引用函数的程序.我想知道为什么在编译包含其他库(如时间库)的程序时不需要显式链接标志.如果我编写一个time()调用该函数的程序,即使没有链接选项也可以编译没有任何问题.但是如果没有-lm旗帜,那么涉及数学库的程序将无法运行.

任何人都可以解释这种行为背后的原因吗?谢谢你的时间.

gcc libc

62
推荐指数
0
解决办法
4万
查看次数

将代码与 `floor();`、`ceil();` 和 `pow();` 链接时出错

我在 GNU/Linux Debian 8.5 下编码

我有一个简单的程序。

如果我用gcc prog.c它编译它就可以了!

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

int main(int argc, char const *argv[]) {

    float _f = 3.1415f;

    floor(_f);
    ceil(_f);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我添加pow(),它会说它找不到pow,我需要添加gcc prog.c -lm以使其正确。

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

int main(int argc, char const *argv[]) {

    float _f = 3.1415f;

    floor(_f);
    ceil(_f);
    pow(_f, 2);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果我是对的,pow(), ceil(),floor()都是来自<math.h>?

那么,为什么不floor() …

c flags math.h linker-errors undefined-reference

2
推荐指数
1
解决办法
1029
查看次数

标签 统计

c ×2

math.h ×2

compilation ×1

flags ×1

gcc ×1

libc ×1

linker-errors ×1

undefined-reference ×1