相同的结构不同的文件
same struct different files
我有个小问题。
我有 2 个文件:one.c 和 two.c
他们都 decler 并实现了结构:StackNode
头文件是:
one.h:
#ifndef ONE_H
#define ONE_H
typedef struct StackNode StackNode;
#endif
two.h:
#ifndef TWO_H
#define TWO_H
#include "one.h"
#endif
cpp 文件:
one.c:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#include "one.h"
struct StackNode
{
........
};
two.c:
#include <stdio.h>
#include <malloc.h>
#include "two.h"
struct StackNode
{
........
};
为什么在 linox 中编译和 运行 但在 visual stutio 中它说:
two.obj:错误 LNK2005:"struct StackNode * top" (?top@@3PAUStackNode@@A) 已在 one.obj 中定义
1>c:\users\documents\visual studio 2010\Projects\Exercise\Debug\Exercise.exe:致命错误 LNK1169:找到一个或多个多重定义的符号
我该怎么做才能让它在视觉上也能正常工作?
谢谢:)
链接器并没有说结构本身被定义了两次。它说对象 top
像 struct StackNode * top
一样被定义了两次。您只需在一个编译单元中定义它。
two.obj : error LNK2005: "struct StackNode * top"
(?top@@3PAUStackNode@@A) already defined in one.obj
我有个小问题。 我有 2 个文件:one.c 和 two.c 他们都 decler 并实现了结构:StackNode 头文件是: one.h:
#ifndef ONE_H
#define ONE_H
typedef struct StackNode StackNode;
#endif
two.h:
#ifndef TWO_H
#define TWO_H
#include "one.h"
#endif
cpp 文件: one.c:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#include "one.h"
struct StackNode
{
........
};
two.c:
#include <stdio.h>
#include <malloc.h>
#include "two.h"
struct StackNode
{
........
};
为什么在 linox 中编译和 运行 但在 visual stutio 中它说: two.obj:错误 LNK2005:"struct StackNode * top" (?top@@3PAUStackNode@@A) 已在 one.obj 中定义 1>c:\users\documents\visual studio 2010\Projects\Exercise\Debug\Exercise.exe:致命错误 LNK1169:找到一个或多个多重定义的符号
我该怎么做才能让它在视觉上也能正常工作? 谢谢:)
链接器并没有说结构本身被定义了两次。它说对象 top
像 struct StackNode * top
一样被定义了两次。您只需在一个编译单元中定义它。
two.obj : error LNK2005: "struct StackNode * top" (?top@@3PAUStackNode@@A) already defined in one.obj