How to link with Qt?

959 views
Skip to first unread message

Csáti Zoltán

unread,
Sep 30, 2016, 9:17:14 AM9/30/16
to MathGL


Dear all,

I ran a QMathGL example from here: http://mathgl.sourceforge.net/doc_en/Using-QMathGL.html:
#include <QApplication>
#include <QMainWindow>
#include <QScrollArea>
#include <mgl2/qmathgl.h>

int sample(mglGraph *gr);

int main(int argc,char **argv)
{
 
QApplication a(argc,argv);
 
QMainWindow *Wnd = new QMainWindow;
 
Wnd->resize(810,610);  // for fill up the QMGL, menu and toolbars
 
Wnd->setWindowTitle("QMathGL sample");
 
// here I allow to scroll QMathGL -- the case
 
// then user want to prepare huge picture
 
QScrollArea *scroll = new QScrollArea(Wnd);
 
// Create and setup QMathGL
 
QMathGL *QMGL = new QMathGL(Wnd);
 
// QMGL->setPopup(popup); // if you want to setup popup menu for QMGL
  QMGL
->setDraw(sample);
 
// or use QMGL->setDraw(foo); for instance of class Foo:public mglDraw
  QMGL
->update();

 
// continue other setup (menu, toolbar and so on)
  scroll
->setWidget(QMGL);
 
Wnd->setCentralWidget(scroll);
 
Wnd->show();
 
return a.exec();
}

int sample(mglGraph *gr)
{
    gr
->SubPlot(1, 1, 0, "");
    gr
->Box();
    gr
->Axis();
    gr
->Grid("xy", ";k");
    gr
->FPlot("sin(pi*x)", "2r");
   
return 1;
}

 I couldn't link it from QtCreator, so I did it from the Terminal:
g++ -m64 -pipe -g -Wall -W -D_REENTRANT -fPIE -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++-64 -I../MathGL -I/usr/include/qt5 -I/usr/include/qt5/QtWidgets -I/usr/include/qt5/QtGui -I/usr/include/qt5/QtCore -I. -I. -I. -lmgl-qt5 -lmgl -std=gnu++11 main.cpp

which gave the error:
/tmp/ccSxmbTR.o: In function `main':
/home/zoli/MathGL/main.cpp:12: undefined reference to `
QApplication::QApplication(int&, char**, int)'
/home/zoli/MathGL/main.cpp:13: undefined reference to `QMainWindow::QMainWindow(QWidget*, QFlags<Qt::WindowType>)'

/home/zoli/MathGL/main.cpp:15: undefined reference to `QWidget::setWindowTitle(QString const&)'
/home/zoli/MathGL/main.cpp:18: undefined reference to `
QScrollArea::QScrollArea(QWidget*)'
/home/zoli/MathGL/main.cpp:20: undefined reference to `QMathGL::QMathGL(QWidget*, QFlags<Qt::WindowType>)'

/home/zoli/MathGL/main.cpp:24: undefined reference to `QMathGL::update()'
/home/zoli/MathGL/main.cpp:27: undefined reference to `
QScrollArea::setWidget(QWidget*)'
/home/zoli/MathGL/main.cpp:28: undefined reference to `QMainWindow::setCentralWidget(QWidget*)'

/home/zoli/MathGL/main.cpp:29: undefined reference to `QWidget::show()'
/home/zoli/MathGL/main.cpp:30: undefined reference to `
QApplication::exec()'
/home/zoli/MathGL/main.cpp:30: undefined reference to `QApplication::~QApplication()'

/home/zoli/MathGL/main.cpp:30: undefined reference to `QApplication::~QApplication()'
/tmp/ccSxmbTR.o: In function `
QString::QString(char const*)':
/usr/include/qt5/QtCore/qstring.h:579: undefined reference to `QString::fromAscii_helper(char const*, int)'

/tmp/ccSxmbTR.o: In function `QWidget::resize(int, int)':
/usr/include/qt5/QtWidgets/qwidget.h:830: undefined reference to `
QWidget::resize(QSize const&)'
/tmp/ccSxmbTR.o: In function `mglGraph::SubPlot(int, int, int, char const*, double, double)'
:
/usr/local/include/mgl2/mgl.h:286: undefined reference to `mgl_subplot_d'
/tmp/ccSxmbTR.o: In function `
mglGraph::Box(char const*, bool)':
/usr/local/include/mgl2/mgl.h:703: undefined reference to `mgl_box_str'

/tmp/ccSxmbTR.o: In function `mglGraph::Axis(char const*, char const*, char const*)':
/usr/local/include/mgl2/mgl.h:722: undefined reference to `
mgl_axis'
/tmp/ccSxmbTR.o: In function `mglGraph::Grid(char const*, char const*, char const*)'
:
/usr/local/include/mgl2/mgl.h:725: undefined reference to `mgl_axis_grid'
/tmp/ccSxmbTR.o: In function `
mglGraph::FPlot(char const*, char const*, char const*)':
/usr/local/include/mgl2/mgl.h:1894: undefined reference to `mgl_fplot'

/tmp/ccSxmbTR.o: In function `QMathGL::setDraw(int (*)(mglGraph*))':
/usr/local/include/mgl2/qmathgl.h:61: undefined reference to `
mgl_draw_graph'
/usr/local/include/mgl2/qmathgl.h:61: undefined reference to `QMathGL::setDraw(int (*)(mglBase*, void*), void*)'

/tmp/ccSxmbTR.o: In function `QTypedArrayData<unsigned short>::deallocate(QArrayData*)':
/usr/include/qt5/QtCore/qarraydata.h:230: undefined reference to `
QArrayData::deallocate(QArrayData*, unsigned long, unsigned long)'
collect2: error: ld returned 1 exit status

Why is the error? The problem is connected to Qt, as a simple MathGL example compiled without problems.

Thank you,
Zoltan

Csáti Zoltán

unread,
Sep 30, 2016, 11:05:10 AM9/30/16
to MathGL
I realized that the linking libraries must follow the executable and not precede it:
g++ -m64 -pipe -g -Wall -W -D_REENTRANT -fPIE -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++-64 -I../MathGL -I/usr/include/qt5 -I/usr/include/qt5/QtWidgets -I/usr/include/qt5/QtGui -I/usr/include/qt5/QtCore -I. -I. -I. -std=gnu++11 main.cpp -lmgl -lmgl-qt5

Now, the error reduced to
/usr/bin/ld: /tmp/cc8J2ZMS.o: undefined reference to symbol '_ZN7QString16fromAscii_helperEPKci'
//usr/lib/x86_64-linux-gnu/libQt5Core.so.5: error adding symbols: DSO missing from command line
Reply all
Reply to author
Forward
0 new messages