1 c linker-errors undefined-reference
我正在尝试编写一个程序来估算pi.它基本上取0.00和1.00之间的随机点,并将它们与圆的界限进行比较,圆内点与总点的比率应接近pi(非常快速的解释,规范深入更多).
但是,在使用gcc进行编译时出现以下错误:
Undefined first referenced
symbol in file
pow /var/tmp//cc6gSbfE.o
ld: fatal: symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
发生了什么事?我以前从未见过这个错误,我不知道它为什么会出现.这是我的代码(虽然我没有完全测试它,因为我无法通过错误):
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void) {
float x, y;
float coordSquared;
float coordRoot;
float ratio;
int n;
int count;
int i;
printf("Enter number of points: ");
scanf("%d", &n);
srand(time(0));
for (i = 0; i < n; i++) {
x = rand();
y = rand();
coordSquared = pow(x, 2) + pow(y, 2);
coordRoot = pow(coordSquared, 0.5);
if ((x < coordRoot) && (y < coordRoot)) {
count++;
}
}
ratio = count / n;
ratio = ratio * 4;
printf("Pi is approximately %f", ratio);
return 0;
}
Run Code Online (Sandbox Code Playgroud)