在 C 中创建自己的头文件 [英] Creating your own header file in C
本文介绍了在 C 中创建自己的头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谁能用一个简单的例子从头到尾解释如何用C创建头文件.
Can anyone explain how to create a header file in C with a simple example from beginning to end.
推荐答案
foo.h
#ifndef FOO_H_ /* Include guard */
#define FOO_H_
int foo(int x); /* An example function declaration */
#endif // FOO_H_
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
", y);
return 0;
}
使用 GCC 编译
gcc -o my_app main.c foo.c
这篇关于在 C 中创建自己的头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文