Question

My project structure looks like this:

├── Makefile
├── data
├── src
│   ├── ...other code
│   └── main.cpp
└── bin
    └── out

The program will look into the parent directory and open a file put in there, the code opening the file looks like:

f.open("../data");

and it will generate some temporary files in bin(where the binary goes) with code like this:

f.open("temp");

My makefile looks like this:

SOURCES=src/main.cpp src/foo.cpp
OBJECTS=$(SOURCES:.cpp=.o)

EXECUTABLE=bin/out

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
    $(CC) $(OBJECTS) -o $@

.cpp.o:
    $(CC) $(CFLAGS) $< -o $@

clean:
    rm src/*.o $(EXECUTABLE)

test:
    bin/out arg1 arg2

load:
    bin/out arg1 arg2

.PHONY: test load

Now, I can make from the parent directory, then go into the bin, execute the program. But the program cannot open the data in the parent directory if I just run make load or make test from the parent directory.

Was it helpful?

Solution

You can put cd on a make recipe command :

test:
     cd bin; ./out arg1 arg2

Don't forget to cd in the same line where you are executing

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top