简体   繁体   English

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

[英]On GNU/Linux systems, Where should I load application data from?

In this instance I'm using c with autoconf, but the question applies elsewhere. 在这个例子中,我使用c和autoconf,但问题适用于其他地方。

I have a glade xml file that is needed at runtime, and I have to tell the application where it is. 我有一个在运行时需要的glade xml文件,我必须告诉应用程序它在哪里。 I'm using autoconf to define a variable in my code that points to the "specified prefix directory"/app-name/glade. 我正在使用autoconf在我的代码中定义一个指向“指定前缀目录”/ 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. 为了澄清,我不需要知道应用程序数据的安装位置(例如,通过在/ usr,usr / local等中搜索),configure脚本会这样做。 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". 我想我会首先检查安装位置,如果不是,则在“./src/foo.glade”中检查。

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. 我个人这样做的方式是我有一个路径列表,如果我可以找到任何人的文件,我会找到该列表,并且该列表应该包含从autoconf和CURRENTDIRECTORY + POSSIBLE_PREFIX定义的DATADIR + APPNAME前缀可能是构建根目录中的某个文件夹。

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) 但无论如何,不​​要忘记使用autoconf的那些定义来处理你的数据文件,那些让你的软件更容易打包(比如deb / rpm)

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. 一般来说,这不应该是如何处理的,但是Debian打包器通常会在/ usr / share,/ usr / lib等等的某处安装应用程序数据。 They may also patch the software to make it read from appropriate locations. 他们还可以修补软件,使其从适当的位置读取。 You can see the Debian policy for more information. 您可以查看Debian策略以获取更多信息。

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() . 我首先创建一个目录列表,我在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. $(datadir)是一个可以在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. 这当然取决于configure的输出以及configure.ac外观。

So, just make a wrapper that will iterate through the locations and get the data from those dirs. 因此,只需创建一个包装器,它将迭代这些位置并从这些dirs获取数据。 Something like a PATH variable, except you implement the iteration. 类似于PATH变量的东西,除了你实现迭代。

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() ). 看看我们的Makefile.am使用$(datadir)util.cpputil.h来获得一个简单的包装器( 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. 如果某些第三方库正在执行fopen() ,我们也有yatc_find_file() ,例如SDL_image或libxml2。

If the program is installed globally: 如果程序是全局安装的:

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

If you want the program to work without being installed (ie just extract a tarball), put it in the program's directory. 如果您希望程序在未安装的情况下工作(即只提取tarball),请将其放在程序的目录中。

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. 我首先构建一个默认目录列表并完成它们,直到我找到一个glade.xml实例并停止查找,或者找不到它并退出并显示错误。 Good candidates for the default list are /etc, /usr/share/app-name, /usr/local/etc. 默认列表的优秀候选者是/ 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. 用户配置文件的良好候选者是〜/ .glade.xml或〜/ .app-name / glade.xml或〜/ .app-name / .glade.xml。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 软件配置文件,在Linux系统中应该放在哪里? - Software configuration files, where should I put them in Linux systems? 在GNU / Linux上的哪里保存配置/数据文件? - Where to save configuration/data files on GNU/Linux? 在Linux下的LVS负载平衡器中应在哪里配置导向器IP - Where should the director IP be configured in LVS load balancer under Linux OPEN_MAX在哪里为Linux系统定义? - Where is OPEN_MAX defined for Linux systems? 在LINUX下应该放置应用程序初始化和支持文件? - Under LINUX where should application initialization and support files be placed? 在Linux-gnu / C ++中的哪里找到qextserialenumerator的objbase.h - Where can I find objbase.h for qextserialenumerator in Linux-gnu/C++ 使用Mono从GNU / Linux在USB设备上部署.NET Micro框架应用程序 - Deploy a .NET Micro framework application on USB device from GNU/Linux with Mono 我应该把我的python脚本放在Linux中? - Where I should put my python scripts in Linux? 在Linux上存储应用程序数据(非特定于用户)的位置 - Where to store application data (non-user specific) on Linux 从“ Docker Hub”下载映像后,应将配置文件放在哪里? 在数据量上? - After downloading image from “Docker Hub”, where should I put my configuration file? In data volume?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM