Skip to content

Embed with automatic download

FetchContent is a CMake command that can automatically download the Matplot++ repository. Check if you have Cmake 3.14+ installed:

1
cmake --version

Include FetchContent in your CMake build script:

1
include(FetchContent)

Declare the source for the contents:

1
2
3
FetchContent_Declare(matplotplusplus
        GIT_REPOSITORY https://github.com/alandefreitas/matplotplusplus
        GIT_TAG origin/master) # or whatever tag you want

Let CMake download the repository and include it as a subdirectory.

1
2
3
4
5
FetchContent_GetProperties(matplotplusplus)
if(NOT matplotplusplus_POPULATED)
    FetchContent_Populate(matplotplusplus)
    add_subdirectory(${matplotplusplus_SOURCE_DIR} ${matplotplusplus_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()

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)

Then add this header to your source files:

1
#include <matplot/matplot.h>

However, in larger projects, it's always recommended to look for Matplot++ with find_package before including it as a subdirectory to avoid ODR errors.