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\n", y);
return 0;
}
To compile using GCC
gcc -o my_app main.c foo.c
make
, you'd (presumably) still be using gcc
or cc
, and you would still need to tell make
to tell either compiler which files were needed. Why do you wish to avoid providing "foo.c" to the compiler? –
Kopeisk (not strictly necessary here)
Balderdash! Rubbish! If not included in translation unit where function is defined, the prototype could say one thing and the func definition something altogether different... ALWAYS include the header when call-er and call-ee must agree on function signatures... ALWAYS!! That is their sole purpose for existence!! –
Tameika main.c
, foo.c
, and foo.h
are local (in this case they are; note the quotes on #include "foo.h"
), it should suffice to use the command: gcc -o my_app main.c
(without foo.c
; with foo.h
in scope already in main.c
, gcc can find foo.c
to bring in the implementation) –
Triclinic #ifndef MY_HEADER_H
# define MY_HEADER_H
//put your function headers here
#endif
MY_HEADER_H
serves as a double-inclusion guard.
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 declares the function foo
which accepts a char*
and returns an int
.
In your source file, you would have:
#include "my_header.h"
int foo(char* name) {
//do stuff
return 0;
}
extern
declarations of variables and functions in a separate file, historically called a header, that is included by #include
at the front of each source file. The functions of the standard library, for example, are declared in headers like <stdio.h>
." –
Attractant myfile.h
#ifndef _myfile_h
#define _myfile_h
void function();
#endif
myfile.c
#include "myfile.h"
void function() {
}
void function();
as a declaration does not prevent calls like function(42);
. Use void
in the declaration like void function(void);
–
Coprology void function();
allows it as that is the style of early C where a declaration pretty much only told of a function name existence and return type. –
Coprology 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.
client.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:
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;
}
#ifndef
and #define
directives used in the source code for the library files (i.e., foo.c
in the above example) rather than in the header file for that library? Is it because the compiler knows it only has one actual definition for foo.c
? –
Propertied © 2022 - 2024 — McMap. All rights reserved.