How to Find Installed Packages I

Let's learn how to find installed package variables in CMake.

Let's say we have decided to up our game with network communication or storing data at rest. Plaintext files, JSON, or even good old XML won't do. We want to serialize our data straight to binary format, preferably with a library known very well in the industry—say, protocol buffers (Protobuf) from Google. We find the documentation, install the dependencies in the system, and now what? How do we actually tell CMake to find and use this external dependency we're introducing? Luckily, there's a find_package() command. It works like a charm in most cases.

The find_package() command

Let's rewind and start by setting the scene: we have to install the dependencies we want to use because find_package(), as the name suggests, is only about discovering packages in a system. We're assuming that dependencies are already installed or that users of our solution know how to install specific, necessary dependencies when prompted.

In the case of Protobuf, the situation is fairly straightforward: we can either download, compile, and install the library ourselves from the official repository or use the package manager in our operating system. The commands to install the Protobuf library and compiler are as follows:

Get hands-on with 1200+ tech skills courses.