--- tags: Tools --- ###### tags: `gnuplot` `c++` `makefile` `eps` # 透過 "gnuplot-iostream.h" 在 c++ 中使用 gnuplot ## Install ```shell= sudo apt-get install libgnuplot-iostream-dev ``` ## Example `example.cpp` ```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++編譯 ```shell= 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) ``` ### 輸出的檔案 ![](https://i.imgur.com/1DNAYiC.png)