I'm a C Newb
Originally I was building all my source in one directory using a makefile, which has worked rather well. However, my project is growing and I would like to split the source into multiple directories (unit tests, utils, core, etc.). For example, my directory tree might look like the following:
. |-- src | |-- foo.c | |-- foo.h | `-- main.c `-- test `-- test_foo.c
test/test_foo.c uses both
src/foo.h. Using makefiles, what is the best/standard way to build this? Preferably, there would be one rule for building the project and one for building the tests.
I know that there are other ways of doing this, including autoconf and other automatic solutions. However, I would like to understand what is happening and be able to write the makefiles from scratch despite its possible impracticality.
Any guidance or tips would be appreciated. Thanks!
So the three solutions given so far are as follows:
- Place globally used header files in a parallel
- use the path in the
#includesatement as in
- use the
-Iswitch to inform the compiler of include locations
So far I like the
-I switch solution because it doesn't involve changing source code when directory structure changes.