编译第一个 mathgl 示例错误

Compile the first mathgl sample error

本文关键字:错误 mathgl 第一个 编译      更新时间:2023-10-16

一个名为 ml.cc 的测试文件,我已经将mathgl标头安装到/usr/local/include和libmgl.a到/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"可以工作,但"g++ ml.cc"不起作用,错误是

/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"是相同的错误

我解决了完全相同的问题并设法解决了它。

如果您按照您所说的正确进行了安装。然后你只需要在行尾添加 -lmgl!喜欢这个:

g++ ml.o -lmgl

正如 luke 已经提到的,您有一个链接错误,因此编译不受影响。以下是此行为的解释:

对符号的未定义引用,即使 nm 指示此符号存在于共享库中

希望这解决了你的问题。

彼得

您有链接错误。要解决此问题,您需要链接到 mgl。看起来您正在尝试,但它不起作用。

首先,我会删除lmgl. so之间的空间

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

如果这不起作用,请检查是否有任何其他库需要链接。查看 g++ 是否抛出它找不到 mgl 的错误。

希望有帮助。