c - 在 C 中的文件之间切换函数
问题描述
嘿,我尝试使用另一个文件的功能,这不是问题,但是如果我也想使用另一个文件的功能,我会遇到问题:
测试1.c
#include "test2.c"
int func1(int a, int b){
return func2(a, b);
}
测试2.c
#include "test1.c"
int func2(int a, int b){
return a + b;
}
int main(void){
func1(10, 5);
}
这可行,但如果我收到错误,则包含包含语句的页面。有没有人知道如何解决这个问题?
解决方案
通常,您不应在其他 C 源文件中包含 C 源文件。在您的特定情况下,您创建了一个依赖循环:test1.c 包含 test2.c,其中再次包含 test1.c,其中包含 test2.c ...相反,在头文件中声明函数原型并包含这些:
测试1.h:
#ifndef TEST1_H
#define TEST1_H
int func2(int a, int b);
#endif
测试2.h
#ifndef TEST2_H
#define TEST2_H
int func2(int a, int b);
#endif
现在您可以包含test2.h
intest1.c
和test1.h
in test2.c
。或者,您可以声明 in 的原型,func1
反之亦然test2.c
,但在我看来,使用头文件是更简洁的解决方案。
推荐阅读
- redis - Redis - 如何将具有相同键的多行加载到 Redis 中?
- vim - 使用文件类型插件文件(ftplugin)改变vim/neovim中的文件类型
- html - Bootstrap 4 IE 没有在 card-img-overlay 上使用 col-sm-12
- angular - 我的表单从不使用反应式表单提交
- javascript - 在我的 JS 蛇游戏中,蛇不能在画布的边缘移动
- python - 如何从熊猫数据框中求和一个切片
- javascript - 未找到未捕获的表 alasql
- javascript - javascript元字符小写字母
- python - 如何使用 Watson Studio 获取 IBM COS Bucket 中的文件列表
- python - 如何在 python 中使用广义回归神经网络?