Boost.Filesystem 如何找出可执行文件在哪个目录?

发布于 2024-11-02 05:19:35 字数 65 浏览 0 评论 0 原文

所以我运行我的应用程序。我需要它知道它的可执行文件在哪里。如何使用 Boost.Filesystem 找到它的路径?

So I run my app. I need for it to know where its executable is. How to find path to it using Boost.Filesystem?

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

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

发布评论

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

评论(6

绾颜 2024-11-09 05:19:35
boost::filesystem::system_complete(argv[0]);

例如,

[davka@bagvapp Debug]$ ./boostfstest 
/home/davka/workspaces/v1.1-POC/boostfstest/Debug/boostfstest

请注意,这将为您提供完整路径包括可执行文件名。

boost::filesystem::system_complete(argv[0]);

e.g.

[davka@bagvapp Debug]$ ./boostfstest 
/home/davka/workspaces/v1.1-POC/boostfstest/Debug/boostfstest

Note that this gives you the full path including the executable file name.

隱形的亼 2024-11-09 05:19:35

你不能,Boost.Filesystem 不提供这样的功能。

但从 Boost 1.61 开始,您可以使用 Boost.Dll 和函数 boost::dll::program_location

#include <boost/dll.hpp>
boost::dll::program_location().parent_path();

You cannot, Boost.Filesystem does not provide such functionality.

But starting with Boost 1.61 you can use Boost.Dll and function boost::dll::program_location:

#include <boost/dll.hpp>
boost::dll::program_location().parent_path();
泪之魂 2024-11-09 05:19:35

您无法使用 boost::filesystem 可靠地完成此操作。

但是,如果您使用的是 Windows,则可以调用 GetModuleFileName 来获取可执行文件的完整路径,然后使用 boost::filesystem 获取目录。 (请参阅parent_path

You can't do it reliably with boost::filesystem.

However if you're on windows you can call GetModuleFileName to get the complete path of the executable and then use boost::filesystem to get the directory. ( see parent_path)

冷弦 2024-11-09 05:19:35

正如此处更全面地讨论的,最可靠的方法这不是通过 boost::filesystem 实现的。相反,您的实现应该考虑运行应用程序的操作系统。

但是,为了快速实现而不考虑可移植性,您可以检查 argv[0] 是否返回可执行文件的完整路径。如果是肯定的,您可以执行以下操作:

namespace fs=boost::filesystem;

fs::path selfpath=argv[0];

selfpath=selfpath.remove_filename();

As discussed more comprehensively here, the most reliable way to do that is not through boost::filesystem. Instead, your implementation should take into the consideration the operating system on which the application is running.

However, for a quick implementation without portability concerns, you can check if your argv[0] returns the complete path to executable. If positive, you can do something like:

namespace fs=boost::filesystem;

fs::path selfpath=argv[0];

selfpath=selfpath.remove_filename();
静待花开 2024-11-09 05:19:35

从 C++ 14 开始,您不需要 Boost,您可以使用标准库的文件系统,您可以轻松做到这一点:
(我可以确认这也适用于 Windows 和 Linux)

#include <iostream>
#include <filesystem>
namespace fs = std::experimental::filesystem;
int main()
{
    fs::path p = argv[0]; // or "C:executable_name.exe";
    std::cout << "Current path is " << fs::current_path() << '\n'
              << "Absolute path for " << p << " is " << fs::absolute(p) << '\n'
          << "System complete path for " << p << " is " << fs::system_complete(p) << '\n';
}

从文档复制的示例: https://en.cppreference.com/w/cpp/experimental/fs/absolute

From C++ 14 you don't need Boost, you can use the filesystem of the standard library you can do that easily:
(I can confirm this works on Windows and Linux as well)

#include <iostream>
#include <filesystem>
namespace fs = std::experimental::filesystem;
int main()
{
    fs::path p = argv[0]; // or "C:executable_name.exe";
    std::cout << "Current path is " << fs::current_path() << '\n'
              << "Absolute path for " << p << " is " << fs::absolute(p) << '\n'
          << "System complete path for " << p << " is " << fs::system_complete(p) << '\n';
}

Sample copied from the documentation: https://en.cppreference.com/w/cpp/experimental/fs/absolute

情仇皆在手 2024-11-09 05:19:35

如果您的意思是从正在运行的可执行文件内部,则可以使用 boost::filesystem::current_path()

If you mean from inside the executable that you're running, you can use boost::filesystem::current_path()

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