Creating your own header file in C

终于跑起来了,含自定义 include .h 的c语言程序,超开心呀!

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语言的书不错,学习之,加油!!! http://publications.gbdirect.co.uk/c_book/chapter1/functions.html

参考: http://stackoverflow.com/questions/7109964/creating-your-own-header-file-in-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
", y);
    return 0;
}

To compile using GCC

gcc -o my_app main.c foo.c
原文地址:https://www.cnblogs.com/oxspirt/p/6207053.html