在 Visual Studio 2019 中运行 python matplotlibcpp

2024-01-22

我想为 C++ 程序安装绘图功能。我选择了matplotlibcpp由于其简单性和在线评论。但是,我无法生成图并出现许多神秘错误(我是 C++ 新手)。更新 VS 2019 的包含路径后,我能够在编译和链接时没有错误,但执行过程中列出了许多错误。

下面是一个产生错误的简单代码片段。

#include <matplotlibcpp.h>
#include <string>
using namespace std;
namespace plt = matplotlibcpp;


int main()
{
    plt::plot({ 1,2,2,4 });
    plt::show();
}

我在输出窗口中得到以下输出:

Python 致命错误:initfsencoding:无法加载文件系统 编解码器 ModuleNotFoundError:没有名为“编码”的模块

当前线程 0x000018c0(最近调用优先)

关于如何获得的任何想法matplotlibcpp在 VS 2019 中运行?

在 Windows 10 x64 上运行 VS 2019。


我很难找到一个不错的 C++ 绘图库。我决定尝试 matplotlibcpp,过了一段时间,我成功地使其在 Windows 10、Visual Studio 2019、Python 3.9 下工作。

首先,我试图让它与 conda 一起工作。由于 PYTHONHOME 变量设置有问题,我放弃了。因此,我继续使用 python (3.9) 的经典安装。

首先 (1),必须确保可以通过命令提示符或 powershell 访问 python,方法是输入:

python --version

第二(2),我安装了 numpy 和 matplotlib :

python -m pip install matplotlib
python -m pip install numpy

然后(3)我在 cpp 中创建了一个新项目,并粘贴了这些简单的行(https://github.com/lava/matplotlib-cpp https://github.com/lava/matplotlib-cpp)在新的 cpp 文件中:

#include <iostream>
#include "matplotlibcpp.h"
#include <vector>

namespace plt = matplotlibcpp;
int main() {
    std::vector<double> y = { 1, 3, 2, 4 };
    plt::plot(y);

    plt::show();

}

then (4), i downloaded the matplotlibcpp.h (https://github.com/lava/matplotlib-cpp/blob/master/matplotlibcpp.h https://github.com/lava/matplotlib-cpp/blob/master/matplotlibcpp.h) and added it in my project One .cpp and one .h

然后(5),我通过执行项目、属性进入项目的属性,然后添加 C/C++ 属性,其他包含目录: E:\Python39\include;E:\Python39\Lib\site-packages\numpy\core\include

然后(6),在链接器,常规,附加库目录中,我添加了: E:\Python39\libs

then (7) in the linker, input, additionnal dependencies, i added: E:\Python39\libs\python39.lib be sure to do not delete "%(AdditionalDependencies)". AdditionalDependencies

然后,我编译并得到两个不同的错误,这些错误是经典的且不难解决:

(8) 其中一条与 matplotlibcpp.h 中的 4 行相关:这四行上方的注释(“健全性检查”)告诉您如果出现问题,应该对它们进行注释。我做到了,它有效:错误不再出现:

(9) 编译器还抱怨一些与 c++ 标准相关的问题。我遵循网上找到的指南,只需进入属性、C/C++、语言并选择 iso C++ 17 标准。然后第二个错误就解决了。

(10) 然后我再次编译,我记得我也抱怨过 python39_d.lib 或类似的东西。我只需要在 Visual Studio 中选择“发布”而不是“调试”(并且还要确保在 X64 中)。

(11)然后,我在生成.exe的X64/release文件夹中添加了python39.dll(我还添加了python3.dll,但我认为它没用;只需要python39(如果有不同版本,则为3x) )。

(12)然后就可以了。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Visual Studio 2019 中运行 python matplotlibcpp 的相关文章