Search code examples
shellmakefilepkg-config

Using glib in C - correct invocation of pkg-config in a Makefile


I am trying to make a web server in C. I am using the glib library which I include in my .c file with the syntax:

#include <glib.h>

To be able to use the library I have added the following two lines in my Makefile:

CFLAGS = 'pkg-config --cflags glib-2.0'
LDLIBS = 'pkg-config --libs glib-2.0'

But when I compile from the Shell I get the following error messages

gcc 'pkg-config --cflags glib-2.0'    httpd.c  'pkg-config --libs glib-2.0' -o httpd
gcc: error: pkg-config --cflags glib-2.0: No such file or directory
gcc: error: pkg-config --libs glib-2.0: No such file or directory
make: *** [httpd] Error 1

Is there anyone who knows a solution to this problem?


Solution

  • pkg-config is a tool meant to print needed CFLAGS and LIBS to standard out, so I see kind of a "double error" here:

    1. What you probably read was giving a parameter like CFLAGS = `pkg-config --cflags glib-2.0` to make. Note the backticks here, they tell the shell to run a command and replace the whole construct with the output of that command (alternate syntax for shells is $()).

    2. Even with backticks, this wouldn't work inside a Makefile which has different syntax from sh. The corresponding construct in GNU make is $(shell ), so just write CFLAGS = $(shell pkg-config --cflags glib-2.0).