谁能用一个简单的例子从头到尾解释一下如何在 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;
}
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;
}
使用GCC编译
gcc -o my_app main.c foo.c
关于在 C 中创建自己的头文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7109964/