user3616359
user3616359

Reputation: 399

Cmake link issue: undefined reference to QPushButton

I just started using Qt. I have a problem with compiling the first example.
main.cpp:

#include <QCoreApplication>
#include <QPushButton>

int main(int argc, char** argv)
{
  QCoreApplication app(argc, argv);
  QPushButton button ("Hello world !");

 return app.exec();
}

CMake.txt:

 cmake_minimum_required(VERSION 2.6)
 project(new)
 find_package(Qt4 REQUIRED)
 enable_testing()
 include_directories(${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR})
 set(source_SRCS main.cpp)
 qt4_automoc(${source_SRCS})
 add_executable(new ${source_SRCS})
 target_link_libraries(new${QT_QTCORE_LIBRARY})
 add_subdirectory(tests)
 install(TARGETS new RUNTIME DESTINATION .)

The error I get upon building is:

undefined reference to `QPushButton::QPushButton(QString const&,QWidget*)'

It is a linking problem, but how can I solve it?

Upvotes: 0

Views: 1765

Answers (6)

Ahmad Sharif
Ahmad Sharif

Reputation: 4435

Include Widgets in *.pro file after the 'INCLUDEPATH += .' line

INCLUDEPATH += .
QT += widgets

Upvotes: 0

roboTURX
roboTURX

Reputation: 1

this answer solves my same problem :)


ok, i had can solve it by myself.

For all they have they problem too:

The error is sourced from the use of "Q_OBJECT". To solve the error, right-cklick on the Project and choose "Run qmake" and after >this: "Rebuild".

Then the error should be disappeared ;-)

-casisto

https://forum.qt.io/topic/52439/get-undefined-reference-error-but-don-t-know-why/2

Upvotes: -1

You have three problems:

  1. You're not linking with the Gui module (Widgets module in Qt 5). This is covered in the other answer.

  2. You you must use QApplication in widgets-based applications. Since QPushButton comes from the Gui module (Widgets in Qt5), you can't merely use QCoreApplication nor QGuiApplication: your program will crash as soon as you attempt to instantiate a QWidget.

  3. You're not showing the button, so when your program starts you'll see nothing once you fix the above.

Your main.cpp should look like:

#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
#include <QtGui>
#else
#include <QtWidgets>
#endif

int main(int argc, char** argv)
{
  QApplication app(argc, argv);
  QPushButton button ("Hello world !");
  button.show();
  return app.exec();
}

Upvotes: 2

Stefano
Stefano

Reputation: 4031

Here is what I think you are missing:

find_package(Qt4 REQUIRED QtGui)

looking at your cmake you probably want to change the target_link_libraries for the following:

target_link_libraries(new ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY})

Upvotes: 4

Resurrection
Resurrection

Reputation: 4106

Consider using qmake instead of cmake with Qt.

Anyway, QCoreApplication (see docs) is the console version of main application class and will not work in GUI application. QPushButton is a widget class and can exist alone and will make a window (although you must show() it explicitely for that) but only with QApplication.

When using qmake in your *.pro file you need to include widgets like so:

CONFIG += widgets

and make sure you don't have

CONFIG -= gui

If you insist on using cmake then see here.

Upvotes: 0

Joe
Joe

Reputation: 7798

You're also going to need to link against the QtGui and QtWidgets library. Within qmake it handles which of the many libraries make up Qt, you'll have to do this by hand in cmake.

If you look at the documentation for QPushButton (http://doc.qt.io/qt-5/qpushbutton.html), the "qmake" line shows what library you need.

Upvotes: 0

Related Questions