nina nina - 1 month ago 9
C Question

Loading texture won't work when providing relative path

I am running OS X 10.10.5. When I try to run (from the terminal) an OpenGL code that loads some textures, I get this error: "Cannot open file chair.bmp"
The error gets issued after executing this line

chairTexture = LoadTexBMP("chair.bmp");


providing a full path solves the problem but I need to make it work with a relative path.

The example code comes with a makefile:

# Example 9
EXE=ex9

# Main target
all: $(EXE)

# MinGW
ifeq "$(OS)" "Windows_NT"
CFLG=-O3 -Wall
LIBS=-lglut32cu -lglu32 -lopengl32
CLEAN=del *.exe *.o *.a
else
# OSX
ifeq "$(shell uname)" "Darwin"
CFLG=-O3 -Wall -Wno-deprecated-declarations
LIBS=-framework GLUT -framework OpenGL
# Linux/Unix/Solaris
else
CFLG=-O3 -Wall
LIBS=-lglut -lGLU -lGL -lm
endif
# OSX/Linux/Unix/Solaris
CLEAN=rm -f $(EXE) *.o *.a
endif

# Compile rules
.c.o:
gcc -c $(CFLG) $<
.cpp.o:
g++ -c $(CFLG) $<

# Link
ex9:ex9.o
gcc -O3 -o $@ $^ $(LIBS)

# Clean
clean:
$(CLEAN)


running
pwd
in the terminal yields
/Users/nina/CG/ex9


All texture images are in ex9 folder along with ex9.c and makefile.
I tried to find a solution but all the solutions I found talks about changing the working directory in Xcode which I am not using.

Answer

I finally figured what I was doing wrong. I was using open ex9 in the terminal which behaves as if I was clicking on the executable which in turns changes the working directory into the home directory. So, in order not to change the working directory I should use ./ex9

Comments