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

相关推荐
VirusVIP3 分钟前
Windows CMD通过adb检查触摸屏Linux驱动是否被编译
linux·运维·adb
chennalC#c.h.JA Ptho7 分钟前
ubuntu studio 系统详解
linux·运维·服务器·经验分享·ubuntu·系统安全
yt948324 小时前
Docker-基础(数据卷、自定义镜像、Compose)
运维·docker·容器
水银嘻嘻6 小时前
web 自动化之 KDT 关键字驱动详解
运维·自动化
Vone_666 小时前
node.js 邮箱验证服务器搭建
运维·服务器·node.js
丢丢丢丢丢丢~7 小时前
apache2的默认html修改
linux·运维·服务器
wusam7 小时前
Linux系统管理与编程20:Apache
linux·运维·服务器·apache·shell编程
我不想当小卡拉米7 小时前
【Linux】操作系统入门:冯诺依曼体系结构
linux·开发语言·网络·c++
ChironW7 小时前
Ubuntu 24.04 LTS系统上配置国内时间同步
linux·运维·服务器·ubuntu
TPBoreas7 小时前
排查服务器内存空间预警思路
运维·服务器