[Ex. 41 devpkg] How to properly link to the apr libraries on Ubuntu

I had problems linking to the apr libraries on Ubuntu. I managed to solve them by altering the Makefile. I thought I’d post the solution here in case anybody else hits this roadblock.

I believe the problem is that gcc cannot parse flags that name specific libraries like -lapr-1 if they are passed before the local source files. Both CFLAGS and LDFLAGS are injected at the beginning of the cc command.

Make actually has another variable for such flags: LDLIBS. If we use it for -lapr-1 and -laprutil-1, the flags are injected at the right position and compilation succeeds. Here’s how it worked for me:

PREFIX?=/usr/local
CFLAGS=-g -Wall	-I${PREFIX}/apr/include/apr-1 -I${PREFIX}/apr/include/apr-util-1
LDLIBS=-lapr-1 -laprutil-1
LDFLAGS= -L${PREFIX}/apr/lib -pthread

@zedshaw: I believe this should work on all platforms. Should you ever happen to revise the book, maybe it’d be worth to change this?

Awesome, thanks for the tip.

A free service run by Zed A. Shaw for learncodethehardway.org.