|
From: | Sander Niemeijer |
Subject: | Re: conditionally compiling C++ code |
Date: | Tue, 29 Apr 2003 19:55:25 +0200 |
On Tue, Apr 29, 2003 at 02:31:54PM +0200, Sander Niemeijer wrote:I was just wondering, but aren't the --with-PACKAGE/--enable-FEATURE options of configure meant for this? I have a package that can optionally make use of the HDF4 library(which, if included, provides extra functionality to import/export datain HDF4 format). I dealt with this in my configure.ac as follows: --- AC_ARG_WITH([hdf4], [AC_HELP_STRING([--with-hdf4],[build HDF4 support into this package])], [ac_cv_with_hdf4=$withval], [AC_CACHE_CHECK([use HDF4],ac_cv_with_hdf4,ac_cv_with_hdf4=no)]) if test $ac_cv_with_hdf4 = yes ; then ... <Check for HDF4 libs and headers via environment variables that specify their location and set HAVE_HDF4 definition and ac_cv_have_hdf4> ... if test $ac_cv_have_hdf4 = no ; then AC_MSG_ERR([HDF4 libraries and/or header files are not found. Try setting the appropriate environment variables.]) fi fi ---This approach forces you to explicitly say that you want to make use ofthe HDF4 functionality and if so it will give an error if it could not find the HDF4 libs/headers.Exactly, and I was suggesting that part should become part of say theAC_CHECK_LIB macro, so you wouldn't have to write it (yet again) in yourconfigure.ac.
In my opinion this wouldn't be a good idea. The HDF4 example above is maybe a good example to illustrate this. For HDF4 you would need four libraries (-lmfhdf -ldf -lz -ljpeg). In this case it it is much more logical to check whether we need to find and include all four libraries with one configure option, instead of four options for each of the libraries. This also hides the 'configure user' from the inter library dependencies. And as the --with-PACKAGE and --enable-FEATURE names already say, the options should be given in terms of packages or features (and not specific files), which is a good idea in my opinion.
Furthermore, in the case of my example there is a HAVE_HDF4 #define created that includes/excludes specific parts of my package's source code that deals with HDF4 import/export. Although it is possible to introduce similar HAVE_<libname> #defines for libraries, I think it is a bad idea to have conditional code based on the availability of a certain library. Within source code you should only have conditional parts based on feature usage (should we include HDF4 import/export functionality yes/no. It is in general a bad idea to derive whether to include a feature from the availability of a single library in my opinion (it is usually also linked to the availability of one or more header files for instance). It should always be the other way around), declarations (including the proper header files), and system/platform/compiler specifics (stuff like WORDS_BIGENDIAN). As a result, if you leave out the HAVE_<libname> possibility, then you either don't need the library at all (in which case you don't include any checks for it and don't link to it) or you /do/ need it, in which case you should generate an error if it is not found (otherwise the compiler will do the complaining for you at link time). So the functionality you are proposing is only useful if it would generate a HAVE_<libname> #define to determine whether to include a certain feature of your package (which is a bad idea as I explained before).
Regards, Sander
[Prev in Thread] | Current Thread | [Next in Thread] |