“应用程序包”在哪里?正在寻找我的媒体文件夹?从 Qt Creator 运行应用程序时发现文件夹,“找不到文件”使用终端时

发布于 2024-12-07 21:12:12 字数 753 浏览 3 评论 0原文

我在让我的应用程序包从终端工作或双击它时遇到了一些麻烦。

该应用程序实际上可以在 Qt Creator IDE 中完美编译、链接和运行。但是,如果我尝试从终端打开它,我会收到“media/file.x 文件未找到”错误。应用程序包和 /Contents/MacOS/executable 都找不到应该位于可执行文件旁边的“media”文件夹。

在我的应用程序中,我做了类似的事情:

openFile("media/file.x");

在 Windows 和 Linux 中,如果“media”文件夹与可执行文件完全相同的层次结构位置(在它旁边),则将找到该文件。在 Mac 上,我发现它的工作方式有所不同,因为 Qt Creator 构建了一个“App Bundle”,而实际的可执行文件位于 /Contents/MacOS 文件夹内,因此我在那里手动复制了“media”。当从 Qt Creator 中“播放”我的应用程序时,这没有任何麻烦,但正如前面提到的,在运行捆绑包本身时它不起作用。

那么有谁知道在哪里或如何均匀化这个“媒体”文件夹的外观,以便它适用于 Qt Creator 和应用程序包?

最近,我一直在使用以下命令来“安装”捆绑包上的文件夹。

mac {
    MediaFiles.files = media
    MediaFiles.path = Contents/MacOS
    QMAKE_BUNDLE_DATA += MediaFiles
}

感谢您的帮助。

I'm having a little trouble making my app bundle work from the terminal or just double clicking it.

This App actually compiles, links and runs perfectly within the Qt Creator IDE. But, if I try to open it from the terminal I get a "media/file.x file not found" error. The App bundle nor the /Contents/MacOS/executable is finding the "media" folder that is supposed to be beside the executable.

In my app I do something like:

openFile("media/file.x");

In Windows and Linux, this file WILL be found if the "media" folder is exactly in the same hierarchical position of the executable (beside it). On the Mac I have discovered it works differently cause Qt Creator builds an "App Bundle" and the actual executable is inside the /Contents/MacOS folder, so I copied the "media" manually there. This worked without any hassle when "playing" my app from the Qt Creator but as mentioned before it doesn't work when running the bundle itself.

So does anyone know where or how can I homogenize the look for this "media" folder so it works on both: Qt Creator and the App bundle?

Lately, I have been using the following command to "install" the folder on the bundle.

mac {
    MediaFiles.files = media
    MediaFiles.path = Contents/MacOS
    QMAKE_BUNDLE_DATA += MediaFiles
}

Thanks for your help.

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

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

发布评论

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

评论(1

绾颜 2024-12-14 21:12:12

经过几天的寻找,我发现了一些讨论相对路径问题的帖子。我只是没有用正确的词进行搜索...答案显示在:

相对路径在 Xcode C++ 中不起作用

http://www.experimentgarden.com/2009/06/how-to-load-resource-from-your.html

基本上有必要添加以下内容:

//On the include part
#ifdef __APPLE__
#include "CoreFoundation/CoreFoundation.h"
#endif


// On the main of your app
// This makes relative paths work in C++ in Xcode by changing directory to the Resources         folder inside the .app bundle
#ifdef __APPLE__    
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
    char path[PATH_MAX];
    if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX))
    {
        // error!
    }
    CFRelease(resourcesURL);

    chdir(path);
    std::cout << "Current Path: " << path << std::endl;
#endif
// ----------------------------------------------------------------------------

重要:
chdir,更改应用程序的工作路径,因此之后无需更改相关代码...

After looking for a few days I found a couple of posts discussing the relative path problem. I was just not searching with the right words... The answer is displayed in:

Relative Paths Not Working in Xcode C++

http://www.experimentgarden.com/2009/06/how-to-load-resource-from-your.html

Basically it is necessary to add this:

//On the include part
#ifdef __APPLE__
#include "CoreFoundation/CoreFoundation.h"
#endif


// On the main of your app
// This makes relative paths work in C++ in Xcode by changing directory to the Resources         folder inside the .app bundle
#ifdef __APPLE__    
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
    char path[PATH_MAX];
    if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX))
    {
        // error!
    }
    CFRelease(resourcesURL);

    chdir(path);
    std::cout << "Current Path: " << path << std::endl;
#endif
// ----------------------------------------------------------------------------

IMPORTANT:
The chdir, changes the working path of the app, so it is not necessary to change you relative code after that...

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