Sophie

Sophie

distrib > Mandriva > 9.1 > ppc > by-pkgid > 26bbc7f1dcc4a86a13a484ac133c34b6 > files > 21

gengetopt-2.8.1-1mdk.ppc.rpm

WHAT IF GETOPT_LONG IS NOT THERE?

If you use gengetopt to generate C functions for parsing command line
arguments you have to know that these generated functions use getopt_long
to actually read the command line and parsing it. This function is typically
part of the standard C library, but some libraries may not include it.
But this is not a problem: we provide C files that actually implement
getopt_long function: getopt.c getopt1.c and getopt.h.
You'll find these files in the <prefix>/share/gengetopt directory
where <prefix> is the one you specified during compilation.
If no prefix had been specified, /usr/local is the default.
If you downloaded gengetopt in binary form prefix will probably be
/usr/local or /usr.

You can simply compile these files and link them to the executable
of you program. But if you use automake and autoconf here's a more
elegant solution; keep on reading ...

This a little explanation on how discovering if getopt_long
is in the standard C library of your C compiler and what to do if it
is not there. This technique can be used if you use automake and autoconf
to build your package's configure script.

In configure.in add the following lines:

dnl check for getopt in standard library
AC_SUBST(LIBOBJS)
AC_CHECK_FUNCS(getopt_long , , [LIBOBJS="$LIBOBJS getopt.o getopt1.o"] ) 

This macro checks if getopt_long function is in C library;
if it is not there it adds getopt.o and getopt1.o to the objects files
that will be linked to your executable (LIBOBJS).
Notice: the AC_SUBST is not necessary if you have already used
a macro such as AC_REPLACE_FUNC.

Then in Makefile.am of your source directory you have to add the contents
of LIBOBJS to the LDADD of the program that has to use getopt_long;
e.g. if the program 'foo' has to use getopt_long, you have to add
the following line

foo_LDADD = @LIBOBJS@

Now these files will be compiled and linked to your program only if
necessary.

Moreover you have to add getopt.c getopt1.c and getopt.h to your
distribution. Note that it is not necessary to put these file names
among the foo_SOURCES contents), but you have to add getopt.h to EXTRA_DIST:

EXTRA_DIST = getopt.h 

You may want to take a look at gengetopt's configure.in and src/Makefile.am:
they both use the techniques described here.

I hope you find this document useful :-)

Happy Coding!

      Lorenzo Bettini <bettini@gnu.org>