I want to call a function that is in sin.c, and the main file is in test1.c
and the files look like this:
file 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;
}
this is the header file:
In sin.h:
extern double sin(float angle);
In file 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;
}
The problem is when I compile the test1.c the error message shows:
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
It is already declared in the header file and I also included that header file, so what is the error. I'm so confused.
Thanks before, John.
I use the "make" command to compile the test1.c
Here is the compilation process:
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
You need to pass both source files to the compiler.
If you're using GCC it would be:
gcc sin.c main.c -o main
Although your fact()
function doesn't seem to be defined anywhere and a function named sin()
is already defined in <math.h>
you probably wanna rename yours.