当前位置: 代码迷 >> 单片机 >> c51创建头文件有关问题
  详细解决方案

c51创建头文件有关问题

热度:45   发布时间:2016-04-28 16:05:43.0
c51创建头文件问题
//**********main.c*************
#include "stc12c5a60s2.h"
 #include "nothing.h"

 void main()
 {
  x=1;
  function(); 
 }
//*******************************


//***************nothing.h*********
#ifndef __NOTHING_H__
#define __NOTHING_H__
#include "stc12c5a60s2.h" 
extern uchar x;
void funtion();
#endif
//*********************************

//*************nothing.c**********
 #include "nothing.h"
uchar x;
 void function()
 {
  if(x)
P2=0;
 }
//***************************

警告提示:MAIN.C(9): warning C206: 'function': missing function-prototype


------解决方案--------------------
extern void funtion();

void main()
。。。
------解决方案--------------------
探讨

extern void funtion();

void main()
。。。

------解决方案--------------------
不是这样的,你的这个
//*************nothing.c**********
 #include "nothing.h"
uchar x;
 void function()
 {
  if(x)
P2=0;
 }
//***************************
把 #include "nothing.h"这个改为#include "stc12c5a60s2.h"保存为nothing.h
------解决方案--------------------
#ifndef __NOTHING_H__
#define __NOTHING_H__
#include "stc12c5a60s2.h" 
extern uchar x;
extern void funtion();
#endif
------解决方案--------------------
1)可以直接在*.h中定义 uchar x;,两个.c文件中已包含.h,所以都不必再声明x;

2)也可以在main.c中直接包含nothing.c,使得funtion()在main中有定义。

 
  相关解决方案