OGRE Wiki
Support and community documentation for Ogre3D
Ogre Forums
ogre3d.org
Log in
Username:
Password:
CapsLock is on.
Remember me (for 1 year)
Log in
Home
Tutorials
Tutorials Home
Basic Tutorials
Intermediate Tutorials
Mad Marx Tutorials
In Depth Tutorials
Older Tutorials
External Tutorials
Cookbook
Cookbook Home
CodeBank
Snippets
Experiences
Ogre Articles
Libraries
Libraries Home
Alternative Languages
Assembling A Toolset
Development Tools
OGRE Libraries
List of Libraries
Tools
Tools Home
DCC Tools
DCC Tutorials
DCC Articles
DCC Resources
Assembling a production pipeline
Development
Development Home
Roadmap
Building Ogre
Installing the Ogre SDK
Setting Up An Application
Ogre Wiki Tutorial Framework
Frequently Asked Questions
Google Summer Of Code
Help Requested
Ogre Core Articles
Community
Community Home
Projects Using Ogre
Recommended Reading
Contractors
Wiki
Immediate Wiki Tasklist
Wiki Ideas
Wiki Guidelines
Article Writing Guidelines
Wiki Styles
Wiki Page Tracker
Ogre Wiki Help
Ogre Wiki Help Overview
Help - Basic Syntax
Help - Images
Help - Pages and Structures
Help - Wiki Plugins
Toolbox
Freetags
Categories
List Pages
Structures
Trackers
Statistics
Rankings
List Galleries
Ogre Lexicon
Comments
History: GetExecutablePath
View page
Source of version: 3
(current)
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. {maketoc} !!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\' {CODE(wrap="1", colors="c++")} /// 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); }{CODE} !!MacOS {CODE(wrap="1", colors="c++")}#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); }{CODE} !!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 [http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC23|Unix Programming Frequently Asked Questions] for more information). If you want to distribute a Linux application, you'd be better advised to respect the [http://www.pathname.com/fhs/|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 : [http://pingus.seul.org/~grumbel/tutorials/game_install/install_dirs.html|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' {CODE(wrap="1", colors="c++")}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; }{CODE}
Search by Tags
Search Wiki by Freetags
Latest Changes
Minimal Ogre Collision
Artifex Terra
OpenMB
Advanced Mogre Framework
MogreSocks
Critter AI
Mogre Add-ons
MOGRE
Mogre MyGUI wrapper
MOGRE Editable Terrain Manager
...more
Search
Find
Advanced
Search Help
Online Users
42 online users