从头文件中声明的另一个文件调用函数
Calling function from another file declared in a header file
我想调用 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>
中定义,您可能想重命名您的函数。
您需要确保您正在编译这两个文件。
因此,例如,如果您使用 g++ 进行编译,它将是:
g++ sin.c test1.c -o run
ld 返回了 1 个退出状态:::: 这是链接器错误。这意味着当您的链接器正在搜索符号 "sin" 时,它无法找到。
您收到此错误的原因是(因为您没有添加整个 sin.h,我假设),您没有在 "sin.h" 中包含 "sin.c"。
此外,编译这两个文件,以便生成 sin.o,您的链接器将能够从那里映射符号。
警告:内置函数“sin”的类型冲突[默认启用]
extern double sin(浮角);
另外,尽量避免使用已经在标准库中定义的函数名称。
函数声明默认有外部链接。要消除警告,请从 sin.h
.
中的函数原型中删除 extern
编译时,先编译sin.c
,但不要链接,例如。 cc -c sin.c
。这将生成一个目标文件,可能名为 sin.o
.
然后您可以编译 test1.c
,将目标文件链接到其中,如下所示:cc test1.c sin.o -o test1
我想调用 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>
中定义,您可能想重命名您的函数。
您需要确保您正在编译这两个文件。 因此,例如,如果您使用 g++ 进行编译,它将是:
g++ sin.c test1.c -o run
ld 返回了 1 个退出状态:::: 这是链接器错误。这意味着当您的链接器正在搜索符号 "sin" 时,它无法找到。
您收到此错误的原因是(因为您没有添加整个 sin.h,我假设),您没有在 "sin.h" 中包含 "sin.c"。
此外,编译这两个文件,以便生成 sin.o,您的链接器将能够从那里映射符号。
警告:内置函数“sin”的类型冲突[默认启用] extern double sin(浮角); 另外,尽量避免使用已经在标准库中定义的函数名称。
函数声明默认有外部链接。要消除警告,请从 sin.h
.
extern
编译时,先编译sin.c
,但不要链接,例如。 cc -c sin.c
。这将生成一个目标文件,可能名为 sin.o
.
然后您可以编译 test1.c
,将目标文件链接到其中,如下所示:cc test1.c sin.o -o test1