ubuntu中安装matplotcpp绘图

安装python必要包

在 Ubuntu 上,你需要安装 python-matplotlib 包,该包只是使得python环境中有 matplotlib 库,如果已经安装可以跳过,python3-dev 包比较重要,其中包括必要的头文件。可以使用以下命令安装:

bash 复制代码
sudo apt  update
sudo apt-get install python-matplotlib python3-dev

具体而言python3-dev 包含了 Python 3 的开发头文件、静态库和编译工具,主要用于编译和构建 C/C++ 扩展模块或其他依赖 Python 3 的底层代码。具体包括:

  1. Python 3 的头文件 :提供用于开发扩展的头文件(如 Python.h),这些文件使得 C 或 C++ 程序能够与 Python 3 进行交互。

  2. 静态库:提供了 Python 3 的静态链接库文件,允许开发者将 Python 3 嵌入到 C/C++ 程序中。

  3. 开发工具和配置文件 :包括用于构建 Python 扩展模块的 Makefile 和其他构建工具,支持使用 setup.py 脚本来编译 Python 模块。

简而言之,python3-dev 是为需要与 Python 3 交互的 C/C++ 扩展或其他底层开发工具提供的支持包。

下载matplotcpp代码

bash 复制代码
git clone https://mirror.ghproxy.com/https://github.com/lava/matplotlib-cpp.git

源码编译

bash 复制代码
git clone https://github.com/lava/matplotlib-cpp.git
cd matplotlib-cpp
mkdir build && cd build
cmake ..
make
sudo make install

设置cmakelists.txt

这里注意对pythonlibs的包含;否则会报Python.h头文件错误;

bash 复制代码
cmake_minimum_required(VERSION 3.20)
project(cpp_plot)

find_package(PythonLibs  REQUIRED)

include_directories(${PYTHON_INCLUDE_DIRS})
# 这里踩了一次坑,整了一晚上
# find_package查找是在linux系统级别进行查找,如果这里你的python想要用conda虚拟环境中的,需要对这里的
# ${PYTHON_INCLUDE_DIRS}与 ${PYTHON_LIBRARIES}进行重新set,指定到conda环境中的路径;
# 这样在vscode中,cmake在构建项目时,才会使用虚拟环境中的python以及相关的包;
add_executable(out_put  test.cpp)

target_link_libraries(out_put  ${PYTHON_LIBRARIES})

简单的例子

cpp 复制代码
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
int main() {
    plt::plot({1,3,2,4});
    plt::show();
}

参考链接:
ubuntu下C++调用matplotlibcpp进行画图(超详细)

Cannot open include file: 'Python.h'无法打开包含文件

Windows安装C++绘图工具(matplotlib-cpp)全过程排错指南

相关推荐
乐维_lwops7 分钟前
2025年运维监控建设重点是什么?
运维·监控系统·it监控
二进制coder14 分钟前
Linux RTC 驱动子系统详细实现方案
linux·运维·实时音视频
淮北49423 分钟前
linux系统学习(10.shell基础)
linux·运维·服务器·学习
视觉震撼38 分钟前
RDP登录事件详细溯源分析脚本(兼容Windows PowerShell版本)
运维·网络·windows·网络安全·网络攻击模型·安全威胁分析·安全架构
Lolo_fi1 小时前
记录Fedora43上安装向日葵
linux
noravinsc1 小时前
两台 centos 7.9 部署 pbs version 18.1.4 集群
linux·运维·centos
你的微笑,乱了夏天1 小时前
linux centos常用命令整理
linux·运维·centos
故林丶1 小时前
【Linux】CentOS 7.8 Docker + Docker Compose 安装
linux·docker·centos
刘一说1 小时前
CentOS系统一键安装Memcached教程
linux·centos·memcached
网硕互联的小客服2 小时前
Windows2008 如何禁用FSO?
运维·服务器·网络·windows·安全