在C中的文件之间切换函数
嘿,我尝试使用另一个文件的函数,这不是问题,但如果我也想使用另一个文件的函数,我会遇到一个问题: test1.c在C中的文件之间切换函数,c,include,C,Include,嘿,我尝试使用另一个文件的函数,这不是问题,但如果我也想使用另一个文件的函数,我会遇到一个问题: test1.c #include "test2.c" int func1(int a, int b){ return func2(a, b); } #include "test1.c" int func2(int a, int b){ return a + b; } int main(void){ func1(10, 5);
#include "test2.c"
int func1(int a, int b){
return func2(a, b);
}
#include "test1.c"
int func2(int a, int b){
return a + b;
}
int main(void){
func1(10, 5);
}
#include "test.h"
int func1(int a, int b){
return func2(a, b);
}
#include "test.h"
int func2(int a, int b){
return a + b;
}
int main(void){
func1(10, 5);
}
#include "functions.h"
int func1(int a, int b){
return func2(a, b);
}
#include "functions.h"
int fun2(int a, int b){
return a + b;
}
int main(){
func1(10, 5);
}
test2.c
#include "test2.c"
int func1(int a, int b){
return func2(a, b);
}
#include "test1.c"
int func2(int a, int b){
return a + b;
}
int main(void){
func1(10, 5);
}
#include "test.h"
int func1(int a, int b){
return func2(a, b);
}
#include "test.h"
int func2(int a, int b){
return a + b;
}
int main(void){
func1(10, 5);
}
#include "functions.h"
int func1(int a, int b){
return func2(a, b);
}
#include "functions.h"
int fun2(int a, int b){
return a + b;
}
int main(){
func1(10, 5);
}
这是可行的,但若我得到一个错误,那个么就有几页include语句。有人知道如何解决这个问题吗?在
test1.c
中,您需要定义不在文件(*)中的正在使用的函数
有两种方法:
- 在C文件的顶部键入函数的原型
- 您提供了一个包含原型的头文件
int func2(int a, int b); // you provide a prototype
....
或:
test2.h文件为:
int func2(int a, int b); // prototype of functions in test2.c
(*)…或在文件中定义之前将使用的通常,您不应在其他C源文件中包含C源文件。 在您的特定情况下,您创建了一个依赖关系循环: c包括test2.c,它又包括test1.c,它包括test2.c。。。 相反,在头文件中声明函数原型,并包括: 测试1.h:
#ifndef TEST1_H
#define TEST1_H
int func2(int a, int b);
#endif
test2.h
#ifndef TEST2_H
#define TEST2_H
int func2(int a, int b);
#endif
现在您可以在test1.c
中包含test2.h
,在test2.c
中包含test1.h
。
或者,您可以在
test2.c
中声明func1
的原型,反之亦然,但在我看来,使用头文件是更干净的解决方案。文件test1.c
和test2.c
递归地相互包含
您需要在头文件中放置公共声明(特别是带有外部链接的声明)
因此,创建一个标头,在其中放置函数声明,如
测试.h
#ifndef TEST_H
#define TEST_H
int func1(int a, int b);
int func2(int a, int b);
#endif
#ifdef FUNCTIONS_H
#define FUNCTIONS_H
int func1(int a, int b);
int func2(int a, int b);
#endif
然后创建文件test1.c
test1.c
#include "test2.c"
int func1(int a, int b){
return func2(a, b);
}
#include "test1.c"
int func2(int a, int b){
return a + b;
}
int main(void){
func1(10, 5);
}
#include "test.h"
int func1(int a, int b){
return func2(a, b);
}
#include "test.h"
int func2(int a, int b){
return a + b;
}
int main(void){
func1(10, 5);
}
#include "functions.h"
int func1(int a, int b){
return func2(a, b);
}
#include "functions.h"
int fun2(int a, int b){
return a + b;
}
int main(){
func1(10, 5);
}
文件test2.c
将如下所示
test2.c
#include "test2.c"
int func1(int a, int b){
return func2(a, b);
}
#include "test1.c"
int func2(int a, int b){
return a + b;
}
int main(void){
func1(10, 5);
}
#include "test.h"
int func1(int a, int b){
return func2(a, b);
}
#include "test.h"
int func2(int a, int b){
return a + b;
}
int main(void){
func1(10, 5);
}
#include "functions.h"
int func1(int a, int b){
return func2(a, b);
}
#include "functions.h"
int fun2(int a, int b){
return a + b;
}
int main(){
func1(10, 5);
}
虽然函数
func2
的定义也可以放在一个单独的.c
文件中。您需要创建一个头文件,例如functions.h
#ifndef TEST_H
#define TEST_H
int func1(int a, int b);
int func2(int a, int b);
#endif
#ifdef FUNCTIONS_H
#define FUNCTIONS_H
int func1(int a, int b);
int func2(int a, int b);
#endif
在头文件中,您需要写入在test1.c和test2.c文件中找到的函数名
函数.h
#ifndef TEST_H
#define TEST_H
int func1(int a, int b);
int func2(int a, int b);
#endif
#ifdef FUNCTIONS_H
#define FUNCTIONS_H
int func1(int a, int b);
int func2(int a, int b);
#endif
在test1.c文件中,您需要包括functions.h
#ifndef TEST_H
#define TEST_H
int func1(int a, int b);
int func2(int a, int b);
#endif
#ifdef FUNCTIONS_H
#define FUNCTIONS_H
int func1(int a, int b);
int func2(int a, int b);
#endif
test1.c
#include "test2.c"
int func1(int a, int b){
return func2(a, b);
}
#include "test1.c"
int func2(int a, int b){
return a + b;
}
int main(void){
func1(10, 5);
}
#include "test.h"
int func1(int a, int b){
return func2(a, b);
}
#include "test.h"
int func2(int a, int b){
return a + b;
}
int main(void){
func1(10, 5);
}
#include "functions.h"
int func1(int a, int b){
return func2(a, b);
}
#include "functions.h"
int fun2(int a, int b){
return a + b;
}
int main(){
func1(10, 5);
}
在test2.c文件中,还需要包含functions.h
test2.c
#include "test2.c"
int func1(int a, int b){
return func2(a, b);
}
#include "test1.c"
int func2(int a, int b){
return a + b;
}
int main(void){
func1(10, 5);
}
#include "test.h"
int func1(int a, int b){
return func2(a, b);
}
#include "test.h"
int func2(int a, int b){
return a + b;
}
int main(void){
func1(10, 5);
}
#include "functions.h"
int func1(int a, int b){
return func2(a, b);
}
#include "functions.h"
int fun2(int a, int b){
return a + b;
}
int main(){
func1(10, 5);
}
您需要两个头文件——一个为test1.h,另一个为test2.h。在头文件中定义这些函数的原型。包括头文件而不是源文件(.c)。只是为了澄清前面的注释:您需要的原型声明如下:在
test1.h
:intfunc1(inta,intb)代码>在test2.h
:intfunc2(inta,intb)代码>。这是两个头文件中所需的全部内容,但您可能希望在两个文件中都添加一个。通常,您应该避免包含.c
文件。相反,编译并链接.c
文件。在相应的.h
中声明外部对象。文件,并且仅包括.h
文件。这样,您就不会复制代码,而只复制声明。