相关疑难解决方法(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万
查看次数

"未定义引用`pow'",即使是math.h和库链接-lm

我正在使用math.h-lm编译的选项.我尝试了所有:

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

但错误:

undefined reference to 'pow'  
Run Code Online (Sandbox Code Playgroud)

在所有情况下都会发生

c linux math gcc

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

在Math.h中调用函数时为什么会出现链接错误?

当试图调用函数时math.h,我收到如下链接错误

undefined reference to sqrt
Run Code Online (Sandbox Code Playgroud)

但我正在做一个#include <math.h>
我正在使用gcc并编译如下:

gcc -Wall -D_GNU_SOURCE blah.c -o blah
Run Code Online (Sandbox Code Playgroud)

为什么链接器找不到定义sqrt

math linker gcc

5
推荐指数
1
解决办法
7301
查看次数

链接错误 - gcc -lm

好吧,我认为我的问题有点有趣,我想了解我的Ubuntu盒子里发生了什么.

我编译并链接gcc -lm -o useless useless.c了以下无用的代码:

/*File useless.c*/
#include <stdio.h>
#include <math.h>
int main()
{
    int sample = (int)(0.75 * 32768.0 * sin(2 * 3.14 * 440 * ((float) 1/44100)));
    return(0);
}
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好.但是当我改变这个:

/*File useless.c*/
#include <stdio.h>
#include <math.h>
int main()
{
    int freq = 440;
    int sample = (int)(0.75 * 32768.0 * sin(2 * 3.14 * freq * ((float) 1/44100)));
    return(0);
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用相同的命令行编译,gcc响应:

/tmp/cctM0k56.o: In function `main':
ao_example3.c:(.text+0x29): undefined reference to `sin'
collect2: ld returned 1 exit …
Run Code Online (Sandbox Code Playgroud)

ubuntu linker gcc math.h ld

3
推荐指数
1
解决办法
3569
查看次数

标签 统计

gcc ×3

c ×2

linker ×2

math ×2

math.h ×2

compilation ×1

ld ×1

linux ×1

ubuntu ×1