在 C 中创建自己的头文件

标签 c header include include-guards c-header

谁能用一个简单的例子从头到尾解释一下如何在 C 中创建头文件。

最佳答案

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;
}

ma​​in.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;
}

使用GCC编译

gcc -o my_app main.c foo.c

关于在 C 中创建自己的头文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7109964/

相关文章:

c - 更新函数中的指针

PHP - 当变量放入 url 时,include() 文件不起作用?

c - 如何获得 C 的 Unix 时间戳( double 或 float )

c - 如何计算字符串列表的出现次数并将其输出到新文件?

javascript - 客户端如何使用CORS跨域?

c++ - 包含在许多翻译单元中时静态常量的开销?

c++ - 在 Xcode 控制台项目中创建和使用 C++ 库

PHP:可以包含 file_exists() 说不存在的文件

php - PHP 包含需要 HTML 和正文标记吗?

c - C调试宏的奇怪语法