From 0dc0e3ddee2a23d5b107cce42ac2ef8d8aee3462 Mon Sep 17 00:00:00 2001 From: Martin Steil Date: Wed, 7 Aug 2024 19:30:10 +0200 Subject: [PATCH] Added MWE for standalone cmake usage and .cmake script to load python --- .gitignore | 4 ++++ CMakeLists.txt | 3 ++- matplotlibcpp.cmake | 13 +++++++++++++ minimal-standalone-example/CMakeLists.txt | 8 ++++++++ minimal-standalone-example/main.cpp | 5 +++++ 5 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 matplotlibcpp.cmake create mode 100644 minimal-standalone-example/CMakeLists.txt create mode 100644 minimal-standalone-example/main.cpp diff --git a/.gitignore b/.gitignore index 1c4a1b0a..0babff96 100644 --- a/.gitignore +++ b/.gitignore @@ -33,6 +33,10 @@ # Build /examples/build/* +.cmake* + +# IDE files +.idea # vim temp files *.sw* diff --git a/CMakeLists.txt b/CMakeLists.txt index bb2decd8..0db46e4a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,11 +1,12 @@ cmake_minimum_required(VERSION 3.8 FATAL_ERROR) project(matplotlib_cpp LANGUAGES CXX) +add_subdirectory(minimal-standalone-example) + include(GNUInstallDirs) set(PACKAGE_NAME matplotlib_cpp) set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/${PACKAGE_NAME}/cmake) - # Library target add_library(matplotlib_cpp INTERFACE) target_include_directories(matplotlib_cpp diff --git a/matplotlibcpp.cmake b/matplotlibcpp.cmake new file mode 100644 index 00000000..bbfffc07 --- /dev/null +++ b/matplotlibcpp.cmake @@ -0,0 +1,13 @@ +find_package(Python3 COMPONENTS Interpreter Development REQUIRED) +find_package(Python3 COMPONENTS NumPy) + +target_link_libraries(${PROJECT_NAME} PRIVATE + Python3::Python + Python3::Module +) + +if (Python3_NumPy_FOUND) + target_link_libraries(${PROJECT_NAME} PRIVATE Python3::NumPy) +else () + target_compile_definitions(${PROJECT_NAME} WITHOUT_NUMPY) +endif () \ No newline at end of file diff --git a/minimal-standalone-example/CMakeLists.txt b/minimal-standalone-example/CMakeLists.txt new file mode 100644 index 00000000..c0c80300 --- /dev/null +++ b/minimal-standalone-example/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.25) +project(smwe) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(smwe main.cpp) + +include(../matplotlibcpp.cmake) diff --git a/minimal-standalone-example/main.cpp b/minimal-standalone-example/main.cpp new file mode 100644 index 00000000..c728a93f --- /dev/null +++ b/minimal-standalone-example/main.cpp @@ -0,0 +1,5 @@ +#include "../matplotlibcpp.h" +int main() { + matplotlibcpp::plot({1,3,2,4}); + matplotlibcpp::show(); +} \ No newline at end of file