C语言 在 C 中创建自己的头文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7109964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:25:39  来源:igfitidea点击:

Creating your own header file in C

cheaderincludeinclude-guardsc-header

提问by Anuragdeb3

Can anyone explain how to create a header file in C with a simple example from beginning to end.

任何人都可以用一个简单的例子从头到尾解释如何在 C 中创建头文件。

回答by Oliver Charlesworth

foo.h

foo.h

#ifndef FOO_H_   /* Include guard */
#define FOO_H_

int foo(int x);  /* An example function declaration */

#endif // FOO_H_

foo.c

foo.c

#include "foo.h"  /* Include the header (not strictly necessary here) */

int foo(int x)    /* Function definition */
{
    return x + 5;
}

main.c

主文件

#include <stdio.h>
#include "foo.h"  /* Include the header here, to obtain the function declaration */

int main(void)
{
    int y = foo(3);  /* Use the function here */
    printf("%d\n", y);
    return 0;
}

To compile using GCC

使用 GCC 编译

gcc -o my_app main.c foo.c

回答by Flavius

#ifndef MY_HEADER_H
# define MY_HEADER_H

//put your function headers here

#endif

MY_HEADER_Hserves as a double-inclusion guard.

MY_HEADER_H作为双重包容守卫。

For the function declaration, you only need to define the signature, that is, without parameter names, like this:

对于函数声明,只需要定义签名,即不带参数名,像这样:

int foo(char*);

If you really want to, you can also include the parameter's identifier, but it's not necessary because the identifier would only be used in a function's body (implementation), which in case of a header (parameter signature), it's missing.

如果你真的想要,你也可以包含参数的标识符,但这不是必需的,因为标识符只会在函数的主体(实现)中使用,在头(参数签名)的情况下,它会丢失。

This declaresthe function foowhich accepts a char*and returns an int.

声明foo接受 achar*并返回a的函数int

In your source file, you would have:

在您的源文件中,您将拥有:

#include "my_header.h"

int foo(char* name) {
   //do stuff
   return 0;
}

回答by TommyGunn32

myfile.h

我的文件.h

#ifndef _myfile_h
#define _myfile_h

void function();

#endif

myfile.c

我的文件

#include "myfile.h"

void function() {

}

回答by djsumdog

header files contain prototypes for functions you define in a .c or .cpp/.cxx file (depending if you're using c or c++). You want to place #ifndef/#defines around your .h code so that if you include the same .h twice in different parts of your programs, the prototypes are only included once.

头文件包含您在 .c 或 .cpp/.cxx 文件中定义的函数的原型(取决于您使用的是 c 还是 c++)。你想在你的 .h 代码周围放置 #ifndef/#defines,这样如果你在程序的不同部分包含两次相同的 .h,原型只包含一次。

client.h

客户端.h

#ifndef CLIENT_H
#define CLIENT_H

short socketConnect(char *host,unsigned short port,char *sendbuf,char *recievebuf, long rbufsize);


#endif /** CLIENT_H */

Then you'd implement the .h in a .c file like so:

然后你会在一个 .c 文件中实现 .h ,如下所示:

client.c

客户端

#include "client.h"

short socketConnect(char *host,unsigned short port,char *sendbuf,char *recievebuf, long rbufsize) {
 short ret = -1;
 //some implementation here
 return ret;
}