In C programming, what is `undefined reference`error, when compiling?

When you link the program you need to give both Main.o and Person.o as inputs.

Build usually is done in two steps (1) compilation and (2) linking. To compile your sources do:

$ gcc -c -o Main.o Main.c
$ gcc -c -o Person.o Person.c

Or in one line:

$ gcc -c Main.c Person.c

Then the resulting object files must be linked into a single executable:

$ gcc -o Main Main.o Person.o

For small projects, of a few compilation units like yours, both step can be done in one gcc invocation:

$ gcc -o Main Main.c Person.c

both files must be given because some symbols in Person.c are used by Main.c.

For bigger projects, the two step process allows to compile only what changed during the generation of the executable. Usually this is done through a Makefile.

Leave a Comment