【问题标题】:Matplotlib wrapper for c++用于 C++ 的 Matplotlib 包装器
【发布时间】:2021-06-26 21:38:28
【问题描述】:

我正在尝试让 Matplotlib 包装器在 wxDevC++ 上工作

代码

#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;
}

我使用的是 Win 7,我有 python 27 和 Python 38。它一直告诉我没有 Python.h 文件。我不知道如何解决这个问题。

【问题讨论】:

  • 你有 Python.h 文件吗? Here 在第 5 行你可以看到包含。
  • 是的,我有那个文件
  • 文件的路径是什么?你如何构建你的项目?你添加那个包含路径吗?你读过documentation,尤其是“安装”部分吗?
  • 是的,你是对的。我包含了路径,但现在得到了这个“这个文件需要编译器和库支持即将推出的 ISO C++ 标准 C++0x。这个支持目前是实验性的,必须使用 -std=c++0x 或 -std 启用=gnu++0x 编译器选项。”
  • 您从哪里获得编译器以及您使用的编译器/版本是什么? C++0x 是 C++11 的旧名称,这是一个 10 年前的标准。您的编译器默认使用 C++03 或更早版本。它使用 18 年的标准。您可能应该升级您的编译器。我建议至少使用 C++20 或 C++17。

标签: c++ python-2.7 matplotlib compilation windows-7


【解决方案1】:

您必须在构建配置中设置 Pathon.h 的包含路径,并且您必须至少使用 C++11。

您可以使用编译器选项 -std=c++11 或在旧版本中使用 -std=c++0x 在 GCC 和兼容编译器中设置 C++ 标准。对于 GNU 变体,您可以使用 -std=gnu++11 resp。 -std=gnu++0x.

【讨论】:

  • 我添加了编译选项 -std=c++11 并且它起作用了。现在我得到一个错误列表,例如 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):​​ 未定义引用__imp_PyCObject_Type' main.cpp:(.text+0xc8): undefined reference to __imp_PyExc_RuntimeError'
  • @Peter 你读过documentation,尤其是“安装”部分吗?你链接 libpython 吗?
  • 我什至不明白“链接 libpython”是什么意思。为什么我不能在 C++ 中制作一个简单的情节?我真的必须使用 disuisting python 吗?我不明白为什么有人会把这个弄得这么复杂。
  • 在几乎所有编程语言中,您都拥有语言特性(例如在 C++ 中 for 循环、if 条件...)、核心库(例如在 C++ 中 std::vectorstd::string, ...) 和其他第三方库。有人花时间和精力写了库matplotlib,免费分享给大家。但是这个人对重新发明轮子不感兴趣,而是使用了libpython 中已有的功能。要链接matplotlib 并使用它,您必须链接libpython。为了使其更简单,您可以使用像 vcpkg 这样的包管理器,如文档中所述。
  • @Peter 如果您不知道如何在 C++ 中使用外部库:stackoverflow.com/questions/10358745/how-to-use-libraries 在接受的答案案例 2 中描述了如何链接已编译的库。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-19
  • 2010-12-28
  • 1970-01-01
  • 1970-01-01
  • 2011-05-31
  • 2020-02-23
  • 2011-10-12
相关资源
最近更新 更多