Skip to content

Find as External Package

If you have the library installed on your system, you can call find_package() from your CMake build script.

1
find_package(Matplot++ REQUIRED)

When creating your executable, link the library to the targets you want:

1
2
add_executable(my_target main.cpp)
target_link_libraries(my_target PUBLIC Matplot++::matplot)

Then add this header to your source files:

1
#include <matplot/matplot.h>

You can see a complete example in test/integration/CMakeLists.txt.

CMake should be able to locate the Matplot++Config.cmake script automatically if you installed the library under /usr/local/ (Linux / Mac OS). Unfortunately, there is no easy default directory for find_package on Windows.

Default directories

By default, the library is likely to be in /usr/local/ (Linux / Mac OS) or C:/Program Files/ (Windows). The installer will try to find the directory where you usually keep your libraries but that's not always perfect.

Finding packages on Windows

Unfortunately, CMake does not have a single default directory for packages on Windows like /usr/local/lib. If CMake cannot find Matplot++ on Windows or if you installed the library outside the default directory on Linux/Mac OS, there are a few options:

  • Environment Variables: The most reliable way to set this default directory is through environment variables. You can create an environment variable MATPLOTPP_DIR and then add $ENV{MATPLOTPP_DIR} to the HINTS section of the find_package command. This tends to be more convenient than requiring the path on the command line every time. Starting with version 3.12, CMake now implicitly considers the <PackageName>_Root environment variable a HINT for every find_package call.
  • Package Registry: CMake offers the Package Registry as an alternative mechanism for finding package locations. CMake maintains a list of package information in the Windows registry under HKEY_CURRENT_USER\Software\Kitware\CMake\Packages\.
  • Append CMAKE_MODULE_PATH: You can append more directories to CMAKE_MODULE_PATH with something like list(APPEND CMAKE_MODULE_PATH "C:\\Program Files\\matplotplusplus 1.0.1"). CMAKE_MODULE_PATH is a list of search paths for CMake modules to be loaded by the include() or find_package() commands.
  • Set the DIR variable directly: Directly set the Matplot++_DIR variable with something like set(Matplot++_DIR "C:\\Program Files\\matplotplusplus 1.0.1\\lib\\cmake\\Matplot++"). This might be good enough for small local projects but it is hard-coding the directory in your build script. When your library gets out of your local environment, you need to choose one of the other options above (better) or make this variable an option and require the user to provide the directory on the command line every time (worse).