1

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)

2
  • Have you tried also adding something like -I../lib to the library makefile too? Aug 28, 2012 at 8:44
  • 1
    It does include 'this directory' but when processing a file in ../main that is 'this directory', not the one you ran make from Aug 28, 2012 at 8:58

1 Answer 1

5

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.

1
  • 1
    Or simply -I. since that's the same as -I../lib when in the lib dir Aug 28, 2012 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.