Suppose you have an existing program consisting of main.c
, foo.c
, foo.h
and a makefile that builds the program. And suppose you want to add bar.c
and bar.h
.
Anything that needs bar code must tell the compiler where to find it. Otherwise if the Foo
class has a Bar
member, and main()
calls bar_transform()
, the compiler will scream that it doesn't know what you're talking about. So foo.h
and main.c
must each contain this line:
#include "bar.h"
(The pattern here is a little subtle. Notice that foo.c
probably does not need this directive, because it has #include "foo.h"
, and foo.h
has #include "bar.h"
. Also, there are cases in which it would be better to use forward declaration int foo.h
and put the directive in foo.c
instead. If you have trouble with this, we can help.)
Now the code is correct. We can compile it and produce main.o
, foo.o
and bar.o
. We just have to link them correctly.
The makefile must contain something that tells Make what files to use when building the executable, perhaps like this:
app: main.o foo.o
$(CC) $^ -o $@
If you try this after modifying the source files, Make will compile main.o
and foo.o
correctly, then try to link them, and the linker will scream that the Bar
class is incomplete, and it can find no definition of bar_transform()
.
To incorporate bar
in the build, you need only add bar.o
to that list of prerequisites:
app: main.o foo.o bar.o
...
There are other ways the makefile could specify the list, so if you have trouble, show us your makefile. Also, this doesn't address the problem of dependency handling (e.g. knowing that foo.o
must be rebuilt if bar.h
has changed); this is probably very easy to fix, but we have to see the makefile.