8

I'm trying to use Autotools to build my C program that needs to be linked against certain libraries. It only contains one C source file.

This is the Makefile.am associated with it:

bin_PROGRAMS=game
game_SOURCES=main.c
game_CFLAGS=`pkg-config --cflags libglfw`
game_LDFLAGS=`pkg-config --libs libglfw`

When I run make, it tries to compile it using this:

gcc `pkg-config --cflags libglfw` -g -O2 `pkg-config --libs libglfw`  -o game game-main.o

However this is wrong, as the library link flags must be at the end, or else it will give errors about undefined references. For example if I run this:

gcc `pkg-config --cflags libglfw` -g -O2   -o game game-main.o `pkg-config --libs libglfw`

It compiles fine.

How can I make it so the LDFLAGS primary is appended at the end rather than in the middle?

kaykun
  • 2,247
  • 2
  • 24
  • 37

1 Answers1

12

You can start by not abusing LDFLAGS for libraries. LDFLAGS is for linker flags. Use foo_LDADD (for executables) or foobar_LIBADD (when producing a library) to list link libraries.

Also, running pkg-config inside Makefile.am is unnecessary and wasteful. Just use:

game_CFLAGS = ${libglfw_CFLAGS}
game_LDADD  = ${libglfw_LIBS}

libglfw_CFLAGS,LIBS is populated by this in configure.ac:

PKG_CHECK_MODULES([libglfw], [libglfw])

user502515
  • 4,346
  • 24
  • 20
  • How would I solve this with a regular makefile, rather than autotools? As far as I can tell, make doesn't respect LDADD (just autotools). – Matthew Aug 20 '12 at 19:16
  • 2
    Follow-up: With regular makefiles, the solution is to use `LOADLIBES = -lm` rather than `LDFLAGS = -lm`. – Matthew Aug 20 '12 at 19:33