Search code examples
c++compiler-errorsmathgl

Compile the first mathgl sample error


a test file named ml.cc, I have already installed the mathgl headers to /usr/local/include and libmgl.a to /usr/local/lib

#include <mgl2/mgl.h>
int main()
{
   mglGraph gr;
   gr.FPlot("sin(pi*x)");
   gr.WriteFrame("test.png");
   return 0;

}

"g++ -c ml.cc" can work,but "g++ ml.cc" does not work,the error is

/tmp/ccPzPcZt.o: In function `mglGraph::mglGraph(int, int, int)':
ml.cc:(.text._ZN8mglGraphC2Eiii[_ZN8mglGraphC5Eiii]+0x3b): undefined reference to     `mgl_create_graph_gl'
ml.cc:(.text._ZN8mglGraphC2Eiii[_ZN8mglGraphC5Eiii]+0x54): undefined reference to  `mgl_create_graph'
/tmp/ccPzPcZt.o: In function `mglGraph::~mglGraph()':
ml.cc:(.text._ZN8mglGraphD2Ev[_ZN8mglGraphD5Ev]+0x28): undefined reference to `mgl_use_graph'
ml.cc:(.text._ZN8mglGraphD2Ev[_ZN8mglGraphD5Ev]+0x42): undefined reference to `mgl_delete_graph'
/tmp/ccPzPcZt.o: In function `mglGraph::SetFontSize(double)':
ml.cc:(.text._ZN8mglGraph11SetFontSizeEd[_ZN8mglGraph11SetFontSizeEd]+0x2a): undefined   reference to `mgl_set_font_size'
/tmp/ccPzPcZt.o: In function `mglGraph::WriteFrame(char const*, char const*)':
ml.cc:(.text._ZN8mglGraph10WriteFrameEPKcS1_[_ZN8mglGraph10WriteFrameEPKcS1_]+0x2b):   undefined reference to `mgl_write_frame'
/tmp/ccPzPcZt.o: In function `mglGraph::FPlot(char const*, char const*, char const*)':
ml.cc:(.text._ZN8mglGraph5FPlotEPKcS1_S1_[_ZN8mglGraph5FPlotEPKcS1_S1_]+0x30): undefined    reference to `mgl_fplot'
collect2: error: ld returned 1 exit status

"g++ -L /usr/local/lib/ -l mgl ml.o" is the same error


Solution

  • I encoutered exactly the same problem and managed to solve it.

    If you did the installation properly as you said. Then you simply need to add -lmgl AT THE END of the line! Like this:

    g++ ml.o -lmgl
    

    As luke already had mentioned, you have a linking error, thus compiling is not affected. Here is an explanation for this behaviour:

    undefined reference to symbol even when nm indicates that this symbol is present in the shared library

    Hope this solved your problem.

    Peter