views:

154

answers:

3

When using autotools (with a config.h file) for both a library, and a software built on that library, the compiler complains about a redefinition of some macros (PACKAGE_NAME, PACKAGE_TARNAME and so on).

How can I prevent this?

The config.h file is needed in the library to propagate it's setting to the software that use it.

Right now I have a wrapper script library_config.h that includes the original config.h and provides defaults when the user is not using autotools, but even undefining the macros in that package I get the redefinition warning from gcc.

#ifndef LIB_CONFIG_H
#define LIB_CONFIG_H
#ifdef HAVE_CONFIG_H
#  include "config.h"
#  undef PACKAGE
#  undef PACKAGE_BUGREPORT
#  undef PACKAGE_NAME
#  undef PACKAGE_STRING
#  undef PACKAGE_TARNAME
#  undef PACKAGE_VERSION
#  undef VERSION
#else
#  if defined (WIN32)
#    define HAVE_UNORDERED_MAP 1
#    define TR1_MIXED_NAMESPACE 1
#  elif defined (__GXX_EXPERIMENTAL_CXX0X__)
#    define HAVE_UNORDERED_MAP 1
#  else
#    define HAVE_TR1_UNORDERED_MAP 1
#  endif
#endif
#endif

I believe the best option would be to have a library without that macros: How can I avoid the definition of PACKAGE, PACKAGE_NAME and so on in the library when using autotools?

EDIT: attemp to explain better.

When using the AC_CONFIG_HEADER macro in the configure.ac of the library I produce a config.h file with a lot of useful defines. This defines are useful for the library itself (both for it's compiled part and for its header files) AND for the client software. It's a pity however that the AC_CONFIG_HEADER mixes the useful macros that I need with other generic defines with fixed names (PACKAGE, PACKAGE_NAME) that clash when autotools are used also for the client software configuration.

+2  A: 

If your application might dynamically link to the library, it makes no sense to have the static strings from the library statically built into the application.

Otherwise, you might want to look for ax_prefix_config_h.m4.

I would still just try to avoid installing a config.h file and define the library's API without resorting to config.h:

/* library.h */
const char library_version[];

/* library.c */
#include "library.h"
#include "config.h" /* the library's config.h */
const char library_version[] = PACKAGE_VERSION;

/* application.c */
#include "library.h"
#include "config.h" /* the application's config.h */
int main()
{
   print("This is app version %s.\n", PACKAGE_VERSION);
   print("The library is version %s\n", library_version);
   return 0;
}

Make sure that the public library header files do not #include "config.h", and that the library does not install its config.h.

ndim
Thank you, but this does not actually answer the question. The header files from the library need to include "config.h" (they depend on other macro that are defined there).
baol
If your `library.h` ***really*** needs to include its `config.h`, the first thing is to make sure that it is NOT included via `#include "config.h"`. Call it library-config.h, at the very least, or install it into `/usr/include/lib-1/library/config.h` and include it like `#include <library/config.h>".
ndim
At least your above `library_config.h` does not make use of any macros from `config.h` as far as I can tell.
ndim
I'll consider changing name to my `config.h` file. Anyway this will not resolve the macro redefinition issues.
baol
And by the way library_config.h is a wrapper script that is included (AND USED) by the library header files.
baol
I've noticed you edited the answer a lot. In it's current form the " ax_prefix_config_h.m4" could actually solve my problem. Thank you.
baol
+1  A: 

You should be able to designate the library as a "subpackage" of the software by calling AC_CONFIG_SUBDIRS([library]) in the software's configure.ac. That should eliminate the conflicts, and the settings from the top-level configure will still propagate down to the subpackage.

Here is the link to the page of the Autoconf manual describing this feature: http://www.gnu.org/software/hello/manual/automake/Subpackages.html

ptomato
The OP still wants to `#include` both `config.h` files into the same `foo.c` file. `AC_CONFIG_SUBDIRS` will not help there in any way.
ndim
But since the subpackage's configure is called with the same arguments and settings, the OP can generate the same `#defines` in both `config.h`s, and therefore won't need to include both anymore.
ptomato
So obviously the `config.h` to be `#include` d would be the library's. Then every time you need to add some config feature to the *application*, you need to adapt the *library* 's `configure.ac`... that sounds like a recipe for a lot of confusion to me. I would avoid that.
ndim
No, you'd include the library's config in the library, and the application's config in the application.
ptomato
A: 

How do you deal with PACKAGE_* macro conflicts when the library you are distributing consists of C++ template code that must include the generated config.h macros to utilize machine specific configuration information? The conflict will occur when a user of the library compiles your distributed template code and the client code uses a generated autoheader generated file.

Do I need to manually remove the conflicting macro definitions from the generated header before distribution?