I am writing C code (not C++) on a Linux system. I am trying to compile one executable from 2 .c files and 1 header file: main.c, sort.c, and sort.h.
main.c's first line reads: #include "sort.h"
inside sort.h, each function in sort.c is defined like this example:
extern void aFunct(int param);
However, when I try to call a function in sort.c from main.c, I get the following error upon compilation: "undefined reference to 'aFunct'".
If i replace #include "sort.h" with #include "sort.c" my program works without issue. However, as I understand it, this is bad form and I would prefer to avoid this. Thanks for any help.
edit: I am compiling this with a makefile containing the following code:
all: index sort.o
sort.o: sort.c sort.h
gcc -Wall -g -c sort.c
index: main.c sort.o
gcc -Wall -g -o index main.c
clean:
rm index
rm sort.o
edit: I have fixed the problem. The problem did not stem from a misunderstanding of C files and how they link, but rather a misunderstanding of the makefile/gcc commands themself. My program works with the following makefile:
all: index sort.o
sort.o: sort.c
gcc -Wall -g -c sort.c
index: main.c sort.o
gcc -Wall -g -o index main.c sort.o
clean:
rm sort.o
rm index
sort.ointo your executable?