Skip to content

pkg-config

A tool to find and compiler options for libraries.


pkg-config is a tool for making sure compiler options are correct when compiling and linking software. Very often with compilers, you will need to determine the proper paths to library header files and code in order to link them to your software project. Finding the correct paths to pass to the compiler can be a challenge. pkg-config is designed to assist with that.

Using pkg-config on the HPC#

You do not need to load any environment modules to use pkg-config on the HPC. Call the pkg-config executable from the shell with the proper options, and it will work. The main options you will need are as follows:

1
2
3
4
5
# --cflags will give you the path for the -I (include) compiler option.  This is usually header files.
--cflags

# --libs will give you the path for the -L (libs) compiler option.  This is needed to link compiled libraries to new code
--libs

Basic usage looks like the following example, where LIBRARYNAME should be replaced by the name of whatever library you are looking to use (e.g., netcdf, hd5, etc).

$ pkg-config --libs --cflags LIBRARYNAME

This will output the paths in the following format:

-I/path/to/library/include/files -L/path/to/library/static/link/files -lLIBRARYNAME

This output can then be used to compile programs. You can do so by either copy/pasting the output from pkg-config or by assigning the output of pkg-config to a custom environment variable. To do the latter, refer to the example below (again, replace LIBRARYNAME with the name of whatever library you intend to use).

export COMPILER_PATHS=$(pkg-config --libs --cflags LIBRARYNAME)

This can all be wrapped up succinctly into a simple workflow for compiling programs and linking them to external libraries. Refer to the following example:

## do this step only if an environment module is needed to access the library you want to use
module load MODULENAME

## Again, should be a name like netcdf, hdf5 or some other lib
export COMPILER_PATHS=$(pkg-config --libs --cflags LIBRARYNAME)  

## Replace COMPILER with a compiler (i.e., icc or gcc) and MYPROGRAM with the filename of your code
COMPILER -c MYPROGRAM ${COMPILER_PATHS}  

## Replace compiler with icc or gcc and MYPROGRAM with the name of your code
COMPILER -o MYPROGRAM.x MYPROGRAM.o ${COMPILER_PATHS} 

Example: Compling a netCDF program using pkg-config#

In this example, we will use the simple_xy_wr.c program from UCAR. This code is available on the HPC in the /gpfs/research/software volume1:

cp /gpfs/research/software/examples/simple_xy_wr.c ~

To start, load the environment module for the gnu compiler:

$ module load gnu

Next, call the pkg-config executable with the --cflags and --libs options:

$ pkg-config --cflags --libs netcdf

Now you can use these paths in the compilation process. The best way to do that is to assign the output to an environment variable. This is done as follows:

$ export COMPILER_PATHS=$(pkg-config --cflags --libs netcdf)

You can now use that variable to compile and link the code. For example, this would look as follows:

$ gcc -c simple_xy_wr.c ${COMPILER_PATHS}
$ gcc -o simple_xy_wr.x simple_xy_wr.o ${COMPILER_PATHS}

This will compile the program. Here are the commands all together:

1
2
3
4
5
$ cp /gpfs/research/software/examples/simple_xy_wr.c ~
$ module load gnu
$ export COMPILER_PATHS=$(pkg-config --cflags --libs netcdf)
$ gcc -c simple_xy_wr.c ${COMPILER_PATHS}
$ gcc -o simple_xy_wr.x simple_xy_wr.o ${COMPILER_PATHS}

pkg-config in Makefiles#

pkg-config can be used in build automation files such as Makefiles. We can use the same example as above for this use-case.

An example Makefile using the simple_xy_wr example would have the contents:

1
2
3
4
5
6
CFLAGS=-O2 $(shell pkg-config --cflags netcdf)
LDFLAGS=$(shell pkg-config --libs netcdf)
CC=gcc

simple_xy_wr: simple_xy_wr.o
simple_xy_wr.o: simple_xy_wr.c

If you name this file Makefile and ensure that the source code (simple_xy_wr.c) is located in the same directory, you can use the command make to compile your program.

Troubleshooting#

If you encounter an error such as the following, make sure you have loaded the correct environment module, for the compiler you are using, and make sure you have typed the name of the library correctly.

Example error:

1
2
3
4
Package test was not found in the pkg-config search path.
Perhaps you should add the directory containing `test.pc'
to the PKG_CONFIG_PATH environment variable
No package 'test' found

If you encounter this or any other errors, feel free to contact RCC support.


  1. You can also download the file from the UCAR website