Skip to main content
GetExecutablePath         Find path names from the location of the executable (Cross platform)

The following snippets of code find path names from the location of the executable. Use this on initialisation of your App. You can use these paths to define the location of ogre.cfg, plugins.cfg, log files, media files, etcetera.

Table of contents

Windows

Outputs:

  • execPath — The directory in which your executable resides. Like 'c:\ogreprojects\bunnyrpg\Debug\' if your executable is called 'c:\ogreprojects\bunnyrpg\Debug\BunnyProject.exe'
  • rootPath — The directory above the one in which your executable resides. Like 'c:\ogreprojects\bunnyrpg\'



Copy to clipboard
/// Find parent of executable directory TCHAR szPath[MAX_PATH]; std::string execPath; std::string rootPath; if(GetModuleFileName(hInstance, szPath, MAX_PATH)) { std::string path(szPath); size_t last = path.rfind('\\'); if(last != std::string::npos) { execPath = path.substr(0, last+1); last = path.rfind('\\', last-1); if(last != std::string::npos) rootPath = path.substr(0, last+1); } } if(rootPath.empty()) { MessageBox( NULL, "Could not locate root path from executable path", "Fatal error", MB_OK | MB_ICONERROR | MB_TASKMODAL); exit(0); }

MacOS


Copy to clipboard
#include <CoreFoundation/CoreFoundation.h> /// This function will locate the path to our application on OS X std::string MacBundlePath() { char path[1024]; CFBundleRef mainBundle = CFBundleGetMainBundle(); if(!mainBundle) return ""; CFURLRef mainBundleURL = CFBundleCopyBundleURL(mainBundle); if(!mainBundleURL) return ""; CFStringRef cfStringRef = CFURLCopyFileSystemPath(mainBundleURL, kCFURLPOSIXPathStyle); if(!cfStringRef) return ""; CFStringGetCString(cfStringRef, path, 1024, kCFStringEncodingASCII); CFRelease(mainBundleURL); CFRelease(cfStringRef); return std::string(path); }

Linux

On Linux, it is considered bad practice to put data or configuration files in a path relative to the location of the executable (see the Unix Programming Frequently Asked Questions for more information). If you want to distribute a Linux application, you'd be better advised to respect the FHS (Filesystem Hierarchy Standard). Here is a document which gives an overview on how to install games under Linux and how to put the files in the correct directories according to the FHS : Linux Games Install And Directory Guide

However, if you still need to find the executable path, here is a way of doing it in Linux:

Returns:

  • The directory in which your executable resides. Like '/usr/local/bin' if your executable is called '/usr/local/bin/myapp'



Copy to clipboard
char* self_exe_dir() { int len; static char path[1024]; char* slash; // Read symbolic link /proc/self/exe len = readlink("/proc/self/exe", path, sizeof(path)); if(len == -1) return NULL; path[len] = '\0'; // Get the directory in the path by stripping exe name slash = strrchr(path, '/'); if(! slash || slash == path) return NULL; *slash = '\0'; // trip slash and exe name return path; }