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:
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).
This will output the paths in the following format:
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,
LIBRARYNAME with the name of whatever library you intend to use).
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:
Example: Compling a netCDF program using pkg-config#
Next, call the
pkg-config executable with the
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:
You can now use that variable to compile and link the code. For example, this would look as follows:
This will compile the program. Here are the commands all together:
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
Makefile using the
simple_xy_wr example would have the contents:
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.
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.
If you encounter this or any other errors, feel free to contact RCC support.