Execute a program opening a file in parent directory from parent directory in C++

StackOverflow https://stackoverflow.com/questions/23421121

  •  13-07-2023
  •  | 
  •  

Domanda

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.

È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top