0

I am using for the first time autotools & Cie and I am lost. I generate about one hundred dynamic libraries in my openwrt environment in differents packages. Later in a binary, I use dlopen + dlsym to use my library. This allow me to use a kind of plugins system.

Now I would like to be able to link all my libraries statically instead of using my plugin sytem. Basically I would like to say if my library A is defined in my .config file, link it with my binary. So now I have just tu use dlsym functionnalities without use of dlopen feature.

I am able to do it but in my opinion, in a very dirty way.

  • Use my library if the package is defined in the .config Makefile of the package of my binary
  PKG_BUILD_DEPENDS+= +PACKAGE_LIBA:LIBA
  ifdef CONFIG_PACKAGE_LIBA
     CONFIGURE_ARGS+= --enable-LIBA
  endif
  • configure.ac of my binary in dl directory
AC_ARG_ENABLE([LIBA],
    [AS_HELP_STRING([--enable-LIBA], [Enable link with LIBA (default no)])],
    [ if test x"$enableval" = "xno" ; then LIBA="0" ; else LIBA="1" ; fi ],
    [LIBA="0"])  
if test "$LIBA" = "1" ; then
    [LIBA="-la"]
    AC_SUBST([LIBA_LIBS])  
fi
  • Makefile.am of my binary in dl directory

MYBINARY_LDADD = $(LIBA_LIBS)

Now, I need to do that for hundred of libraries. Moreover, if LIBA is using for example a standard library I need to add it manually... Quite ironic to use AUTOtools like this... Anyway it is working but so ugly.. The benefit of this is that I only need to modify my binary Makefile/configure and not all packages. If I could I did that only with Makefile but one of my requirement here is to use autotools. I have also try another solution with pc.in file where I have to describe my library, but I was not able to use it correctly. And not sure that it was really clean to follow this solution.

Thanks, Arthur.

ArthurLambert
  • 749
  • 1
  • 7
  • 30

1 Answers1

3

it is a common misconception that autotools would make everything automatic. instead you must be very explicit, e.g. you must add every single source-file to your Makefile.am, rather than using wildcards.

anyhow, for your specific problem, i would creat a single variable (e.g. MY_LIBS) and put all your plugin-libraries you need to link to, into this single variable:

PLUGIN_LIBS=""
AC_ARG_ENABLE([LIBA],
 [AS_HELP_STRING([--enable-LIBA], [Enable link with LIBA (default no)])],
 [ test x"$enableval" = "xyes" && PLUGIN_LIBS="${PLUGIN_LIBS} -la" || true ])
AC_ARG_ENABLE([LIBB],
 [AS_HELP_STRING([--enable-LIBB], [Enable link with LIBB (default no)])],
 [ test x"$enableval" = "xyes" && PLUGIN_LIBS="${PLUGIN_LIBS} -lb" || true ])
# ...
AC_SUBST([PLUGIN_LIBS])

in practice you would create an m4-macro that does the repeated tasks (calling AC_ARG_ENABLE, setting the help-string, doing the test, adding the libs to PLUGIN_LIBS), so you end up with:

PLUGIN_LIBS=""
PLUGIN_BUILTIN([LIBA], [-la])
PLUGIN_BUILTIN([LIBB], [-lb])
# ...

in both cases, your Makefile.am would look like:

MYBINARY_LDADD = $(PLUGIN_LIBS)
umläute
  • 28,885
  • 9
  • 68
  • 122
  • Thanks about your tips with m4 macro, I will check it. Until right now I writted a shell script which read my .config of my openwrt environment and generate configure.ac, Makefile, etc... I also used sed to generate all the function to initialize my plugin in my code. It's really helpfull when you have to modify the build process of a very old and big project. Moreover I never use wildcard in Makefile. Just need one line : `%.o: %.cpp` and `$(CC) $(CFLAGS) -c $<`. – ArthurLambert Jul 17 '13 at 10:47
  • i mainly wanted to point out, that `autotools` will not try to *auto* matically guess which files belong to your project. the *auto* refers to how it will *auto* matically figure out how to build your project on platform XY (which you might never have heard of). – umläute Jul 17 '13 at 11:06