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)全过程排错指南

相关推荐
知白守黑26721 分钟前
Linux磁盘阵列
linux·运维·服务器
维尔切2 小时前
Linux中基于Centos7使用lamp架构搭建个人论坛(wordpress)
linux·运维·架构
tan77º2 小时前
【项目】分布式Json-RPC框架 - 项目介绍与前置知识准备
linux·网络·分布式·网络协议·tcp/ip·rpc·json
丘大梨3 小时前
QT 基础聊天应用项目文档
运维·数据库·系统架构
怀刃3 小时前
C# 内存监控怎么定位问题
运维
TPBoreas3 小时前
Jenkins启动端口修改失败查找日志
运维·服务器·jenkins
正在努力的小河5 小时前
Linux设备树简介
linux·运维·服务器
荣光波比5 小时前
Linux(十一)——LVM磁盘配额整理
linux·运维·云计算
小晶晶京京5 小时前
day35-负载均衡
运维·网络·网络协议·学习·负载均衡
LLLLYYYRRRRRTT5 小时前
WordPress (LNMP 架构) 一键部署 Playbook
linux·架构·ansible·mariadb