Compilation error in qt

Asked 2 years ago, Updated 2 years ago, 148 views

Attempted to 'Hello World' with Qt for Beginners-Qt Wiki using c++ in vscode on docker.
The main.cpp is as follows:

#include<QApplication>
# include <QPushButton>

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

    return app.exec();
}   

The main.pro file looks like this:

TEMPLATE=app TARGET=name_of_the_app
QT = core gui
QT+=widgets
SOURCES+=main.cpp

Then

on the terminal.
qmake-project
qmake
make

after doing
g++-Wl, -O1-oCalculation_cpp main.o-lQt5Gui-lQt5 Core-lGL-lpthread 
/usr/bin/ld:main.o:in function`main.cold.0':
main.cpp: (.text.unlikely+0xc): undefined reference to `QApplication::~QApplication()'
/usr/bin/ld:main.cpp:(.text.unlikely+0x1f): undefined reference to `QPushButton::~QPushButton()'
/usr/bin/ld:main.o:in function `main':
main.cpp: (.text.startup+0x22): undefined reference to `QApplication::QApplication(int&, char**, int)'
/usr/bin/ld:main.cpp:(.text.startup+0x4f): undefined reference to `QPushButton::QPushButton(QString const&, QWidget*)'
/usr/bin/ld:main.cpp:(.text.startup+0x5f): undefined reference to `QWidget::show()'
/usr/bin/ld:main.cpp: (.text.startup+0x64): undefined reference to `QApplication::exec()'
/usr/bin/ld:main.cpp:(.text.startup+0x6f): undefined reference to `QPushButton::~QPushButton()'
/usr/bin/ld:main.cpp:(.text.startup+0x77): undefined reference to `QApplication::~QApplication()'
collect2:error:ld returned1 exit status
make:*** [Makefile:139:Calculation_cpp] Error 1

appears as shown in the

cannot open source file "QAplication"
cannot open source file "QPushButton"

It turned out to be the case.
qt5-default, build-essential, apt-get install mesa-common-dev are installed.
What should I do?

add
When I checked that QT+=widgets was written, I ran qmake, make and found the following output:

g++-Wl, -O1-oCalculation_cpp main.o-lQt5Gui-lQt5 Core-lGL-lpthread 
/usr/bin/ld:main.o:in function`main.cold.0':
main.cpp: (.text.unlikely+0xc): undefined reference to `QApplication::~QApplication()'
/usr/bin/ld:main.cpp:(.text.unlikely+0x1f): undefined reference to `QPushButton::~QPushButton()'
/usr/bin/ld:main.o:in function `main':
main.cpp: (.text.startup+0x22): undefined reference to `QApplication::QApplication(int&, char**, int)'
/usr/bin/ld:main.cpp:(.text.startup+0x4f): undefined reference to `QPushButton::QPushButton(QString const&, QWidget*)'
/usr/bin/ld:main.cpp:(.text.startup+0x5f): undefined reference to `QWidget::show()'
/usr/bin/ld:main.cpp: (.text.startup+0x64): undefined reference to `QApplication::exec()'
/usr/bin/ld:main.cpp:(.text.startup+0x6f): undefined reference to `QPushButton::~QPushButton()'
/usr/bin/ld:main.cpp:(.text.startup+0x77): undefined reference to `QApplication::~QApplication()'
collect2:error:ld returned1 exit status
make:*** [Makefile:139:Calculation_cpp] Error 1

Also, as above,

#include errors detected.Please update your includePath.Squiggles are disabled for this translation unit(/workspaces/Calculation_cpp/main.cpp).C/C++(1696)
cannot open source file "QApplication" C/C++ (1696)

It was written like this.

c++ ubuntu qt qt5

2022-09-30 14:48

1 Answers

qmake-project is a command that automatically generates .pro, so you don't have to run it when you write main.pro yourself (it will overwrite the content).

The contents of main.cpp and main.pro do not appear to be a problem.

g++-Wl, -O1-o Calculation_cpp main.o-lQt5Gui-lQt5 Core-lGL-lpthread

As far as you can see, the -lQt5Widgets are missing, resulting in an undefined reference to … error.

Check if "QT+=widgets" is listed in the .pro file.

However, if QT+=widgets are not available,
when compiling the main.cpp before the link. I have a feeling that #include such as QApplication will fail.


2022-09-30 14:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.