使用matplotlibcpp的时候在编译的时候没有报错,但在运行的时候碰到了一些错误
问题一:
sys:1: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
在main函数执行matplotlibcpp::backend("TkAgg");注意要第一个执行

问题二:
如果你解决了问题一,那你可能会遇到问题二
erminate called after throwing an instance of 'std::runtime_error' what(): Call to show() failed.

这是因为用gcc编译的时候没有包含matplotlib的路径,注意这里的matplotlib是python的库。
执行g++语句
g++ main.cpp -o main -IC:\\Python310\\include -IC:\\Lib\\site-packages\\numpy\\core\\include
-IC:Python\\Python310\\site-packages -LC:\\Python310 -lpython310
-IC:\\Python310\\include这是为了包含Python.h
-IC:\\Lib\\site-packages\\numpy\\core\\include这是为了包含numpy/arrayobject.h
-IC:\\Python\\Python310\\site-packages这是为了包含matplotlib,这是python绘图要用的
-LC:\\Python310 -lpython310这是为了包含python310.dll,-lpython310是小写的L
注意以上的路径要换成你自己的文件的位置
在解决这些问题后我成功运行了,但奇怪的是运行成功一次后,把matplotlibcpp::backend("TkAgg")以及-IC:\\Python\\Python310\\site-packages删除后仍能运行成功。

文章讲述了在使用matplotlibcpp时遇到的两个问题:一是编译时的UserWarning,需设置backend为TkAgg;二是运行时的std::runtime_error,源于未正确包含matplotlib路径。通过调整编译参数和指定Python库路径,作者成功解决问题并发现部分情况下不需backend设置仍可运行。
584

被折叠的 条评论
为什么被折叠?



