7

I need to include the GLib headers for a project that is built with an autoconf-based system for portability.

How can I safely import the GLib headers in a portable manner? I know about pkg-config, but that is not entirely portable (since some systems don't have it and I would prefer to only rely on autoconf for configuration).

Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193
alternative
  • 12,703
  • 5
  • 41
  • 41
  • Which systems don't have pkg-config? – Gonzalo Nov 14 '09 at 01:27
  • By "don't have" I mean don't have great support for it by default (ie Windows) – alternative Nov 14 '09 at 01:32
  • Either way, I would prefer to do it with just autotools if possible. If necessary, I can resort to pkg-config. – alternative Nov 14 '09 at 01:37
  • 1
    By default no system has it. In windows you can use a native one http://www.gtk.org/download-windows.html or the cygwin one. – Gonzalo Nov 14 '09 at 01:39
  • If you want to go with autotools, you'll have to check a few well-known folders for glib header files and libraries and then set variables for what you find. – Gonzalo Nov 14 '09 at 01:40
  • Great question, I am tripping over the same problem right now. – Setjmp Dec 07 '09 at 05:24
  • If you have glib on windows you get a version of pkg-config with it, basically just assume you have it if you're building with autotools. It'd be kind of stupid to have mingw and glib without getting pkg-config... that'd just be shooting yourself in the foot (same goes for cygwin) if someone building your code is that obtuse, just ignore them. – Spudd86 May 28 '10 at 13:58

2 Answers2

12

By using the PKG_CHECK_MODULES macro, Autoconf-generated configure scripts can retrieve pkg-config data automatically. As an example, adding this line to your configure.ac file:

PKG_CHECK_MODULES([DEPS], [glib-2.0 >= 2.24.1])

will cause the resulting configure script to ensure that the installed version of glib-2.0 is greater than or equal to version 2.24.1 as well as append to variables DEPS_CFLAGS and DEPS_LIBS the output of pkg-config --cflags glib-2.0 and pkg-config --libs glib-2.0, respectively. You then use the $(DEPS_CFLAGS) and $(DEPS_LIBS) variables in the _CFLAGS and _LDADD primaries:

bin_PROGRAMS = hello

hello_CFLAGS = $(DEPS_CFLAGS)
hello_SOURCES = hello.c
hello_LDADD = $(DEPS_LIBS)
Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193
  • Should be `hello_LDADD` not `hello_LIBS` in the `Makefile.am`. – RubenLaguna Feb 06 '15 at 16:49
  • @ecerulm: I think that `hello_LDADD` is for when your project uses [libtool](http://www.gnu.org/software/libtool/). – Daniel Trebbien Feb 06 '15 at 20:08
  • 1
    No, that would be `hello_LIBADD`. `hello_LIBS` doesn'exist. `*_LIBS` is NOT mentioned in the [automake doc](http://www.gnu.org/software/automake/manual/automake.html#Program-and-Library-Variables) . I tried `hello_LIBS` didn't have any effect on the `gcc` command that gets executed. It's completely ignored whereas `hello_LDADD` has the desired effect of adding the `$(DEPS_LIBS)` at the end of the `gcc` command line. – RubenLaguna Feb 07 '15 at 08:02
  • @ecerulm: Ah! You are right. I will correct my answer. – Daniel Trebbien Feb 09 '15 at 13:26
4

The GLib 2.22 INSTALL file states that pkg-config is a requirement for installing this library. I am not being GLib (pun intended!); statement of this requirement is one of the first things on the top of the INSTALL file.

From the text surrounding it is unclear whether pkg-config is needed to compile GLib itself, however it is clear that GLib 2.22 authors do not intend for any users to compile against GLib without having pkg-config. In particular, GLib's make install will install .pc files appropriately.

For platform portability, instruct the user to set $PKG_CONFIG_PATH appropriately.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Setjmp
  • 27,279
  • 27
  • 74
  • 92