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

相关推荐
chen9458 分钟前
k8s集群部署vector日志采集器
运维
chen94514 分钟前
aws ec2部署harbor,使用s3存储
运维
轻松Ai享生活4 小时前
5 节课深入学习Linux Cgroups
linux
christine-rr5 小时前
linux常用命令(4)——压缩命令
linux·服务器·redis
三坛海会大神5555 小时前
LVS与Keepalived详解(二)LVS负载均衡实现实操
linux·负载均衡·lvs
東雪蓮☆5 小时前
深入理解 LVS-DR 模式与 Keepalived 高可用集群
linux·运维·服务器·lvs
qq_264220895 小时前
LVS负载均衡群集和LVS+Keepalived群集
运维·负载均衡·lvs
乌萨奇也要立志学C++6 小时前
【Linux】进程概念(二):进程查看与 fork 初探
linux·运维·服务器
雨落Liy6 小时前
Nginx 从入门到进阶:反向代理、负载均衡与高性能实战指南
运维·nginx·负载均衡
Yyyy4826 小时前
Nginx负载均衡集群实验步骤
运维·nginx·负载均衡