Skip to main content

How to generate a pkg-config file with cmake

pkg-config is a great way to get compiler flags for a given library. Since I'm currently implementing the rainbrurpg's meta as a library (see libwsmeta's annoucement and repository) I would like to generate a pkg-config file using cmake.

The input file

We'll start with a template file :

Name: libmylib
Description: Mylib description.
Version: 1.0

Libs: -L${libdir} @PRIVATE_LIBS@
Cflags: -I${includedir}
The @ variables will be replaced with cmake variable content : it's the process of substitution.

The cmake part

From cmake, you have to define @DEST_DIR@ and @PRIVATE_LIBS@.

This will set the correct installation prefix to CMAKE_INSTALL_PREFIX.

Now, imagine you have a library list in a cmake variable. This is a comma separaetd value and you need a list of compiler flags :


Generating the file

Now, the last part, the file generation. And here's the trick. If you simply call CONFIGURE_FILE, cmake will allow substitution in both @ and ${} variables. But you need to keep ${} variables in the pkg-config output file.

CONFIGURE_FILE("" "mylib.pc" @ONLY)
The @ONLY prevents cmake substitution of ${} variables (see configure_file documentation).


Popular posts from this blog

How to make a map of variant in C++

This code is used by RainbruRPG to store any scalar types in a single class instance. It's already in use since revision 254 to save game states values when switching fullscreen or changing screen resolution.
The map class It's basically a map of boost::variant using std::string as map key. I decided to use function template setter and getter to have a standardized exception-based error handling, but you could have multiple parameter-based specialized getter/setter (void set(bool), void set(int)...).