在 GNU/Linux 系统上,我应该从哪里加载应用程序数据?

发布于 2024-07-23 06:22:09 字数 380 浏览 13 评论 0原文

在本例中,我将 c 与 autoconf 一起使用,但问题也适用于其他地方。

我有一个运行时需要的空地 xml 文件,我必须告诉应用程序它在哪里。 我使用 autoconf 在代码中定义一个变量,该变量指向“指定的前缀目录”/app-name/glade。 但这只有在安装应用程序后才开始起作用。 如果我想在那之前运行该程序怎么办? 是否有标准方法来确定应检查哪些路径的应用程序数据?

感谢


感谢您的回复。 澄清一下,我不需要知道应用程序数据的安装位置(例如通过在 /usr、usr/local 等中搜索),配置脚本可以做到这一点。 问题更多的是确定应用程序是否已经安装。 我想我会先检查安装位置,如果没有,则检查“./src/foo.glade”。

In this instance I'm using c with autoconf, but the question applies elsewhere.

I have a glade xml file that is needed at runtime, and I have to tell the application where it is. I'm using autoconf to define a variable in my code that points to the "specified prefix directory"/app-name/glade. But that only begins to work once the application is installed. What if I want to run the program before that point? Is there a standard way to determine what paths should be checked for application data?

Thanks


Thanks for the responses. To clarify, I don't need to know where the app data is installed (eg by searching in /usr,usr/local,etc etc), the configure script does that. The problem was more determining whether the app has been installed yet. I guess I'll just check in install location first, and if not then in "./src/foo.glade".

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

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

发布评论

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

评论(4

我只土不豪 2024-07-30 06:22:09

我认为没有任何标准方法可以找到此类数据。

我个人会这样做,我有一个路径列表,并且我会找到是否可以从其中任何一个找到该文件,并且该列表应包含从 autoconf 和 CURRENTDIRECTORY+POSSIBLE_PREFIX 定义的 DATADIR+APPNAME,其中前缀可能是构建根目录中的某个文件夹。

但无论如何,不​​要忘记对您的数据文件使用 autoconf 中的那些定义,它们使您的软件更容易打包(例如 deb/rpm)

I dont think there's any standard way on how to locate such data.

I'd personally do it in a way that i'd have a list of paths and i'd locate if i can find the file from anyone of those and the list should containt the DATADIR+APPNAME defined from autoconf and CURRENTDIRECTORY+POSSIBLE_PREFIX where prefix might be some folder from your build root.

But in any case, dont forget to use those defines from autoconf for your data files, those make your software easier to package (like deb/rpm)

撩人痒 2024-07-30 06:22:09

一般来说,没有规定如何完成此操作,但 Debian 打包程序通常将应用程序数据安装在 /usr/share、/usr/lib 等位置。 他们还可能修补软件以使其从适当的位置读取。 您可以查看 Debian 政策 了解更多信息。

不过我可以简单说几句我是怎么做的。 首先,我不希望在单个目录中找到该文件; 我首先创建一个目录列表,在围绕 fopen() 的包装器中迭代该列表。 这是我认为应该完成文件读取的顺序:

  • 当前目录(显然)
  • ~/.program-name
  • $(datadir)/program-name

$(datadir) 是一个可以在 Makefile.am 中使用的变量。 示例:

AM_CPPFLAGS = $(ASSERT_FLAGS) $(DEBUG_FLAGS) $(SDLGFX_FLAGS) $(OPENGL_FLAGS) -DDESTDIRS=\"$(prefix):$(datadir)/:$(datadir)/program-name/\"

这当然取决于 configure 的输出以及 configure.ac 的外观。

因此,只需创建一个包装器,它将迭代这些位置并从这些目录中获取数据。 类似于 PATH 变量,只不过您实现了迭代。

写完这篇文章后,我注意到我需要清理 这个项目中的实现,但它可以作为一个好的开始。 查看使用 $(datadir)Makefile.am 以及 util.cpputil.h > 一个简单的包装器 (yatc_fopen())。 我们还有 yatc_find_file(),以防某些第三方库执行 fopen(),例如 SDL_image 或 libxml2。

There is no prescription how this should be done in general, but Debian packagers usually installs the application data somewhere in /usr/share, /usr/lib, et cetera. They may also patch the software to make it read from appropriate locations. You can see the Debian policy for more information.

I can however say a few words how I do it. First, I don't expect to find the file in a single directory; I first create a list of directories that I iterate through in my wrapper around fopen(). This is the order in which I believe the file reading should be done:

  • current directory (obviously)
  • ~/.program-name
  • $(datadir)/program-name

$(datadir) is a variable you can use in Makefile.am. Example:

AM_CPPFLAGS = $(ASSERT_FLAGS) $(DEBUG_FLAGS) $(SDLGFX_FLAGS) $(OPENGL_FLAGS) -DDESTDIRS=\"$(prefix):$(datadir)/:$(datadir)/program-name/\"

This of course depends on your output from configure and how your configure.ac looks like.

So, just make a wrapper that will iterate through the locations and get the data from those dirs. Something like a PATH variable, except you implement the iteration.

After writing this post, I noticed I need to clean up our implementation in this project, but it can serve as a nice start. Take a look at our Makefile.am for using $(datadir) and our util.cpp and util.h for a simple wrapper (yatc_fopen()). We also have yatc_find_file() in case some third-party library is doing the fopen()ing, such as SDL_image or libxml2.

你的他你的她 2024-07-30 06:22:09

如果程序是全局安装的:

/usr/share/app-name/glade.xml

如果您希望程序在不安装的情况下工作(即仅提取 tarball),请将其放在程序的目录中。

If the program is installed globally:

/usr/share/app-name/glade.xml

If you want the program to work without being installed (i.e. just extract a tarball), put it in the program's directory.

何止钟意 2024-07-30 06:22:09

我认为没有放置文件的标准方法。 我将其构建到程序中,并且不将其限制在一个位置。

这取决于需要对配置文件进行多少自定义。

我首先构建一个默认目录列表,然后处理它们,直到找到glade.xml 的实例并停止查找,或者找不到它并退出并出现错误。 默认列表的最佳候选者是 /etc、/usr/share/app-name、/usr/local/etc。

如果文件设计为可自定义,则在查看默认目录之前,我会获得用户文件和路径的列表并对其进行处理。 如果它找不到用户版本之一,那么我会查看默认目录列表。 用户配置文件的良好候选者是 ~/.glade.xml 或 ~/.app-name/glade.xml 或 ~/.app-name/.glade.xml。

I don't think there is a standard way of placing files. I build it into the program, and I don't limit it to one location.

It depends on how much customising of the config file is going to be required.

I start by constructing a list of default directories and work through them until I find an instance of glade.xml and stop looking, or not find it and exit with an error. Good candidates for the default list are /etc, /usr/share/app-name, /usr/local/etc.

If the file is designed to be customizable, before I look through the default directories, I have a list of user files and paths and work through them. If it doesn't find one of the user versions, then I look in the list of default directories. Good candidates for the user config files are ~/.glade.xml or ~/.app-name/glade.xml or ~/.app-name/.glade.xml.

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