6

I'm just getting started with autotools, and have followed A. Duret-Lutz's tutorial closely to get a working C hello world that uses GNU gettext.

The AM_CFLAGS and AM_LDFLAGS are set appropriately in the Makefile.am, and the code compiles and runs successfully.

The issue is that autoconf is not searching in the directories that AM_CFLAGS and AM_LDFLAGS is telling the compiler to search, and as a result not defining the HAVE_GETTEXT_H, HAVE_LIBINTL, etc. macros in the config.h.

How can I get the configure.ac to supplement the library and include directories it searches when using AC_CHECK_LIB and AC_CHECK_HEADERS?

klmanion
  • 63
  • 1
  • 5
  • Can you provide an example? Ordinarily, a user would use something like `./configure CFLAGS=-I/path/to/gettext/include LDFLAGS=-L/path/to/gettext/lib` for nonstandard library locations, and those would be used by the `AC_CHECK_HEADERS` and `AC_CHECK_LIB` macros if I recall correctly. See [this answer](https://stackoverflow.com/a/10210401/539810) for example. –  Jun 13 '17 at 02:39
  • @ChronoKitsune I'm realizing that passing environment variables to configure might be the best way to do this, but I was hoping for a way to add those nonstandard library locations in the code. – klmanion Jun 13 '17 at 15:47

2 Answers2

6

I think I misread the original question but since nothing in my other answer is incorrect per se I'll add another answer.

In order to use custom paths in AC_CHECK_HEADER and AC_CHECK_LIBS one has to (temporarily) set CFLAGS and LDFLAGS accordingly:

CFLAGS_backup="${CFLAGS}"
LDFLAGS_backup="${LDFLAGS}"
CFLAGS="-I/path/to/an/additional/include/ ${CFLAGS}"
LDFLAGS="-L/path/to/the/lib/ ${LDFLAGS}"

AC_CHECK_HEADER(...)
AC_CHECK_LIB(...)

## reset CFLAGS and LDFLAGS
CFLAGS="${CFLAGS_backup}"
LDFLAGS="${LDFLAGS_backup}"

Within AC_CHECK_* you'd typically set GETTEXT_CFLAGS or LIBINTL_LIBS as variables and export them for use in automake per AC_SUBST([GETTEXT_CFLAGS]) and AC_SUBST([LIBINTL_LIBS]) respectively.

Unfortunately, you cannot access AM_CFLAGS or AM_LDFLAGS in configure.ac.

Now in Makefile.am you can use

AM_CFLAGS = $(GETTEXT_CFLAGS) <other stuff>
AM_LDFLAGS = $(GETTEXT_LIBS) <other stuff>

For convenience, typically, you'd expose a parameter to the user as well, either via AC_ARG_WITH or AC_ARG_VAR, so they can use --with-gettext or LIBINTL_LIBS=... along with the configure command.

Seeing as autoconf is really only m4 you could wrap the above in a macro yourself. And seeing as we talk about gettext here, there is already such a thing: AM_GNU_GETTEXT, an m4 macro that you could use in your configure.ac after you called gettextize.

hroptatyr
  • 4,702
  • 1
  • 35
  • 38
1

Instead of AC_CHECK_HEADER, use AC_CHECK_HEADERS, that defines tokens of the form HAVE_<HEADER>_H. The singular form expects you to define things yourself using the ACTION-IF-FOUND (2nd argument).

For AC_CHECK_LIB there is no such comfort, you must use the ACTION-IF-FOUND (3rd argument) and AC_DEFINE whatever is needed.

Additionally there will be shell variables ac_cv_header_<HEADER>_h and ac_cv_lib_<LIBRARY>_<FUNCTION> set.

hroptatyr
  • 4,702
  • 1
  • 35
  • 38