如何启用共享对象在运行时访问数据文件 (UNIX)

发布于 2024-10-06 10:17:49 字数 429 浏览 3 评论 0原文

我有一个类方法(在 UNIX 环境中的共享对象中实现),需要在运行时访问文本数据文件(使用 ifstream)。目前,该方法假设数据文件可在没有任何相对路径的情况下打开,即类似

ifstream dataFile("data.txt");

共享对象是从 python 代码加载的,为了使其可用于加载,它被复制到 \ usr\lib\ 文件夹作为 makefile 的构建后步骤。我的问题是如何使文本数据文件可用于共享对象。我考虑了以下可能性:

  • 使​​用一些相对路径,但该方法并非完全万无一失(该项目托管在各种实例上,我无法确定目录树是否会保持不变(例如)一个月后)。
  • 将数据文件也复制到\usr\lib,但我觉得这是一个错误的态度。

欢迎任何建议。

I have a class method (implemented in a shared object in UNIX environment) which needs to access a text data file in runtime (using ifstream). Currently the method assumes that the data file is available for opening without any relative path, i.e something like

ifstream dataFile("data.txt");

The shared object is loaded from python code, and in order for it to be available for loading, it is being copied to the \usr\lib\ folder as a post-build step of the makefile. My question is how to make the text data file available for the shared object. I have considered the following possibilities:

  • Use some relative path, but that method is not totally fool-proof (the project is hosted on various instances and I cannot be sure the directory tree will stay the same (e.g) a month from now).
  • copy the data file as well to \usr\lib, but I feel this is a wrong attitude.

Any suggestions are welcomed.

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

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

发布评论

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

评论(2

琉璃繁缕 2024-10-13 10:17:49

正确的方法是将文本文件的位置设置为可配置值,该值将在安装项目时设置。使用 /etc/ 中的配置文件是存储该值的常见方法。

这样,您可以将文本文件与所有与机器无关的文件(该数据文件机器独立的,对吧?)放在例如 /usr/share/ 中,并且您的代码将“知道”在哪里找到它。

请注意,如果数据文件将作为代码操作的一部分进行修改,那么根据文件系统层次结构标准 (FHS),它可能应该放置在 /var(/var/lib 或 /var/cache)下的某个位置,并且大多数其他 Unix 文件系统标准。

如果数据文件可以被视为配置文件,正如您在评论之一中提到的,您可以将其路径硬编码到 /etc/ 下的某个位置(例如 /etc/MyProject/data.cfg)并继续。

The proper way to go about this is to make the location of the text file a configurable value that will be set when your project is installed. Using a configuration file in /etc/ is a common way to store that value.

That way you can put the text file in e.g. /usr/share/ with all the machine-independent files (that data file is machine-independent, right?) and your code would "know" where to find it.

Note that if the data file is going to be modified as part of your code's operation, then it should probably be placed somewhere under /var (/var/lib or perhaps /var/cache) according to the Filesystem Hierarchy Standard (FHS) and most other Unix filesystem standards.

If the data file could be considered a configuration file, as you mentioned in one of your comments, you could just hard-code its path to somewhere under /etc/ (e.g. /etc/MyProject/data.cfg) and go on.

雄赳赳气昂昂 2024-10-13 10:17:49

我可以想到两种解决方案:

  • 当您加载共享对象时,您以某种方式为其提供文件的路径。
  • 您可以在 /usr/lib 中创建一个符号链接,而不是将文件复制到 /usr/lib ,但这并不是最好的做法。

第一个解决方案对我来说是最好的。

I can think of two solutions :

  • When you load your shared object, you somehow give it the path to your file.
  • Instead of copying the file to /usr/lib you could create a symbolic link do it in /usr/lib but that is not the best thing to do imho.

The first solution is the best one for me.

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