将目录传递给 C++来自编译器的应用程序

发布于 2024-12-18 02:45:22 字数 278 浏览 3 评论 0原文

某些应用程序包含由驻留在 /usr/libexec 中的主应用程序运行的脚本。但是,autoconf 脚本可以通过将 --libexecdir 传递给配置脚本来更改该目录。

例如,在git源代码中运行./configure时,我可以将--libexecdir设置为我想要的任何目录,并且程序仍然会工作。

我需要向 C++ 添加什么才能使此功能发挥作用?换句话说,如何通过编译到程序中的配置脚本设置目录名称?

Some applications contain scripts that are run by the main application that reside in /usr/libexec. However, the autoconf scripts are able to change that directory by passing --libexecdir to the configure script.

For example, when running ./configure in the git source code, I can set --libexecdir to any directory I want, and the program will still work.

What do I need to add to a C++ to make this functionality work? In other words, how can I have a directory name set by a configure script compiled into the program?

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

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

发布评论

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

评论(1

信仰 2024-12-25 02:45:22

您需要将@libexecdir@ 替换变量的值(例如在Makefile.in 中使用)公开给您的C++ 代码。最简单、最可靠的方法是在编译器命令行上使用 -D 开关来获取需要知道的目标文件:

foo.o: CPPFLAGS += -DLIBEXECDIR='"$(libexecdir)"'

foo.cc 中,LIBEXECDIR 然后将是一个预处理器宏,扩展为具有您需要的路径的字符串常量。不过,有两个警告:上面的 Makefile 片段使用了 GNU make 功能,target-具体变量。它在其他 Make 实现中不起作用。另外,我没有在 $(libexecdir) 的扩展中引用任何字符。完全防御性引用看起来像这样:

foo.o: CPPFLAGS += \
  -DLIBEXECDIR='"$(subst ",\",$(subst ','\'',$(subst \,\\,$(libexecdir))))"'

如果您希望能够使用 Windows 路径名,并且斜杠走错方向,那么您肯定至少需要最里面的 $(subst ...) 构造。人们通常不会将 '" 放在路径名中,因此我可能不会打扰外面的两个,直到有人抱怨为止。

相同的技术适用于任何 @whatever@ 替换变量不是 AC_DEFINE


您可能认为可以使用 AC_DEFINE_UNQUOTED 以某种方式获取 的值。 $(libexecdir)config.h 中,因此可以避免使用命令行进行所有这些操作,不幸的是,Autoconf 无法完全计算其 @ 的值。 *dir@ 在配置时替换:

# near the top of the generated 'configure':
exec_prefix=NONE
libexecdir='${exec_prefix}/libexec'

# much, much later -- as part of AC_OUTPUT:
test "x$exec_prefix" = xNONE && exec_prefix='${prefix}'

因此,如果您使用 AC_DEFINE_UNQUOTED 做明显的事情,您将

#define LIBEXECDIR "${exec_prefix}/libexec"

在 config.h 中得到类似的内容,所以这是行不通的,我。没看到一个让它发挥作用的好方法。

You need the value of the @libexecdir@ substitution variable (as used in e.g. Makefile.in) to be exposed to your C++ code. The simplest and most reliable way to do that is with a -D switch on the compiler command line for the object file that needs to know:

foo.o: CPPFLAGS += -DLIBEXECDIR='"$(libexecdir)"'

In foo.cc, LIBEXECDIR will then be a preprocessor macro expanding to a string constant that has the path you need. Two caveats, though: The above Makefile snippet uses a GNU make feature, target-specific variables. It will not work in other Make implementations. Also, I didn't bother quoting any characters in the expansion of $(libexecdir). Fully defensive quoting would look something like this:

foo.o: CPPFLAGS += \
  -DLIBEXECDIR='"$(subst ",\",$(subst ','\'',$(subst \,\\,$(libexecdir))))"'

You will definitely need at least the innermost $(subst ...) construct if you want to be able to use Windows pathnames, with the slashes going the wrong way. People don't usually put ' or " in pathnames, so I probably wouldn't bother with the outer two until someone complained.

The same technique will work for any @whatever@ substitution variable that isn't also an AC_DEFINE.


You might think you could use AC_DEFINE_UNQUOTED somehow to get the value of $(libexecdir) into config.h and so avoid all this mucking around with the command line. Unfortunately, Autoconf doesn't fully compute the value of its @*dir@ substitutions at configure time:

# near the top of the generated 'configure':
exec_prefix=NONE
libexecdir='${exec_prefix}/libexec'

# much, much later -- as part of AC_OUTPUT:
test "x$exec_prefix" = xNONE && exec_prefix='${prefix}'

Therefore, if you do the obvious thing with AC_DEFINE_UNQUOTED, you will get something like

#define LIBEXECDIR "${exec_prefix}/libexec"

in your config.h. So that's not going to work, and I don't see a good way to make it work.

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