nina nina - 10 months ago 54
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

# Main target
all: $(EXE)

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

# Compile rules
gcc -c $(CFLG) $<
g++ -c $(CFLG) $<

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

# Clean

in the terminal yields

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 Source

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