Skip to content

Commit f59b63b

Browse files
committed
Toggle building examples and fixed Windows issues
- Added MATPLOTLIBCPP_BUILD_EXAMPLES to CMake (good for disabling when importing this into other projects) - Fixed several cast warnings - Fixed Python header trying to link to debug library
1 parent 616a1ea commit f59b63b

6 files changed

Lines changed: 82 additions & 72 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
# Build
3535
/examples/build/*
36+
build/
3637

3738
# vim temp files
3839
*.sw*

CMakeLists.txt

Lines changed: 66 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ include(GNUInstallDirs)
55
set(PACKAGE_NAME matplotlib_cpp)
66
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/${PACKAGE_NAME}/cmake)
77

8+
option(MATPLOTLIBCPP_BUILD_EXAMPLES "Compile example programs" ON)
89

910
# Library target
1011
add_library(matplotlib_cpp INTERFACE)
@@ -37,72 +38,73 @@ install(
3738

3839

3940
# Examples
40-
add_executable(minimal examples/minimal.cpp)
41-
target_link_libraries(minimal PRIVATE matplotlib_cpp)
42-
set_target_properties(minimal PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
43-
44-
add_executable(basic examples/basic.cpp)
45-
target_link_libraries(basic PRIVATE matplotlib_cpp)
46-
set_target_properties(basic PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
47-
48-
add_executable(modern examples/modern.cpp)
49-
target_link_libraries(modern PRIVATE matplotlib_cpp)
50-
set_target_properties(modern PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
51-
52-
add_executable(animation examples/animation.cpp)
53-
target_link_libraries(animation PRIVATE matplotlib_cpp)
54-
set_target_properties(animation PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
55-
56-
add_executable(nonblock examples/nonblock.cpp)
57-
target_link_libraries(nonblock PRIVATE matplotlib_cpp)
58-
set_target_properties(nonblock PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
59-
60-
add_executable(xkcd examples/xkcd.cpp)
61-
target_link_libraries(xkcd PRIVATE matplotlib_cpp)
62-
set_target_properties(xkcd PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
63-
64-
add_executable(bar examples/bar.cpp)
65-
target_link_libraries(bar PRIVATE matplotlib_cpp)
66-
set_target_properties(bar PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
67-
68-
add_executable(fill_inbetween examples/fill_inbetween.cpp)
69-
target_link_libraries(fill_inbetween PRIVATE matplotlib_cpp)
70-
set_target_properties(fill_inbetween PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
71-
72-
add_executable(fill examples/fill.cpp)
73-
target_link_libraries(fill PRIVATE matplotlib_cpp)
74-
set_target_properties(fill PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
75-
76-
add_executable(update examples/update.cpp)
77-
target_link_libraries(update PRIVATE matplotlib_cpp)
78-
set_target_properties(update PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
79-
80-
add_executable(subplot2grid examples/subplot2grid.cpp)
81-
target_link_libraries(subplot2grid PRIVATE matplotlib_cpp)
82-
set_target_properties(subplot2grid PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
83-
84-
add_executable(lines3d examples/lines3d.cpp)
85-
target_link_libraries(lines3d PRIVATE matplotlib_cpp)
86-
set_target_properties(lines3d PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
87-
88-
if(Python3_NumPy_FOUND)
89-
add_executable(surface examples/surface.cpp)
90-
target_link_libraries(surface PRIVATE matplotlib_cpp)
91-
set_target_properties(surface PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
92-
93-
add_executable(colorbar examples/colorbar.cpp)
94-
target_link_libraries(colorbar PRIVATE matplotlib_cpp)
95-
set_target_properties(colorbar PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
96-
add_executable(contour examples/contour.cpp)
97-
target_link_libraries(contour PRIVATE matplotlib_cpp)
98-
set_target_properties(contour PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
99-
100-
add_executable(spy examples/spy.cpp)
101-
target_link_libraries(spy PRIVATE matplotlib_cpp)
102-
set_target_properties(spy PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
41+
if(MATPLOTLIBCPP_BUILD_EXAMPLES)
42+
add_executable(minimal examples/minimal.cpp)
43+
target_link_libraries(minimal PRIVATE matplotlib_cpp)
44+
set_target_properties(minimal PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
45+
46+
add_executable(basic examples/basic.cpp)
47+
target_link_libraries(basic PRIVATE matplotlib_cpp)
48+
set_target_properties(basic PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
49+
50+
add_executable(modern examples/modern.cpp)
51+
target_link_libraries(modern PRIVATE matplotlib_cpp)
52+
set_target_properties(modern PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
53+
54+
add_executable(animation examples/animation.cpp)
55+
target_link_libraries(animation PRIVATE matplotlib_cpp)
56+
set_target_properties(animation PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
57+
58+
add_executable(nonblock examples/nonblock.cpp)
59+
target_link_libraries(nonblock PRIVATE matplotlib_cpp)
60+
set_target_properties(nonblock PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
61+
62+
add_executable(xkcd examples/xkcd.cpp)
63+
target_link_libraries(xkcd PRIVATE matplotlib_cpp)
64+
set_target_properties(xkcd PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
65+
66+
add_executable(bar examples/bar.cpp)
67+
target_link_libraries(bar PRIVATE matplotlib_cpp)
68+
set_target_properties(bar PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
69+
70+
add_executable(fill_inbetween examples/fill_inbetween.cpp)
71+
target_link_libraries(fill_inbetween PRIVATE matplotlib_cpp)
72+
set_target_properties(fill_inbetween PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
73+
74+
add_executable(fill examples/fill.cpp)
75+
target_link_libraries(fill PRIVATE matplotlib_cpp)
76+
set_target_properties(fill PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
77+
78+
add_executable(update examples/update.cpp)
79+
target_link_libraries(update PRIVATE matplotlib_cpp)
80+
set_target_properties(update PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
81+
82+
add_executable(subplot2grid examples/subplot2grid.cpp)
83+
target_link_libraries(subplot2grid PRIVATE matplotlib_cpp)
84+
set_target_properties(subplot2grid PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
85+
86+
add_executable(lines3d examples/lines3d.cpp)
87+
target_link_libraries(lines3d PRIVATE matplotlib_cpp)
88+
set_target_properties(lines3d PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
89+
90+
if(Python3_NumPy_FOUND)
91+
add_executable(surface examples/surface.cpp)
92+
target_link_libraries(surface PRIVATE matplotlib_cpp)
93+
set_target_properties(surface PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
94+
95+
add_executable(colorbar examples/colorbar.cpp)
96+
target_link_libraries(colorbar PRIVATE matplotlib_cpp)
97+
set_target_properties(colorbar PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
98+
add_executable(contour examples/contour.cpp)
99+
target_link_libraries(contour PRIVATE matplotlib_cpp)
100+
set_target_properties(contour PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
101+
102+
add_executable(spy examples/spy.cpp)
103+
target_link_libraries(spy PRIVATE matplotlib_cpp)
104+
set_target_properties(spy PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
105+
endif()
103106
endif()
104107

105-
106108
# Install headers
107109
install(FILES
108110
"${PROJECT_SOURCE_DIR}/matplotlibcpp.h"

examples/colorbar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ int main()
1313
std::vector<float> z(ncols * nrows);
1414
for (int j=0; j<nrows; ++j) {
1515
for (int i=0; i<ncols; ++i) {
16-
z.at(ncols * j + i) = std::sin(std::hypot(i - ncols/2, j - nrows/2));
16+
z.at(ncols * j + i) = static_cast<float>(std::sin(std::hypot(i - ncols/2, j - nrows/2)));
1717
}
1818
}
1919

examples/lines3d.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ int main()
1010
double theta, r;
1111
double z_inc = 4.0/99.0; double theta_inc = (8.0 * M_PI)/99.0;
1212

13-
for (double i = 0; i < 100; i += 1) {
13+
for (int i = 0; i < 100; i += 1) {
1414
theta = -4.0 * M_PI + theta_inc*i;
1515
z.push_back(-2.0 + z_inc*i);
1616
r = z[i]*z[i] + 1;

examples/update.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ int main()
2424
std::vector<double> x, y;
2525

2626
const double w = 0.05;
27-
const double a = n/2;
27+
const double a = n/2.0;
2828

29-
for (size_t i=0; i<n; i++) {
29+
for (int i=0; i<n; i++) {
3030
x.push_back(i);
3131
y.push_back(a*sin(w*i));
3232
}

matplotlibcpp.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@
77

88
// Python headers must be included before any system headers, since
99
// they define _POSIX_C_SOURCE
10-
#include <Python.h>
10+
#if defined(_WIN32) && defined(_DEBUG)
11+
// Python.h will try to load the library debug version, but that is not installed by default.
12+
#undef _DEBUG
13+
#include <Python.h>
14+
#define _DEBUG
15+
#else
16+
#include <Python.h>
17+
#endif
1118

1219
#include <vector>
1320
#include <map>
@@ -282,7 +289,7 @@ struct _interpreter {
282289
s_python_function_colorbar = PyObject_GetAttrString(pymod, "colorbar");
283290
s_python_function_subplots_adjust = safe_import(pymod,"subplots_adjust");
284291
s_python_function_rcparams = PyObject_GetAttrString(pymod, "rcParams");
285-
s_python_function_spy = PyObject_GetAttrString(pymod, "spy");
292+
s_python_function_spy = PyObject_GetAttrString(pymod, "spy");
286293
#ifndef WITHOUT_NUMPY
287294
s_python_function_imshow = safe_import(pymod, "imshow");
288295
#endif
@@ -1277,7 +1284,7 @@ bool bar(const std::vector<Numeric> & y,
12771284
detail::_interpreter::get();
12781285

12791286
std::vector<T> x;
1280-
for (std::size_t i = 0; i < y.size(); i++) { x.push_back(i); }
1287+
for (std::size_t i = 0; i < y.size(); i++) { x.push_back(static_cast<T>(i)); }
12811288

12821289
return bar(x, y, ec, ls, lw, keywords);
12831290
}
@@ -2825,7 +2832,7 @@ struct plot_impl<std::false_type>
28252832
PyObject* pystring = PyString_FromString(format.c_str());
28262833

28272834
auto itx = begin(x), ity = begin(y);
2828-
for(size_t i = 0; i < xs; ++i) {
2835+
for(int i = 0; i < xs; ++i) {
28292836
PyList_SetItem(xlist, i, PyFloat_FromDouble(*itx++));
28302837
PyList_SetItem(ylist, i, PyFloat_FromDouble(*ity++));
28312838
}

0 commit comments

Comments
 (0)