读书人

头文件重复定义的有关问题

发布时间: 2013-01-23 10:44:49 作者: rapoo

头文件重复定义的问题。
多个.c文件包含一个.h文件。
.h里面有定义变量。
.h 用了
#ifndef
#define
#endif.
。现在怎么解决重复定义的问题? C
[解决办法]

引用:
引用:
例如这样
sample.h
C/C++ code?1234#ifndef __SAMPLE_H_#define __SAMPLE_H_extern int a;#endif

sample.c
C/C++ code?1int a=0;
如果在头文件里面定义的是函数呢?


只写原型,实现扔到.c里
sample.h

#ifndef __SAMPLE_H_
#define __SAMPLE_H_
int func(int a);
#endif

sample.c

#include "sample.h"
int func(int a)
{
return a+1;
}

读书人网 >C语言

热点推荐