引言
欢迎使用 matplotlib-cpp,它可能是最简单的 C++ 绘图库。 它的构建类似于 Matlab 和 matplotlib 使用的绘图 API。
如上其 Readme 介绍:
https://github.com/lava/matplotlib-cpp
第一步git clone source code
1.直接编译使用
# 源码方式默认编译安装
git clone https://github.com/lava/matplotlib-cpp.git
cd matplotlib-cpp/
mkdir build && cd build
cmake ..
make
sudo make install
# 安装依赖
sudo apt-get install python-matplotlib python-numpy python2.7-dev
预配置环境已经完成,下面演示一个例子来自官方介绍:
// 创建文件
vim main.cpp
//复制下面代码
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
int main() {
plt::plot({1,3,2,4});
plt::show();
}
// g++ 编译
g++ main.cpp -std=c++11 -I/usr/include/python2.7 -lpython2.7
// 执行生成程序
./a.out
2. 使用Cmake,编写CMakeLists.txt
详细看这条指令g++ main.cpp -std=c++11 -I/usr/include/python2.7 -lpython2.7
他提示了我们如何编写CMakeLists文件。
-l 查找头文件 /usr/include/python2.7
-l 查找库文件 python2.7
因此CMakeList.txt文件内容这样编写:
# matplotlib-cpp
find_package(PythonLibs REQUIRED)
set(PYTHON_INCLUDE_DIRS /usr/include/python2.7)
set(PYTHON_LIBRARIES /usr/lib/x86_64-linux-gnu/libpython2.7.so)
include_directories(${PYTHON_INCLUDE_DIRS})
....
target_link_libraries(xxx
${PYTHON_LIBRARIES})