I’ve got a project which has two source folders (main and lib). It produces a shared library and an executable. It is currently built as so:
- copy all files from both folders into a new temp folder
- run lib_makefile
- run main_makefile
- copy binaries out
- delete temp folder
This struck me as being a weird way to do things, so I tried building each in-place by adding -I../main to lib_makefile (and vice-versa). Unfortunately, this doesn’t seem to work.
Illustrative example:
foo.cpp (in lib) includes bar.h (in main), which includes baz.h (back in lib).
When I try to compile the shared lib, it correctly locates bar.h in main/, but then bails out with “no such file or directory” claiming it cannot find baz.h, even though baz.h is in the same directory as lib_makefile!
All includes are in the format #include “xxx.h” (i.e no relative paths in the include statements).
Is there a way to get this to work? I feel like I must be missing something obvious..
(nb: I can’t modify the #includes because other people still build this the copy-everything-across way)
You should add something like
-I../lib(or whatever your library path is) to the makefile for the library as well.The reason for this is that the pre-processor looks for include-files relative to the directory the current file is in, not from where the original file is in.