【发布时间】:2014-10-13 13:23:09
【问题描述】:
谁能告诉我为什么我在 gcc 中编译我的程序时会出现这个错误。我已经包含了数学库。我已包含-lm。我的编译器有问题吗?
$ gcc -lm -Wall *.c
main.c: In function ‘main’:
/tmp/ccqqxFDD.o: In function `main':
main.c:(.text+0x324): undefined reference to `sqrt'
collect2: ld returned 1 exit status
来了。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[])
{
int x1 = 5;
int x2 = 7;
int y1 = 11;
int y2 = 8;
double distance = 0;
distance=sqrt((pow((x2- x1), 2))+(pow((y2 - y1), 2)));
printf(" distance is: %lf \n", distance);
return 0;
}
【问题讨论】:
-
我认为您需要将库放在源文件之后:- gcc -Wall *.c -lm 以便它知道要寻找哪些符号来解决问题
-
尝试编译一个最小的程序:
#include <math.h> \n int main() { sqrt(3.0); return 0; },看看它是否会重现问题。 -
我们确实需要看一些代码/源代码,否则我们只是在猜测。
标签: c