如何找出从哪里加载程序数据?

发布于 2024-07-15 04:20:39 字数 344 浏览 6 评论 0原文

我有一个用 C++ 编写的项目(在 glibmm 的帮助下),并且我正在使用自动工具来管理它。 我要问的问题是“我到底如何确定从哪里加载东西?”。 虽然我可以在自动工具上找到所有我想要的指南,但没有一个能回答这个问题。

例如,地图放在 $DATADIR/maps 中(通常是 /usr/[local/]share/myprogram/maps。Autotools 做了正确的事情并将它们放在那里,但是我如何知道它是将它们放在那里还是 $HOME/myprogram/maps 并适当地加载它们(理想情况下它会首先搜索 $PWD,但这很容易)。

I've got a project written in C++ (with glibmm helping), and I'm using autotools to manage it. The question I have to ask is "HOW ON EARTH DO I FIGURE OUT WHERE TO LOAD STUFF FROM?". While I can find all the guides I want to on autotools, none answer this question.

For example, maps go in $DATADIR/maps (usually /usr/[local/]share/myprogram/maps. Autotools does the right thing and puts them there, but how do I know whether it put them there or $HOME/myprogram/maps and load them appropriately (idealy it'd would search $PWD first, but that's easy).

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

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

发布评论

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

评论(2

素年丶 2024-07-22 04:20:39

您可以将字符串构建到您可能想要查看的各种自动配置位置的可执行文件中。这或多或少是 GCC 等程序所做的事情。

例如,我机器上的 GCC 4.3.3 是在 /work5/tmp 下编译的,并配置为安装在 /usr/gcc/v4.3.3 中,并且它包含(以及许多其他字符串)字符串:

/usr/ccs/bin/
/usr/ccs/lib/
/usr/gcc/v4.3.3
/usr/gcc/v4.3.3/bin/
/usr/gcc/v4.3.3/lib
/usr/gcc/v4.3.3/lib/gcc/
/usr/gcc/v4.3.3/libexec/gcc/
/usr/gcc/v4.3.3/share/locale
/usr/gnu/include
/usr/include
/usr/include/iso
/usr/include/sys
/usr/lib/
/usr/lib/ld.so.1
/usr/libexec/gcc/
/usr/local/share/bison
/usr/tmp
/var/tmp
/var/tmp/
/work5/tmp/gcc-4.3.3-obj/./prev-gcc/include
/work5/tmp/gcc-4.3.3-obj/gcc
/work5/tmp/gcc-4.3.3-obj/intl
/work5/tmp/gcc-4.3.3-obj/libiberty
/work5/tmp/gcc-4.3.3-obj/sparc-sun-solaris2.10/libgcc

一条注释说:“这就是我的意思正在想办法怎么办?”

方法有很多种。 部分答案是知道您需要哪些名称。 如果查看自动配置输出,会发现 makefile 中有诸如 prefixdir 和 pkglibdir 之类的名称。 您可以将它们添加到 C 编译器行:

DFLAG1 = -DPKGLIBDIR='"$(pkglibdir)"'

并将 DFLAG1 添加到 CFLAGS。 重复令人作呕...

程序内的代码可以执行以下操作:

static const char *libdirs_to_search[] =
{
    ...
#ifdef PKGLIBDIR
    PKGLIBDIR,
#endif /* PKGLIBDIR */
};

一种更简洁的方法是修改生成的 config.h 文件和配置代码,以便在配置中定义您感兴趣的目录。 h 文件中,而不是 makefile 中。

You can build the strings into your executable for the various autoconfigured locations that you might want to look in. This is more or less what programs such as GCC do.

For example, GCC 4.3.3 on my machine was compiled under /work5/tmp and configured for installation in /usr/gcc/v4.3.3, and it contains (amongst many others) the strings:

/usr/ccs/bin/
/usr/ccs/lib/
/usr/gcc/v4.3.3
/usr/gcc/v4.3.3/bin/
/usr/gcc/v4.3.3/lib
/usr/gcc/v4.3.3/lib/gcc/
/usr/gcc/v4.3.3/libexec/gcc/
/usr/gcc/v4.3.3/share/locale
/usr/gnu/include
/usr/include
/usr/include/iso
/usr/include/sys
/usr/lib/
/usr/lib/ld.so.1
/usr/libexec/gcc/
/usr/local/share/bison
/usr/tmp
/var/tmp
/var/tmp/
/work5/tmp/gcc-4.3.3-obj/./prev-gcc/include
/work5/tmp/gcc-4.3.3-obj/gcc
/work5/tmp/gcc-4.3.3-obj/intl
/work5/tmp/gcc-4.3.3-obj/libiberty
/work5/tmp/gcc-4.3.3-obj/sparc-sun-solaris2.10/libgcc

A comment says: "That's what I'm trying to figure out how to do?"

There are numerous ways. Part of the answer is knowing which names you need. If you look at the autoconfigure output, there are names such as prefixdir and pkglibdir in the makefile. You could add those to the C compiler lines:

DFLAG1 = -DPKGLIBDIR='"$(pkglibdir)"'

and add DFLAG1 to your CFLAGS. Repeat ad nauseam...

The code inside your program can then do things like:

static const char *libdirs_to_search[] =
{
    ...
#ifdef PKGLIBDIR
    PKGLIBDIR,
#endif /* PKGLIBDIR */
};

A cleaner way to do it is to modify the generated config.h file and the configure code so that the directories you are interested in are defined in the config.h file, rather than in the makefile.

笨死的猪 2024-07-22 04:20:39

乔纳森·莱弗勒(Jonathan Leffler)的回答很有帮助(他为我提供了足够的信息来获取源代码进化并找到我需要做的事情),但它并没有完全回答我的问题,所以我将在这里发布完整的解决方案:

在配置中。 ac(或configure.in,无论如何)我在末尾添加了以下行:

# Defines so we know where to look
privdatadir='${datadir}'/myprogram
AC_SUBST(privdatadir)

mapsdir='${privdatadir}'/maps
AC_SUBST(mapsdir)

在 src/Makefile.am 中,添加了以下内容:

AM_CXXFLAGS = -DMYPROGRAM_MAPSDIR=\"$(mapsdir)\"

在实际的 C++ 代码中,使用了以下内容:

const std::string file_directory (MYPROGRAM_MAPSDIR); // Defined in Makefile.am

再次感谢乔纳森向我展示了哪里看看,但我想为后代发布完整的答案。

Jonathan Leffler answer was helpful (he provided enough info for me to apt-get source evolution and find what I needed to do), but it didn't entirely answer my question, so I'll post the complete solution here:

In configure.ac (or configure.in, whatever) I added the following lines at the end:

# Defines so we know where to look
privdatadir='${datadir}'/myprogram
AC_SUBST(privdatadir)

mapsdir='${privdatadir}'/maps
AC_SUBST(mapsdir)

In src/Makefile.am, the following was added:

AM_CXXFLAGS = -DMYPROGRAM_MAPSDIR=\"$(mapsdir)\"

In the actual C++ code, the following was used:

const std::string file_directory (MYPROGRAM_MAPSDIR); // Defined in Makefile.am

Once again, thanks Jonathan for showing me where to look, but I wanted to post the complete answer for posterity.

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