【发布时间】:2012-08-30 06:30:14
【问题描述】:
我正在移植我拥有的一些 C++ 代码。它在 C++ 中完美运行,所以我不知道我做错了什么。
我有“a.c”和“b.c”。它们都包括“a.h”,而后者又包括“b.h”。 "b.c" 中的函数在其头文件中声明(使用extern 关键字),其中之一是从"a.c" 调用的。它编译并运行,但调用似乎从未通过(我什至在其中添加了几行 printf() 进行诊断,但没有任何反应)。
我知道我忽略了一些简单的事情,我就是无法确定是什么。任何帮助表示赞赏。
编辑:粘贴一些简化代码: 编辑 2:通话通过,有一个库问题需要修复。现在索引变量没有增加,我认为这可能是一个范围问题。
在“交流”中
#include "a.h"
...
int sSimDisp(void){
buttons();
printf("x = %04d\n", index);
...
}
int main(){
...
while(1) {
sSimDisp();
...
}
return 0;
}
在“啊.h”中
#ifndef a_H_
#define a_H_
...
#include "b.h"
#include "c.h"
int sSimDisp(void);
int main(void);
#endif /* a_H_ */
在“b.c”中
#include "a.h"
...
int index = 0;
void buttons(){
printf("Entry to button subroutine.\n");
index++;
...
}
void touchscreen(){
...
}
在“b.h”中
#ifndef b_H_
#define b_H_
...
extern int index;
//button handler
void buttons();
//touch screen handler
void touchscreen();
#endif /* b_H_ */
【问题讨论】:
-
您的
printf通话结束时是否有'\n'? -
"带 extern 关键字" - 所有函数默认为
extern -
你能做一个最小的例子给我们看吗?参见例如sscce.org
-
我会精简代码并粘贴它。我只在变量上使用了“extern”,这是我发帖时的错误。
-
尝试在调试器中运行它,并单步执行代码以查看它的作用。