1

I am creating a C++ SDL game engine, and it is relevant to know the executable path since images and other resources are not stored within the executable - they are in a separated folder("res/").

Under Linux, I am using a shell script "rungame.sh" that cd's to the executable path and then runs the executable(using then "./" to reference the executable folder). However, I believe this is an "ugly" approach and I want it to be Windows-compatible.

Also, the current approach is not very good as it may change the meaning of some command line arguments.

I want a cross-platform(*NIX, Windows and Mac OS X, if possible) solution to get the current path of the executable. The game path/executable name may change.

What is the cleanest way to solve my problem (preferrably using std::string and as few platform-dependent APIs as possible)?

luiscubal
  • 24,773
  • 9
  • 57
  • 83

3 Answers3

6

Since SDL 2.0.1 there's SDL_GetBasePath.

felipecrv
  • 1,780
  • 2
  • 15
  • 14
1

I strongly suggest searching before posting: How do I get the directory that a program is running from?

Community
  • 1
  • 1
Jacek Ławrynowicz
  • 2,670
  • 2
  • 23
  • 23
  • 1
    Actually, it is not the solution I needed. That is about getting the current directory path. I want the EXECUTABLE directory path. If I run 'cd /something/' then run './x/y', that will output '/something/' and I want it to output '/something/x/'. – luiscubal Jan 26 '09 at 19:42
  • It turns out it had that answer too. However, it wasn't the question so I guess I couldn't discover it using StackOverflow's search system. – luiscubal Jan 26 '09 at 21:21
0

physicsfs has PHYSFS_getBaseDir():

Get the path where the application resides.

Helper function.

Get the "base dir". This is the directory where the application was run from, which is probably the installation directory, and may or may not be the process's current working directory.

You should probably use the base dir in your search path.

genpfault
  • 51,148
  • 11
  • 85
  • 139