views:

115

answers:

2

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).

+2  A: 

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.

Jonathan Leffler
...that's what I'm trying to figure out how to do.
MighMoS
+1  A: 

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.

MighMoS