C++以编程方式查找可执行根目录的便携式方法

发布于 2024-08-22 10:59:49 字数 863 浏览 4 评论 0原文

可能的重复:
如何在 C 中查找可执行文件的位置

嗨,

我正在寻找一种可移植的方法来查找程序的根目录(在C++中)。例如,在 Linux 下,用户可以将代码复制到 /opt,将其添加到 PATH,然后执行它:(

cp -r my_special_code /opt/
export PATH=${PATH}:/opt/my_special_code/
cd /home/tony/
execution_of_my_special_code

其中“execute_my_special_code”是 /opt/my_special_code 中的程序)。

现在,作为“execution_of_my_special_code”的开发人员,是否有一种可移植的编程方式来查找可执行文件位于 /opt/my_special_code 中?

第二个例子是在 MS Windows 上:如果我当前的工作目录位于一个硬盘驱动器上(例如“C:\”),而可执行文件位于另一个硬盘驱动器上(例如“D:\”),该怎么办?

最终的目标是读取一些与程序代码打包在一起的预定义配置文件,而不强制用户进入安装目录。

预先非常感谢!

Possible Duplicate:
how to find the location of the executable in C

Hi,

I am looking for a portable way to find the root directory of a program (in C++). For instance, under Linux, a user could copy code to /opt, add it to the PATH, then execute it:

cp -r my_special_code /opt/
export PATH=${PATH}:/opt/my_special_code/
cd /home/tony/
execution_of_my_special_code

(where "execute_my_special_code" is the program in /opt/my_special_code).

Now, as a developer of "execution_of_my_special_code", is there a portable programmatical way of finding out that the executable is in /opt/my_special_code?

A second example is on MS Windows: What if my current working directory is on one hard drive (e.g. "C:\") and the executable is placed on another (e.g. "D:\")?

Ultimately, the goal is to read some predefined configuration files that are packaged with the program code without forcing the user to enter the installation directory.

Thanks a lot in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星光不落少年眉 2024-08-29 10:59:49

没有可移植的方法可以做到这一点。 C 和 C++ 标准都不需要 argv 数组来保存此信息,并且标准库中没有提供此信息的库例程。

您需要找到一种非可移植的方式来以特定于平台的方式获取此信息,例如 Windows 下的 GetModuleFileName 或 Linux 下的读取 /proc/self/exe

无论如何,将配置信息与可执行文件一起存储实际上是一个坏主意,因为这意味着它对于所有用户都是相同的。 UNIX 执行此操作的方法是将特定于程序的信息存储在 /etc 树中,并将特定于用户的信息存储在用户的主目录中。例如,对于名为 dodgybob 的应用程序,用户的配置文件可以称为 $HOME/.dodgybob$HOME/.dodgybobrc

对于 Windows,您应该将特定于程序的信息存储在注册表 (HKLM) 中,并将特定于用户的信息存储在注册表 (HKCU) 或用户自己的区域中(例如,Documents & Settings\) \Local Settings 虽然正确的方法是使用 SHGetSpecialFolderPath 传递 CSIDL_APPDATA 或(Vista 及更高版本)已知文件夹 ID,但旧方法仍然有效在 Vista 上作为存根函数)。

There is no portable way of doing this. Neither the C nor C++ standard require the argv array to hold this information and there are no library routines in the standard libraries that provide it.

You will need to find a non-portable way of obtaining this information in a platform-specific way, such as GetModuleFileName under Windows or reading /proc/self/exe under Linux.

In any case, it's actually a bad idea to store configuration information with the executable since that means it's the same for all users. The UNIX way of doing this is to store program-specific information in the /etc tree and user-specific info in the user's home directory. For example, for your application called dodgybob, the user's configuration file could be called $HOME/.dodgybob or $HOME/.dodgybobrc.

For Windows, you should be storing program-specific information in the registry (HKLM) and user-specific information either in the registry (HKCU) or in the user's own areas (e.g., Documents & Settings\<username>\Local Settings although the right way to do that is with SHGetSpecialFolderPath passing CSIDL_APPDATA or (Vista and later) with known folder IDs, though the old method still works on Vista as a stub function).

最佳男配角 2024-08-29 10:59:49

没有用于此目的的 ISO C++ 代码,但您可以进行条件编译并使用 _getcwd (Windows) 或 getcwd (Linux):

#define GetCurrentDir _getcwd
std::string SettingsHandler::getFullPath() {
    char chPath[2048];

    if (!GetCurrentDir(chPath, sizeof(chPath))) {
        return "ERROR";
    }

    chPath[sizeof(chPath) - 1] = '/0';

    return chPath;
}

由于您想要进行跨平台编程,您应该将设置/配置存储在 xml 文件中。这样您就不必创建两个解析器(一个用于注册表,另一个用于 Linux 解决方案),并且您不必处理不同的 Windows 版本!

There is no ISO c++ code for this but you could do a conditional compiling and use _getcwd (Windows) or getcwd (Linux):

#define GetCurrentDir _getcwd
std::string SettingsHandler::getFullPath() {
    char chPath[2048];

    if (!GetCurrentDir(chPath, sizeof(chPath))) {
        return "ERROR";
    }

    chPath[sizeof(chPath) - 1] = '/0';

    return chPath;
}

Since you want to do cross-platform-programming you should store your settings/configurations in a xml file. This way you don't have to create two parser (one for the registry and another one for a linux solution) and you don't have to deal with different windows versions!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文