链接.H与.c文件与#IFDEF头卫士 [英] Linking .h files with .c with #ifdef header guards

查看:199
本文介绍了链接.H与.c文件与#IFDEF头卫士的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

连接h和.c文件有问题的IM,我也看到了关于这个问题,所有的人都有些线程是有点含糊和我仍不能完全掌握它的概念,并有很多IM连接问题,说我有BC和bh,我将在AC 使用和IM困惑是否包括BH AC和BC中的Cuz BC本身需要知道BH定义的结构,我有一些功能,在波黑它的原型,并在公元前也使用结构BH定义,即时我不包括在卑诗省的Cuz的什么我知道BH更像是交流的界面,将使用功能于公元前BH。这里更明显的例子。

im having trouble linking .h and .c files, i've also read some threads regarding this problem and all of them is a bit vague and still i can't fully grasp the concept of it, and im having a lot of linking problems, Say i have b.c and b.h which i will use in a.c, and im confused whether to include b.h both a.c and b.c cuz b.c itself needs to know the structure defined in b.h, i have some function which has its prototype in b.h and is defined in b.c which also use the structure in b.h, im am not including b.h in b.c cuz as what i know b.h is more like an interface to a.c which will use the functions in b.c... Here a more clear example

b.h文件

typedef struct{
int x, y;
}myStruct;

void funct1(myStruct);
void funct2(myStruct);

b.c文件

void funct1(myStruct x)
{
    //do something
}

void funct2(myStruct y)
{
     //do something
} 

交流转换文件

#include "b.h"

int main()
{
myStruct x;
  funct1(x);
  funct2(y);
return 0;
}

执行该命令在Cygwin中: GCC b.c交流转换器-g

现在混乱的部分,我有一个链接错误,其中当b.c编译它不能检测的结构和B.H.原型所有的Cuz我知道的是,b.h用于从交流转换器链接b.c但是当两者的.c编译似乎b.c无法找到其strucutre和原型,

Now the confusing part, i have a linking error wherein when b.c is compiled it can't detect the structure and the prototypes in b.h. Cuz all i know is that b.h is used to link b.c from a.c but when both .c is compiled it seems that b.c can't find its strucutre and prototypes,

我为什么没有包括在b.h b.c?
:作为的Cuz我所知,BH已经包含在交流,当我在公元前再有,我会做双夹杂< ---这就是到目前为止,我什么我学知道有#IFDEF但现在看来,这是行不通的,也许我还是不知道如何使用它,如果你知道请随时讨论这个问题。

Why didn't i include b.h in b.c? Answer: Cuz as what i know, b.h is already included in a.c and when i include it again in b.c, i'll be doing double inclusions <--- thats what i learn so far and i know there is #ifdef but it seems it won't work, maybe i still don't know how to use it, if you know please feel free to discuss this.

如果您有任何想法,如何去了解这个随时告诉我一些。

If you have any idea as to how to go about this feel free to tell me some.

有一个#ifdef指令,但我似乎无法有任何想法如何做到这一点。

there is a #ifdef directive but i can't seem to have any idea how to do this.

请注意:假设所有高于codeS语法正确,如果有任何拼写错误的单词,请无视,我只.H .C和之间的夹杂物后

NOTE: ASSUME THAT ALL ABOVE CODES IS SYNTACTICALLY CORRECT if there are any misspelled word please ignore, i'm only after the inclusions between .h and .c

推荐答案

您确实需要的#include BH BC 。连接器接管之前,每个文件都是单独编译的,所以不要紧,已包含在交流转换器b.h,因为b.c是由本身编译并没有关于b.h内容主意,除非你包括它。

You do indeed need to #include b.h in b.c. Each file is compiled separately before the linker takes over, so it doesn't matter that you have included b.h in a.c, because b.c is compiled by itself and has no idea about the contents of b.h unless you include it.

下面是一个的#include 后卫的例子

// some_header_file.h
#ifndef SOME_HEADER_FILE_H
#define SOME_HEADER_FILE_H
// your code
#endif

在任何地方被列入some_header_file.h,在 #indef #ENDIF 将被忽略之间的一切如果SOME_HEADER_FILE_H已经定义,这将发生在它被包含在编译单元的第一次。

When some_header_file.h is included anywhere, everything in between the #indef and the #endif will be ignored if SOME_HEADER_FILE_H has been defined, which will happen on the first time it is included in the compilation unit.

这是常见的做法来命名的#define 该文件的名称后,以确保您的项目中的唯一性。我喜欢美元,我的项目或命名空间的名称p $ PFIX它为好,以减少与其他code冲突的风险,以及

It is common practice to name the #define after the name of the file, to ensure uniqueness within your project. I like to prefix it with the name of my project or namespace as well, to reduce the risk of clashes with other code, as well.

请注意:相同的头文件可以包含即使上述项目中多次包括门卫,它只是不能在同一编译单元内包含了两次。这表现如下:

NOTE: The same header file CAN be included multiple times within your project even with the above include guard, it just can't be included twice within the same compilation unit. This is demonstrated as follows:

// header1.h
#ifndef HEADER_H
#define HEADER_H
int test1 = 1;
#endif

// header2.h
#ifndef HEADER_H
#define HEADER_H
int test2 = 2;
#endif

现在,让我们看看会发生什么,当我们试图包括上述两个文件。在一个单一的编译单元:

Now let's see what happens when we try to include the above two files. In a single compilation unit:

// a.cpp
#include "header1.h"
#include "header2.h"
#include <iostream>
int main()
{
   std::cout << test1;
   std::cout << test2;
};

这生成编译器错误,因为test2的是没有定义 - 它在header2.h被忽略,因为HEADER_H已经由包括时定义。现在,如果我们包括单独的编译单元,每个单元标题:

This generates a compiler error because test2 is not defined - it is ignored in header2.h because HEADER_H is already defined by the time that is included. Now if we include each header in separate compilation units:

// a.cpp
#include "header2.h"
int getTest2()
{
   return test2;
};

// b.cpp
#include "header1.h"
#include <iostream>
int getTest2(); // forward declaration
int main()
{
   std::cout << test1;
   std::cout << getTest2();
};

它编译罚款,并产生预期的输出(1和2),即使我们是其中包括两个文件,​​这两个定义HEADER_H。

It compiles fine and produces the expected output (1 and 2), even though we are including two files which both define HEADER_H.

这篇关于链接.H与.c文件与#IFDEF头卫士的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆