将目录传递给 C++来自编译器的应用程序
某些应用程序包含由驻留在 /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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将
@libexecdir@
替换变量的值(例如在Makefile.in
中使用)公开给您的C++ 代码。最简单、最可靠的方法是在编译器命令行上使用-D
开关来获取需要知道的目标文件:在
foo.cc
中,LIBEXECDIR
然后将是一个预处理器宏,扩展为具有您需要的路径的字符串常量。不过,有两个警告:上面的 Makefile 片段使用了 GNU make 功能,target-具体变量。它在其他 Make 实现中不起作用。另外,我没有在$(libexecdir)
的扩展中引用任何字符。完全防御性引用看起来像这样:如果您希望能够使用 Windows 路径名,并且斜杠走错方向,那么您肯定至少需要最里面的
$(subst ...)
构造。人们通常不会将 ' 或 " 放在路径名中,因此我可能不会打扰外面的两个,直到有人抱怨为止。相同的技术适用于任何
@whatever@
替换变量不是AC_DEFINE
您可能认为可以使用
AC_DEFINE_UNQUOTED
以某种方式获取 的值。$(libexecdir)
到config.h
中,因此可以避免使用命令行进行所有这些操作,不幸的是,Autoconf 无法完全计算其@ 的值。 *dir@
在配置时替换:因此,如果您使用
AC_DEFINE_UNQUOTED
做明显的事情,您将在 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: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: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 anAC_DEFINE
.You might think you could use
AC_DEFINE_UNQUOTED
somehow to get the value of$(libexecdir)
intoconfig.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:Therefore, if you do the obvious thing with
AC_DEFINE_UNQUOTED
, you will get something likein your config.h. So that's not going to work, and I don't see a good way to make it work.