1

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

imotai
  • 2,006
  • 1
  • 12
  • 9

2 Answers2

3

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

Community
  • 1
  • 1
peter
  • 46
  • 2
0

You have a linking error. To solve this you need to link against mgl. It looks like you are trying to, but its not working.

First, I would drop the space between the l and the mgl. so

g++ -L /usr/local/lib/ -lmgl ml.o

If that doesn't work, check to see if there are any other libraries you need to link against. See if g++ is throwing errors that it cannot find mgl.

Hope that helps.

David G
  • 94,763
  • 41
  • 167
  • 253
luke
  • 1,024
  • 3
  • 11
  • 21