You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu下C++调用matplotlibcpp.h遇Python.h缺失及编译错误求助

Ubuntu下VS Code使用matplotlibcpp.h编译报错解决指南

问题概述

在Ubuntu系统中用VS Code编写C++代码,通过matplotlibcpp.h绘图时遇到两个问题:

  • 安装python3-dev、python2-dev后仍提示找不到Python.h
  • 修改c_cpp_properties.json后编辑器无报错,但编译时出现Windows路径C:\msys64\mingw64\bin\g++.exe不存在的错误;直接用g++命令编译又回到Python.h找不到的问题

解决步骤

1. 修复VS Code编译配置(解决Windows编译器路径错误)

VS Code默认编译任务可能误配置了Windows下的MinGW编译器,需切换到Ubuntu系统自带的g++:

  • 按Ctrl+Shift+B打开编译任务面板,选择创建tasks.json文件,再选择g++ 生成活动文件
  • 修改tasks.json中的command字段为系统g++路径(一般是/usr/bin/g++),并在args中添加Python依赖的编译参数,示例配置如下:
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ 生成活动文件",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "$(python3-config --includes)",
                "$(python3-config --ldflags)"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "编译器: /usr/bin/g++"
        }
    ]
}

2. 直接编译的正确命令(解决Python.h找不到)

手动执行g++编译时,必须指定Python的头文件路径和链接库,使用python3-config自动获取依赖参数:

g++ -o Plots "/home/abhiram/Documents/Project Masters/Program files/Frequency noise Measurement/Plots.cpp" $(python3-config --includes) $(python3-config --ldflags)

若坚持使用Python2,将命令中的python3-config替换为python2-config即可,但不推荐(Python2已停止维护)

3. 确认Python依赖安装

确保已安装numpy和matplotlib(matplotlibcpp.h依赖这两个库):

pip3 install numpy matplotlib

4. 优化编辑器智能提示(可选)

若编辑器仍有红色报错,修改c_cpp_properties.json(按Ctrl+Shift+P,输入C/C++: Edit Configurations (JSON)),添加Python头文件路径,示例:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/include/python3.10",  // 替换为你的Python版本路径
                "/usr/include/python3.10/numpy"
            ],
            "compilerPath": "/usr/bin/g++",
            "cppStandard": "c++17",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

可通过python3 --version查看系统Python版本,替换路径中的版本号

修正后的测试代码

#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>  // 补充原代码缺失的vector头文件
#include "matplotlibcpp.h"
using namespace std;

namespace plt = matplotlibcpp;

// 定义sinc函数
double sinc(double x) {
    if (x == 0) {
        return 1.0;
    } else {
        return sin(x) / x;
    }
}

int main() {
    // 生成第一个图的数据
    constexpr int n_points = 1000;
    double frequencies[n_points];
    double spectrum[n_points];
    double bandwidth = 1 / (2 * M_PI * 1e-12 * 4700);
    for (int i = 0; i < n_points; i++) {
        frequencies[i] = i * 200e6 / (n_points - 1);
        spectrum[i] = abs(sinc(frequencies[i] / bandwidth));
    }
    double max_spectrum = *max_element(spectrum, spectrum + n_points);
    for (int i = 0; i < n_points; i++) {
        spectrum[i] /= max_spectrum;
    }

    // 生成第二个图的数据
    constexpr int n_gains = 1000;
    double gains[n_gains];
    double noise_factor[n_gains];
    for (int i = 0; i < n_gains; i++) {
        gains[i] = -20 + i * 40 / (n_gains - 1);
        double noise_figure = pow(10, gains[i] / 10);
        noise_factor[i] = (noise_figure - 1) / noise_figure;
    }

    // 绘制第一个图
    plt::loglog(vector<double>(frequencies, frequencies + n_points), vector<double>(spectrum, spectrum + n_points));
    plt::title("带通滤波器的频率响应");
    plt::xlabel("频率 (MHz)");
    plt::ylabel("归一化幅度");
    plt::grid(true);
    plt::show();

    // 关闭第一个图再显示第二个图
    plt::clf();

    // 绘制第二个图
    plt::loglog(vector<double>(gains, gains + n_gains), vector<double>(noise_factor, noise_factor + n_gains));
    plt::title("噪声系数随增益的变化");
    plt::xlabel("增益 (dB)");
    plt::ylabel("噪声系数");
    plt::grid(true);
    plt::show();

    return 0;
}

内容的提问来源于stack exchange,提问作者Abhiram C D

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 18:33:13