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

相关推荐
likfishdn几秒前
Linux的文件与目录管理
linux·运维·服务器
chian-ocean20 分钟前
Linux 文件缓冲区:高效数据访问的幕后推手
linux·运维·服务器
加油=^_^=21 分钟前
【Linux】进程优先级 | 进程调度(三)
linux·运维·服务器
若云止水31 分钟前
Ubuntu 下 nginx-1.24.0 源码分析 - ngx_init_cycle 函数 - 详解(4)
数据库·nginx·ubuntu
吃汤圆的抹香鲸1 小时前
GoLand 安装包 绿色版 Win,Mac,Linux 包含IntelliJ全家桶 专为Go语言设计的集成开发环境(IDE)
linux·windows·macos·go·intellij-idea·go1.19
追寻光1 小时前
Cannot find a valid baseurl for repo: base/7/x86_64
linux
元气满满的热码式1 小时前
Docker数据卷操作实战
linux·运维·docker·云原生·容器
陆沙1 小时前
deepseek-r1-centos-本地服务器配置方法
linux·服务器·centos
北京耐用通信1 小时前
航空装配自动化神器Ethercat转profient网关搭配机器人精准控制
运维·机器人·自动化
吃汤圆的抹香鲸1 小时前
Rider 安装包 绿色版 Win/Mac/Linux 适合.NET和游戏开发者使用 2025全栈开发终极指南:从零配置到企业级实战
linux·运维·windows·sql·游戏·macos·.net