Get path of executable(获取可执行文件路径)
问题描述
我知道以前有人问过这个问题,但我仍然没有看到满意的答案,或者明确的不,这不能完成",所以我再问一次!
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一个>
这篇关于获取可执行文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:获取可执行文件路径
基础教程推荐
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- C++,'if' 表达式中的变量声明 2021-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01