It is hard to say what is wrong with your particular program, but here is an example which links 2 .c
files with one .h
file.
1. A header file functions.h
:
#include <stdio.h>
extern void func();
Where I use extern
to provide definitions for another file.
2. Now, a functions.c
file which uses this header file:
#include "functions.h"
void func() {
printf("hello");
}
This needs to #include
the header file, and use the function void()
to print a message.
3. Finally, a main.c
file which links it all together:
#include <stdio.h>
#include <stdlib.h>
#include "functions.h"
int main(void) {
func();
return 0;
}
Which also needs function.h
as it uses func()
. You then can compile the code as:
gcc -Wall -Wextra -g main.c functions.c -o main
You could also look into makefiles, which would reduce this long compilation line to simply make
.