当前位置: 技术问答>linux和unix
C头文件互相引用的问题
来源: 互联网 发布时间:2015-12-25
本文导语: +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ a.h 如下 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #ifndef _A_H_ #define _A_H_ #include "b.h" struct _struct_a { int test_int; char test_char; }; struct __struct_c {...
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
a.h 如下
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#ifndef _A_H_
#define _A_H_
#include "b.h"
struct _struct_a
{
int test_int;
char test_char;
};
struct __struct_c
{
struct _struct_b b;
int test_c;
};
#endif
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
b.h如下
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#ifndef _B_H_
#define _B_H_
#include "a.h"
struct _struct_b
{
struct _struct_a a;
int test_b;
};
#endif
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
c.c如下
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include "a.h"
#include "b.h"
int main()
{
struct _struct_a a;
struct _struct_b b;
struct _struct_c c;
return 0;
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
在不合并头文件的前提下,是否可以编译通过,如何更改???
a.h 如下
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#ifndef _A_H_
#define _A_H_
#include "b.h"
struct _struct_a
{
int test_int;
char test_char;
};
struct __struct_c
{
struct _struct_b b;
int test_c;
};
#endif
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
b.h如下
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#ifndef _B_H_
#define _B_H_
#include "a.h"
struct _struct_b
{
struct _struct_a a;
int test_b;
};
#endif
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
c.c如下
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include "a.h"
#include "b.h"
int main()
{
struct _struct_a a;
struct _struct_b b;
struct _struct_c c;
return 0;
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
在不合并头文件的前提下,是否可以编译通过,如何更改???
|
大概是不行的吧
_sturct_c需要知道_struct_b
_struct_b需要知道_struct_a
可以把_struct_a与_struct_c放在不同.h
或者干脆把三个放在同一个.h
_sturct_c需要知道_struct_b
_struct_b需要知道_struct_a
可以把_struct_a与_struct_c放在不同.h
或者干脆把三个放在同一个.h
|
Yes, you can compile the program without combining the head files. but you have to modify the a.h file as follows:
move the #include "b.h" after declaration of _struct_a because in b.h the _struct_a is used.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
a.h 如下
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#ifndef _A_H_
#define _A_H_
struct _struct_a
{
int test_int;
char test_char;
};
#include "b.h"
struct __struct_c
{
struct _struct_b b;
int test_c;
};
#endif
move the #include "b.h" after declaration of _struct_a because in b.h the _struct_a is used.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
a.h 如下
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#ifndef _A_H_
#define _A_H_
struct _struct_a
{
int test_int;
char test_char;
};
#include "b.h"
struct __struct_c
{
struct _struct_b b;
int test_c;
};
#endif