获取可执行文件路径
我知道以前有人问过这个问题,但我仍然没有看到满意的答案,或者明确的不,这不能完成",所以我再问一次!
I know this question has been asked before but I still haven't seen a satisfactory answer, or a definitive "no, this cannot be done", so I'll ask again!
我想要做的就是以独立于平台的方式获取当前运行的可执行文件的路径,可以是绝对路径,也可以是相对于调用可执行文件的位置.我虽然 boost::filesystem::initial_path 是我的麻烦的答案,但这似乎只处理问题的平台无关"部分 - 它仍然返回调用应用程序的路径.
All I want to do is get the path to the currently running executable, either as an absolute path or relative to where the executable is invoked from, in a platform-independent fashion. I though boost::filesystem::initial_path was the answer to my troubles but that seems to only handle the 'platform-independent' part of the question - it still returns the path from which the application was invoked.
对于一些背景知识,这是一个使用 Ogre 的游戏,我正在尝试使用 Very Sleepy 对其进行分析,它从自己的目录运行目标可执行文件,所以当然在加载游戏时找不到配置文件等.并立即崩溃.我希望能够将其传递给配置文件的绝对路径,我知道该配置文件将始终与可执行文件一起存在.在 Visual Studio 中进行调试也是如此――我希望能够运行 $(TargetPath) 而无需设置工作目录.
For a bit of background, this is a game using Ogre, which I'm trying to profile using Very Sleepy, which runs the target executable from its own directory, so of course on load the game finds no configuration files etc. and promptly crashes. I want to be able to pass it an absolute path to the configuration files, which I know will always live alongside the executable. The same goes for debugging in Visual Studio - I'd like to be able to run $(TargetPath) without having to set the working directory.
推荐答案
我知道没有跨平台的方式.
There is no cross platform way that I know.
对于 Linux:readlink/proc/self/exe
For Linux: readlink /proc/self/exe
Windows:GetModuleFileName一个>
相关文章