Linux For Cynics One user's experiences with Linux


Using pkg-config to Find Library Names

If you are trying to use a library in your program, of course you need to link with it. How do you know what it's called? Use pkg-config! From the pkg-config man page

The pkg-config program is used to retrieve information about installed libraries in the system. It is typically used to compile and link against one or more libraries. Here is a typical usage scenario in a Makefile:

program: program.c
cc program.c $(pkg-config --cflags --libs gnomeui)

Here are some common usages:

List all installed libraries:

pkg-config --list-all

List what compiler flags you need for somelib:

pkg-config --cflags somelib

List what linker flags you need for somelib:

pkg-config --libs somelib

Then there's also --static if you're compiling statically.

pkg-config outputs in a format that is ready for gcc, so you can use backticks to run it and get the output when invoking gcc on the command line:

gcc program.c `pkg-config --cflags --libs zlib`

Or if you use a makefile you can capture it in variables:

LIBS := $(shell pkg-config --libs zlib)
CFLAGS := $(shell pkg-config --clfags zlib)