Reputation: 166
I am trying to open some openGL shaders on my C++ application. My application is separated on two abstraction layers: the graphics engine (which uses openGL) and the actual application logic. I want the graphics engine to be as recyclable as possible[1] (for other projects).
The problem is that if I define the shader file's path relatively (for instance #define FOO_FRAG_SHADER "shaders/foo.frag"
) the program is not able to find it, as this directory is relative to the graphics engine's path and not the application's path. For the reason commented above [1], I really don't want to specify it's directory relative to the application's dir, as it becomes application dependent.
Therefore, my question is: Is there anyway to specify a directory relative to a file which specifies it?
Upvotes: 1
Views: 503
Reputation: 726659
Relative paths are resolved with respect to the working directory of the process, which is not necessarily the location of the executable itself. For example, if you start your program like this
/user/oier/bin/> ./a.out
then relative paths are resolved in relation to /user/oier/bin
. However, if you start it like this
/user/oier/> bin/a.out
then relative paths are resolved in relation to /user/oier
.
There are two common approaches to solving this problem without hard-coding the location of your files:
If the files in question are small, and there is a possibility that each application might want to supply a private copy of it, a third approach is to copy the files in question to a folder under your application's bin
, and use a relative reference based on the location of the copy.
Upvotes: 2