0

I'm trying to get Matplotlib wrapper to work on wxDevC++

The code

#include <cstdlib>
#include <iostream>

#include "matplotlib-cpp/matplotlibcpp.h"
#include <vector>
namespace plt=matplotlibcpp;
using namespace std;

int main(int argc, char *argv[])
{
    
    std::vector<double> y={1,3,2,4};
    plt::plot(y);
    plt::savefig("minimal.pdf");

    cout << "Press the enter key to continue ...";
    cin.get();
    return EXIT_SUCCESS;
}

I use Win 7, I have python 27 and Python 38. It keeps telling me that there is no Python.h file. I've no idea how to fix this.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Some Student
  • 134
  • 6
  • Do you have a Python.h file? [Here](https://github.com/lava/matplotlib-cpp/blob/master/matplotlibcpp.h) in line 5 you can see that include. –  Mar 30 '21 at 20:47
  • Yes I do have that file – Some Student Mar 30 '21 at 20:48
  • What's the path to the file? How do you build your project? Do you add that include path? Have you read the [documentation](https://github.com/lava/matplotlib-cpp), especially the section "Installation"? –  Mar 30 '21 at 20:50
  • Yes, you're right. I included the path but now getting this "This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options." – Some Student Mar 30 '21 at 20:53
  • Where did you get your compiler from and what compiler/version do you use? C++0x is the old name for C++11, a 10 year old standard. Your compiler uses C++03 or older as default. It uses an 18 year old standard. You should probably upgrade your compiler. I recommend to use C++20 or C++17 at least. –  Mar 30 '21 at 20:56
  • [DevC++ 6.0](https://en.wikipedia.org/wiki/Dev-C%2B%2B) is released with GCC 9.2.0 and should support C++11 and partially C++20. –  Mar 30 '21 at 21:02
  • is matplotlib even a good idea or c++ or should i find another library for data visualisation? – Some Student Mar 30 '21 at 21:03
  • I only can say you shouldn't use an ancient toolset from 15 years ago. _"is matplotlib even a good idea or c++ or should i find another library for data visualisation?"_ This question is very opinion based and depends on your use-case. I like matplotlib with C++ and Python. I like C++. In my opinion that's a good combination. C++ is a complex language. Other languages like Python are good for a quick and dirty prototype but I prefer C++ for production. I often use a script language for a proof of concept and a compiled language for implementation. –  Mar 30 '21 at 21:09

1 Answers1

0

You have to set the include path for Pathon.h in your build configuration and you have to use at least C++11.

You can set the C++ standard in GCC and compatible compilers with the compiler option -std=c++11 or in older versions with -std=c++0x. For the GNU variants you can use -std=gnu++11 resp. -std=gnu++0x.

  • I added the compile option -std=c++11 and it worked. Now I'm getting a list of errors like main.cpp:(.text+0x12): undefined reference to `__imp_PyImport_ImportModule' main.cpp:(.text+0x46): undefined reference to `__imp_PyObject_GetAttrString' main.cpp:(.text+0x8d): undefined reference to `__imp_PyExc_AttributeError' main.cpp:(.text+0xa1): undefined reference to `__imp_PyErr_SetString' main.cpp:(.text+0xbc): undefined reference to `__imp_PyCObject_Type' main.cpp:(.text+0xc8): undefined reference to `__imp_PyExc_RuntimeError' – Some Student Mar 31 '21 at 05:14
  • @Peter Have you read the [documentation](https://github.com/lava/matplotlib-cpp), especially the section "Installation"? Do you link libpython? –  Mar 31 '21 at 07:05
  • I don't even understand what "link libpython" means. Why can't I just make a simple plot in C++? Do I really have to use the disuisting python? I dont understand why would someone make this so complicated. – Some Student Mar 31 '21 at 10:13
  • In almost all programming languages you have the language features (.e.g in C++ `for` loops, `if` conditions, ...), the core library (.e.g. in C++ `std::vector`, `std::string`, ...) and additional third-party libraries. Someone invested time and energy to write the library `matplotlib` and shared it with you for free. But this person wasn't interested in reinventing the wheel and used already available functionality from `libpython`. To link `matplotlib` and use it you have to link `libpython`. To make it simpler you can use a package manager like `vcpkg` like described in the documentation. –  Mar 31 '21 at 10:18
  • @Peter If you don't know how to use external libraries in C++: https://stackoverflow.com/questions/10358745/how-to-use-libraries In the accepted answer case 2 describes how to link a compiled library. –  Mar 31 '21 at 10:22
  • I used vcpkg following the documentation. It doesnt work. I have no idea what to do, I am trying to learn sth new but i keep seeing exoteric terminology everywhere and I don't know what it all means. I dont know what "package manager" means or "libplot linking". – Some Student Mar 31 '21 at 10:24
  • What does "link a compiled library" mean? I am not trolling, this really doesnt mean anything to someone who is new ar this. – Some Student Mar 31 '21 at 10:24
  • @Peter As described in the accepted answer a library can be linked at compile time (as source code) or at link time (as compiled library). A compiled library is a collection of functions and other code that can be used in other code that is already compiled an can be linked. A _"package manager"_ is a tool that handles dependencies for you. Usually you won't write all code yourself but you use other libraries/dependencies. `libplot` is a library containing functions for plotting and you can link it into your project. That sounds complex but is necessary to use C++ at a professional level. –  Mar 31 '21 at 10:27
  • I give up. I decided to design and write my own library for data visualisation. Thanks for all advice – Some Student Apr 02 '21 at 21:06
  • @Peter Are telling me that you are not able to link a library to your project but you want to write your own library? How would this solve your problem. At the end you will have to link your own library to your project. –  Apr 02 '21 at 21:16