1

I have integrated Matplotlibcpp in my C++ project from :

https://github.com/lava/matplotlib-cpp

I am using Clion to build by project and here's how I did the linkage:

    cmake_minimum_required(VERSION 3.13)
    project(beep)    
    set(CMAKE_CXX_STANDARD 14)
    set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g -ftest-coverage -fprofile-arcs" )
    link_directories(C:/MinGW/lib)
    find_package (Python3 COMPONENTS Interpreter NumPy)
    include_directories(C:/Python37/include/)
    link_libraries(C:/Python37/libs/libpython37.a Python3::NumPy)
    include_directories( kissfft kissfft/tools matplotlib-cpp )
    add_executable(beep kissfft matplotlib-cpp  kissfft/tools/kiss_fftr.c  kissfft/_kiss_fft_guts.h Beep_Generator.cpp plottingVector.h)

Here's my code:

#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
    void plotting() {
        try {
            plt::plot( {1, 2, 3, 4, 5, 6},{0,1,2,3,4,5},"test");
            plt::show();
        }
        catch (std::exception &e) {
            cout << e.what() << endl;
        }

When I run this code, I get this error of :

Call to show() failed.

When I debugged the code, I saw the problem here :

matplotlibcpp.h(Line # 1835):  res = PyObject_CallObject(detail::_interpreter::get().s_python_function_show,detail::_interpreter::get().s_python_empty_tuple);

res becomes NULL.

What's the issue here? How can I solve it?

Regards, Khubaib

Khubaib Ahmad
  • 141
  • 10

1 Answers1

1

res returning NULL indicates that a Python exception has occurred. Unfortunately the wrapper doesn't seem to probe this and just raises an arbitrary C exception.

To investigate further you need to use the Python C API directly. Double-check that an exception has been set and then print it.

if (PyErr_Occurred()) {
    PyErr_PrintEx(0);
    PyErr_Clear(); // this will reset the error indicator so you can run Python code again
}
DavidW
  • 29,336
  • 6
  • 55
  • 86
  • Okay. Let me try. – Khubaib Ahmad Apr 11 '20 at 09:24
  • Hi @DavidW, I tried this thing and yeah PyErr_Occured. But I am unable to know what type of error is there to inspect? How do I know the exact exception raised by Python? Also whenever I call "detail::_interpreter::get()" , I always get NULL. I dont have any issue regarding numpy or python.h etc. – Khubaib Ahmad Apr 14 '20 at 09:56
  • The `PyErr_PrintEx` should send it to `sys.stderr` but that may or may not be a useful location for you to read. Otherwise use something like `PyErr_GetCause` to get a Python object that you can probe (do this before print, because print clears the error indicator) – DavidW Apr 14 '20 at 14:37
  • Same issue: In my case the output says: window = tk.Tk(className="matplotlib") File "C:\Program Files\Python37\Lib\tkinter\__init__.py", line 2018, in __init__ baseName = os.path.basename(sys.argv[0]) IndexError: list index out of range – lalitm Jul 06 '20 at 10:51
  • Maybe make sure you've called [`PySys_SetArgvEx`](https://docs.python.org/3/c-api/init.html#c.PySys_SetArgvEx). I don't really know though. – DavidW Jul 06 '20 at 13:05