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++结构和函数声明。为什么它不能编译? 2022-11-07
- 常量变量在标题中不起作用 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01