Using the simple scenario of a folder structure with a project folder where in this root there is a build and a sources folder:
project
- build
- sources
In the sources folder there is hello.cpp:
#include <iostream>
using namespace std;
int main() {
cout << "Hello world!\n";
}
and a Makefile:
SOURCE_DIRECTORIES = ../ch1
vpath %.cpp $(SOURCE_DIRECTORIES)
hello: hello.cpp
g++ ${SOURCE_DIRECTORIES}/hello.cpp -o hello
I run like this from build folder:
make -f ../sources/Makefile
The above works but the vpath isn't so useful. Even with vpath I have to add the SOURCE_DIRECTORIES path the the command line. I might as well not use vpath and just use $(SOURCE_DIRECTORIES) where required.
It is also a little brittle in that the build folder has to be at the same level as the sources folder. But I can live with that.
Is this the best approach? I am thinking there must be a better way to do this.