C++中调用matplotlibcpp.h画图
一、Python配置
??这里我安装的是python2.7 ,matplotlib 库里面需要用到python 的numpy 和matplotlib 库,可以采用以下命令安装python 和python 的库。
sudo apt-get install python-dev
sudo apt-get install python-numpy
sudo apt-get install python-matplotlib
二、CMakeLists.txt文件配置
cmake_minimum_required(VERSION 3.10)
project(ProjectName)
set(CMAKE_CXX_STANDARD 11)
#指定库路径
file(GLOB_RECURSE PYTHON2.7_LIB "/usr/lib/python2.7/config-x86_64-linux-gnu/*.so")
#指定头文件路径
set(PYTHON2.7_INLCUDE_DIRS "/usr/include/python2.7")
#添加头文件到工程
include_directories(include
${PYTHON2.7_INLCUDE_DIRS}
)
#这样配置可以使得matplotlib.h中找到python.h文件,通常这样设置就可以。
add_executable(ProjectName ${DIRS_SRCS})
#添加库文件到工程
target_link_libraries(ProjectName ${PYTHON2.7_LIB})
三、错误修改
??在matplotlibcpp.h 中第353和355行可能会出现以下错误: ??需要把353行和355行做以下修改: ??修改完毕后,即可运行,但是如果采用VScode开发,VScode可能会报错。
四、VScode配置
??此时需要修改VScode中的c_cpp_properties.json 文件,修改如下:
{
"configurations": [
{
"browse": {
"databaseFilename": "",
"limitSymbolsToIncludedHeaders": true
},
"includePath": [
"/opt/ros/kinetic/include/**",
"/home/he/catkin_ws/src/learning_topic/include/**",
"/usr/include/**",
"/usr/include/python2.7",
"${workspaceFolder}/devel/include"
],
"name": "ROS",
"intelliSenseMode": "gcc-x64",
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu11",
"cppStandard": "c++14"
}
],
"version": 4
}
五、画图的例子
#include <iostream>
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
int main() {
plt::plot({1,2,3,4});
plt::show();
return 0;
}
??得到的结果如图:
参考文章
[1].https://blog.csdn.net/ABC_ORANGE/article/details/104601195 [2].https://blog.csdn.net/weixin_43142797/article/details/102505385 [3].https://mangoroom.cn/cpp/call-matplotlib-on-cpp.html [4].https://www.cnblogs.com/linlongyu/p/10361985.html
|