在vim中可以直接使用以下命令:
g++ 文件名.cpp -std=c++11 -I/usr/include/python2.7 -lpython2.7
进行编译生成可执行文件a.out并能成功运行。
但是在vscode中编译时 由于matplotlib不是系统自带的库文件,导致vscode正常编译找不到相应的文件,会出现包含头文件时不识别,以及未定义的引用等错误。
解决方案:首先确保系统安装了python2.7(或其他版本),numpy,并且下载好了matplotlibcpp.h头文件(https://github.com/lava/matplotlib-cpp)。
编译之前需要先进行配置,添加自己库文件所在的路径,链接对应库文件:
1.配置tasks.json: 将"-I","/usr/include/python2.7","-lpython2.7"添加到args中,如下所示:
2.配置c_cpp_properties.json:将自己安装的python2.7,numpy和下载的matplotlibcpp.h头文件所在的路径添加到includePath中,如下所示:
然后就可以正常编译并运行了,结果如下:
测试所用代码:
#include "matplotlibcpp.h"
#include <cmath>
#include <vector>
namespace plt = matplotlibcpp;
int main()
{
// Prepare data.
std::vector<int> x = {1, 2, 3, 4, 4, 5, 6};
std::vector<int> y = {1, 3, 2, 4, 4, 5, 6};
// Set the size of output image to 1200x780 pixels
plt::figure_size(1200, 780);
// Plot line from given x and y data. Color is selected automatically.
plt::plot(x, y);
// Set x-axis to interval [0,1000000]
//plt::xlim(0, 1000*1000);
// Add graph title
plt::title("Sample figure");
// Enable legend.
//plt::legend();
// Save the image (file format is determined by the extension)
plt::save("./basic.png");
plt::show();
}
参考文章:https://blog.csdn.net/weixin_43769166/article/details/118365416