在头文件中定义结构体,然后在需要使用它的源文件中包含该头文件即可。例如:
定义头文件struct.h:
#ifndef STRUCT_H
#define STRUCT_H
struct MyStruct {
int x;
int y;
};
#endif
源文件source1.c:
#include "struct.h"
struct MyStruct myStruct1 = {1, 2};
源文件source2.c:
#include "struct.h"
struct MyStruct myStruct2 = {3, 4};
这样,不同的源文件都会包含同一个结构体的定义,解决了不同的翻译单元中有相同的结构体定义的问题。
下一篇:不同的非线性dag执行