8000 fix segfault on exit with python3 by amadeus84 · Pull Request #301 · lava/matplotlib-cpp · GitHub
[go: up one dir, main page]

Skip to content

fix segfault on exit with python3 #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
examples: use std::span (C++20) to plot 2D C-style arrays
  • Loading branch information
amadeus84 committed Mar 13, 2022
commit 62558776fe35eac6591848f80080d53827609b05
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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
Expand Down Expand Up @@ -100,6 +99,11 @@ if(Python3_NumPy_FOUND)
add_executable(spy examples/spy.cpp)
target_link_libraries(spy PRIVATE matplotlib_cpp)
set_target_properties(spy PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")

add_executable(span examples/span.cpp)
target_link_libraries(span PRIVATE matplotlib_cpp)
target_compile_features(span PRIVATE cxx_std_20)
set_target_properties(span PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
endif()


Expand Down
34 changes: 34 additions & 0 deletions examples/span.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// g++ -std=c++20 -g -Wall -o span $(python-config --includes) span.cpp $(python-config --ldflags --embed)
//
#include "../matplotlibcpp.h"

#include <span>

using namespace std;
namespace plt = matplotlibcpp;

int main()
{
// C-style arrays with multiple rows
time_t t[]={1, 2, 3, 4};
int data[]={
3, 1, 4, 5,
5, 4, 1, 3,
3, 3, 3, 3
};

size_t n=sizeof(t)/sizeof(decltype(t[0]));
size_t m=sizeof(data)/sizeof(decltype(data[0]));

// Use std::span to pass data chunk to plot(), without copying it.
// Unfortunately, plt::plot() makes an internal copy of both x and y
// before passing them to python.
for (size_t offset=0; offset<m; offset+=n)
plt::plot(t, std::span {data+offset, n}, "");
plt::show();

plt::detail::_interpreter::kill();

return 0;
}
0