1

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?

oierlauzi
  • 167
  • 2
  • 7
  • 3
    ***Is there anyway to specify a directory relative to a file which specifies it rather than to the executable?*** It should have no relation to the executable. – drescherjm Dec 14 '18 at 19:23
  • 2
    Relative directory paths are relative to the programs working directory from where you running it, not where the executable actually resides. – πάντα ῥεῖ Dec 14 '18 at 19:26

1 Answers1

2

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:

  • Pass a command line parameter specifying the base path for the graphics engine, or
  • Use an environment variable to specify the location of the graphics engine

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Let's see if I have understood your solution correctly (the second one looks reasonable). I've looked how ENVIRONMENT VARIABLES work in C++. So Before executing the code I need to set a environment variable indicating the path of the shaders. For example: `user@mySystem:$ FOO_APP_SHADERS=whatever/someotherdir/shaders` then I need to query it on my graphics engine with `getenv()`, so that by adding "/foo.frag" at the end of it I can access the file – oierlauzi Dec 14 '18 at 19:49
  • You may want to have a fallback default path if the environment variable is not set. – drescherjm Dec 14 '18 at 20:10
  • @oierlauzi Right, that's the approach. Most systems let you define environment variables as part of some sort of per-user/per-system start-up script, so you would have to configure `FOO_APP_SHADERS` once during the installation of your library. – Sergey Kalinichenko Dec 14 '18 at 20:17