我很难找到一个不错的 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
然后(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)".
然后,我编译并得到两个不同的错误,这些错误是经典的且不难解决:
(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)然后就可以了。