2

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?

1 Answer 1

3

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.