捆绑到 AppImage 时如何访问 C 中的资源?

问题描述 投票:0回答:1

我有一个 C 程序,我想将其打包到 AppImage 文件中。我正在尝试使用 linuxdeployappimagetool 为我的程序创建 AppImage。
到目前为止它已经工作了,但是我不知道如何将我的C程序所需的资源文件放入AppImage中,也不知道使用什么代码来加载文件。

目前,我用来访问资源的代码是这样的:

#include <stdio.h>
int main() {
  FILE *file = fopen("resource.txt", "r"); //resource file is in the same directory as program
  //more code to read file
  fclose(file);

  return 0;
}

我使用以下命令将程序打包到 AppImage 中:

gcc program.c -o myprogram
./linuxdeploy-x86_64.AppImage --executable myprogram --desktop-file mydesktopfile --icon icon.png --appdir MyProgram.AppDir
ARCH=x86_64 ./appimagetool-x86_64.AppImage MyProgram.AppDir

其他一些相关信息:

  • 我使用的是 Debian 12 linux
  • 我的电脑架构是x86_64
  • 所有程序文件都位于同一目录中,彼此相邻放置
  • 我已经静态编译了程序,因此不需要包含库

我应该如何打包我的程序以便它仍然可以访问资源文件,以及我应该如何修改我的代码来读取资源文件?

c file-io appimage appimage-builder
1个回答
0
投票

我找到了一个对我有用的方法。
在创建的 AppDir 中,我在

resources
内添加了一个文件夹
usr/share
,并将我的资源文件放入其中。 然后我将代码更改为:

#include <stdio.h>
int main() {
  FILE *file = fopen("resources/resource.txt", "r");
  //more code to read file
  fclose(file);

  return 0;
}

这是对我有用的方法,但如果有人认为这不是一个好方法,请随时发表评论。
快速说明一下,经过一番研究,我发现不可能在 AppImage 内编辑文件,这是设计使然。 (将 AppImage 视为可执行的 .ZIP 文件)因此请确保您只是打开文件来读取它。

© www.soinside.com 2019 - 2024. All rights reserved.