Tag: 包括警卫

C头文件循环

我有几个头文件,归结为: tree.h中: #include “element.h” typedef struct tree_ { struct *tree_ first_child; struct *tree_ next_sibling; int tag; element *obj; …. } tree; 和element.h: #include “tree.h” typedef struct element_ { tree *tree_parent; char *name; … } element; 问题是它们都相互引用,因此树需要包含元素,元素需要包含树。 这不起作用,因为要定义“树”结构,元素结构必须已知,但要定义元素结构,必须知道树结构。 如何解决这些类型的循环(我认为这可能与’前向声明’有关?)?

标题/包括警卫不起作用?

出于某种原因,我在头文件中获得了多个内容声明,即使我正在使用标题保护。 我的示例代码如下: main.c中: #include “thing.h” int main(){ printf(“%d”, increment()); return 0; } thing.c: #include “thing.h” int increment(){ return something++; } thing.h: #ifndef THING_H_ #define THING_H_ #include int something = 0; int increment(); #endif 当我尝试编译它时,GCC说我对some变量有多个定义。 ifndef应该确保不会发生这种情况,所以我很困惑为什么会这样。

为什么头文件中的全局变量会导致链接错误?

我总是得到以下错误,即使我已将include guard放入头文件中。 duplicate symbol _Bittorrent in: /Users/tracking/Library/Developer/Xcode/DerivedData/SHT-giuwkwyqonghabcqbvbwpucmavvg/Build/Intermediates/SHT.build/Debug/SHT.build/Objects-normal/x86_64/main.o /Users/tracking/Library/Developer/Xcode/DerivedData/SHT-giuwkwyqonghabcqbvbwpucmavvg/Build/Intermediates/SHT.build/Debug/SHT.build/Objects-normal/x86_64/Handshake.o duplicate symbol _eight_byte in: /Users/tracking/Library/Developer/Xcode/DerivedData/SHT-giuwkwyqonghabcqbvbwpucmavvg/Build/Intermediates/SHT.build/Debug/SHT.build/Objects-normal/x86_64/main.o /Users/tracking/Library/Developer/Xcode/DerivedData/SHT-giuwkwyqonghabcqbvbwpucmavvg/Build/Intermediates/SHT.build/Debug/SHT.build/Objects-normal/x86_64/Handshake.o ld: 2 duplicate symbols for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) 这是.h头文件,.c文件和main.c main.c中 #include “Handshake.h” int main(int argc, char** argv) { // some code. return 0; } Handshake.h #ifndef SHT_Handshake_h #define […]

在C中包含保护约定

设置包含警卫的常规方法是什么? 我通常把它们写成(例如h.h): #ifndef _EXAMPLE_H_ #define _EXAMPLE_H_ #include “example.h” #endif 强调惯例是否重要? 当我用Google搜索时,我看到了相互矛盾的信息。 _EXAMPLE_H_甚至必须匹配标题的名称吗?