Try   HackMD
tags: gnuplot c++ makefile eps

透過 "gnuplot-iostream.h" 在 c++ 中使用 gnuplot

Install

sudo apt-get install libgnuplot-iostream-dev

Example

example.cpp

#include"gnuplot-iostream.h" #include<bits/stdc++.h> Gnuplot gp; using namespace std; int main(){ gp << "set term postscript eps enhance color\n"; vector<pair<double, double>> m, m1; for(int i = 0;i < 10;i++){ m.push_back(pair<double, double>(i, pow(i, 2))); m1.push_back(pair<double, double>(i, pow(i, 3))); } gp << "set output \"example.eps\"\n"; gp << "set title \"power of 2\"\n"; gp << "plot" << gp.file1d(m) << "with lines title \"square\", " << gp.file1d(m1) << "with points title \"cubic\"\n"; gp << "set output\n"; }

直接用g++編譯

g++ -o example example.cpp -lboost_system -lboost_iostreams -lboost_filesystem

用Makefile編譯

由於在make的auto compile的link時,會把LDFLAGS放在最前面,這時後會因為不需要這些東西所以沒有連結這些東西(因為需要得東西都放在後面),所以要另外寫link的部份,強制放在最後
Makefile

CC=g++
LDFLAGS+= -lboost_system -lboost_filesystem -lboost_iostreams
dep:
    gcc -lstdc++ -M *.cpp > dep
-include dep
all: example
example:example.o
    $(CC) -o $@ $^ $(LDFLAGS)

輸出的檔案

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →