我想调用一个位于sin.c中的函数,而主文件位于test1.c中
文件看起来像这样:
文件test1.c:
#include <stdio.h>
#include <stdlib.h>
#include "sin.h"
int main(){
float angle;
double sinValue;
printf("Please enter a angle: ");
scanf("%f", &angle);
sinValue = sin(angle);
printf("the sin value of this angle is: %2.7f.", sinValue);
printf("program terminated");
return 0;
}
这是头文件:
在sin.h中:
extern double sin(float angle);
在文件sin.c中:
#include <math.h>
#include <stdlib.h>
#define EPSILON 0.0000001;
int fact(int n);
double sin(float angle){
float rad;
float pi = M_PI;
double newSin, oldSin;
double n = 1.0;
double token;
//find the radians
rad = angle * M_PI / 180.0;
newSin = rad;
//find the approxmate value of sin(x)
while((newSin - oldSin) > EPSILON ){
oldSin = newSin;
token = 2.0 * n - 1.0;
newSin = oldSin + pow(-1.0, n) * pow(rad, token) / fact(token);
n++;
}
return newSin;
}
问题是当我编译test1.c时,错误消息显示:
sin.h:1:15: warning: conflicting types for built-in function ‘sin’ [enabled by default]
extern double sin(float angle);
^
/tmp/ccxzixfm.o: In function `main':
test1.c:(.text+0x39): undefined reference to `sin'
collect2: error: ld returned 1 exit status
make: *** [test1] Error 1
它已经在头文件中声明了,并且我还包含了该头文件,所以有什么错误。我很混乱。
谢谢,约翰。
我使用“ make”命令来编译test1.c
这是编译过程:
zxz111@ubuntu:~/Desktop/sin$ ls
sin.c sin.c~ sin.h test1.c test1.c~
zxz111@ubuntu:~/Desktop/sin$ make test1
cc test1.c -o test1
In file included from test1.c:3:0:
sin.h:1:15: warning: conflicting types for built-in function ‘sin’ [enabled by default]
extern double sin(float angle);
^
/tmp/ccxzixfm.o: In function `main':
test1.c:(.text+0x39): undefined reference to `sin'
collect2: error: ld returned 1 exit status
make: *** [test1] Error 1
zxz111@ubuntu:~/Desktop/sin$ make test1
您需要将两个源文件都传递给编译器。
如果您使用的是GCC,则可能是:
gcc sin.c main.c -o main
尽管您的fact()
函数似乎没有在任何地方sin()
定义,并且<math.h>
您已经在其中定义了命名函数,但您可能想重命名您的函数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句