在使用 Autotools 构建的 C 程序中定位数据文件

发布于 2024-11-18 00:42:34 字数 547 浏览 7 评论 0原文

我有一个使用 Autotools 构建的 C 程序。在 src/Makefile.am 中,我定义了一个宏,其中包含已安装数据文件的路径:

AM_CPPFLAGS = -DAM_INSTALLDIR='"$(pkgdatadir)"'

问题是我需要运行 make install 才能测试二进制文件(因为它需要能够找到数据文件)。

我可以使用源树的路径定义另一个宏,以便无需安装即可找到数据文件:

AM_CPPFLAGS = -DAM_INSTALLDIR='"$(pkgdatadir)"' -DAM_TOPDIR='"$(abs_top_srcdir)"'

现在,我想要以下行为:

  1. 如果二进制文件是通过 make install 安装的,请使用 AM_INSTALLDIR获取数据文件。
  2. 如果未安装二进制文件,请使用 AM_TOPDIR 获取数据文件。

这可能吗?有没有更好的方法来解决这个问题?

I have a C program built using Autotools. In src/Makefile.am, I define a macro with the path to installed data files:

AM_CPPFLAGS = -DAM_INSTALLDIR='"$(pkgdatadir)"'

The problem is that I need to run make install before I can test the binary (since it needs to be able to find the data files).

I can define another macro with the path of the source tree so the data files can be located without installing:

AM_CPPFLAGS = -DAM_INSTALLDIR='"$(pkgdatadir)"' -DAM_TOPDIR='"$(abs_top_srcdir)"'

Now, I would like the following behavior:

  1. If the binary was installed via make install, use AM_INSTALLDIR to fetch data files.
  2. If the binary was not installed, use AM_TOPDIR to fetch data files.

Is this possible? Is there a better approach to this problem?

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

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

发布评论

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

评论(3

酒废 2024-11-25 00:42:34

我做什么(在 https://http://rhdunn.github.com/cainteoir/< /a>)是:

const char *basedir = getenv("CAINTEOIR_DATADIR");
if (!basedir)
    basedir = DATADIR "/" PACKAGE; // e.g. /usr/share/cainteoir-engine

然后运行它(在tests/harness.py中):

CAINTEOIR_DATADIR=`pwd`/data src/apps/metadata/metadata test_file.epub

然后,这允许用户根据需要更改获取数据的位置。

What I do (in https://http://rhdunn.github.com/cainteoir/) is:

const char *basedir = getenv("CAINTEOIR_DATADIR");
if (!basedir)
    basedir = DATADIR "/" PACKAGE; // e.g. /usr/share/cainteoir-engine

and then run it (in tests/harness.py) as:

CAINTEOIR_DATADIR=`pwd`/data src/apps/metadata/metadata test_file.epub

This then allows the user to change the location of where to get the data if they wish.

江湖正好 2024-11-25 00:42:34

使程序能够使用 reece 提出的运行时配置是一个很好的解决方案。如果由于某种原因您不希望它在运行时可配置,一个常见的解决方案是构建一个与已安装的二进制文件不同的测试二进制文件(还有与此相关的其他问题,特别是确保您正在测试的程序具有与安装的程序一致的行为。)一种简单的方法是:

bin_PROGRAMS = foo
check_PROGRAMS = test-foo
test_foo_SOURCES = $(foo_SOURCES)
AM_CPPFLAGS = -DINSTALLDIR='"$(pkgdatadir)"'
test_foo_CPPFLAGS = -DINSTALLDIR='"$(abs_top_srcdir)"'

您可能希望拥有一个专用的测试目录并使用与以下名称相同的名称构建程序,而不是使用具有不同名称的二进制文件原来的。

请注意,我已将名称从 AM_INSTALLDIR 更改为 INSTALLDIR。 Automake保留名称
以“AM_”开头供其自己使用,通过使用该名称,您正在践踏 Automake 的
命名空间。

Making the program able to use a run-time configuration as proposed by reece is a good solution. If for some reason you do not want it to be configurable at run-time, a common solution is to build a test binary differently than the installed binary (there are other problems associated with this, in particular ensuring that the program you are testing has behavior that is consistent with the program that is installed.) An easy way to do that is something like:

bin_PROGRAMS = foo
check_PROGRAMS = test-foo
test_foo_SOURCES = $(foo_SOURCES)
AM_CPPFLAGS = -DINSTALLDIR='"$(pkgdatadir)"'
test_foo_CPPFLAGS = -DINSTALLDIR='"$(abs_top_srcdir)"'

Rather than using a binary with a different name, you might want to have a dedicated tests directory and build the program using the same name as the original.

Note that I've changed the name from AM_INSTALLDIR to INSTALLDIR. Automake reserves names
beginning with "AM_" for its own use, and by using that name you are stomping on Automake's
namespace.

风透绣罗衣 2024-11-25 00:42:34

首先是一些附加信息:数据文件正在积极开发中,我有各种脚本需要使用本地数据文件调用二进制文件,而安装的二进制文件应使用稳定的已安装数据文件。

我最初的解决方案使用了 reece 提出的环境变量。但我不想在不同的地方管理环境变量的设置,并且我不想因为错误而承担错误的数据文件的风险。

因此,我最终得到的解决方案是在构建时为两个位置定义宏,并向二进制文件添加一个标志(-local)以强制使用本地数据文件。

A bit of additional information first: The data files are under active development, and I have various scripts that need to call binaries using local data files, whereas installed binaries should use stable, installed data files.

My original solution made use of an environment variable, as proposed by reece. But I didn't want to manage setting up environment variables in various places, and I didn't want any risk of the wrong data files being picked up due to a mistake.

So the solution I ended up with was to define macros for both locations at build time, and add a flag (-local) to the binaries to force local data files to be used.

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