How to run qt creator demo code in clion? - qt

To see the code:
I create a repository on https://github.com/jaysimon/clion_qtcreator
I successed make a demo ui in qt creator, but failed in clion with the same code.
I got this:
[ 16%] Automatic MOC and UIC for target 20190314_clion_qt
[ 16%] Built target 20190314_clion_qt_autogen
[ 33%] Linking CXX executable 20190314_clion_qt
CMakeFiles/20190314_clion_qt.dir/src/mainwindow.cpp.o: In function `MainWindow::MainWindow(QWidget*)':
/home/hw/01-workspace/20190314_clion_qt/src/mainwindow.cpp:7: undefined reference to `vtable for MainWindow'
/home/hw/01-workspace/20190314_clion_qt/src/mainwindow.cpp:7: undefined reference to `vtable for MainWindow'
CMakeFiles/20190314_clion_qt.dir/src/mainwindow.cpp.o: In function `MainWindow::~MainWindow()':
/home/hw/01-workspace/20190314_clion_qt/src/mainwindow.cpp:30: undefined reference to `vtable for MainWindow'
/home/hw/01-workspace/20190314_clion_qt/src/mainwindow.cpp:30: undefined reference to `vtable for MainWindow'
collect2: error: ld returned 1 exit status
CMakeFiles/20190314_clion_qt.dir/build.make:131: recipe for target '20190314_clion_qt' failed
make[3]: *** [20190314_clion_qt] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/20190314_clion_qt.dir/all' failed
make[2]: *** [CMakeFiles/20190314_clion_qt.dir/all] Error 2
CMakeFiles/Makefile2:84: recipe for target 'CMakeFiles/20190314_clion_qt.dir/rule' failed
make[1]: *** [CMakeFiles/20190314_clion_qt.dir/rule] Error 2
Makefile:118: recipe for target '20190314_clion_qt' failed
make: *** [20190314_clion_qt] Error 2
Could you help to run it with CMake successfully?
A push request is the best.
CMakeList.txt:
cmake_minimum_required(VERSION 3.0)
project(20190314_clion_qt)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
find_package(Qt5Core REQUIRED)
find_package(Qt5Gui REQUIRED)
find_package(Qt5Widgets REQUIRED)
include_directories(./inc)
set(SOURCE_CODE
src/main.cpp
src/mainwindow.cpp
src/deal.cpp)
add_executable(${PROJECT_NAME} ${SOURCE_CODE})
target_link_libraries(${PROJECT_NAME} -pthread Qt5::Core)
target_link_libraries(${PROJECT_NAME} Qt5::Gui)
target_link_libraries(${PROJECT_NAME} Qt5::Widgets)
main.cpp:
#include "mainwindow.h"
#include <QApplication>
#include "deal.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
pthread_t tid;
pthread_create(&tid, NULL, run, NULL);
//pthread_exit(&tid);
return a.exec();
}

I solved it, but confused why, here is the solution, you can also git it on github.
put MainWindow.cpp MainWindow.h MainWindow.ui, all 3 files into the same directory, it's solved.

Related

CLion setting up Qt and getting numerous errors stating 'undefined reference to '_imp___ZN

I am getting several undefined reference errors with using Qt. I am using MinGW as my compiler and I am using Qt version 5.15.2.
This is my CMake file:
cmake_minimum_required(VERSION 3.17)
project(jerseytraffic)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(QT_VERSION 5)
set(REQUIRED_LIBS Core Gui Widgets)
set(REQUIRED_LIBS_QUALIFIED Qt5::Core Qt5::Gui Qt5::Widgets)
set(CMAKE_PREFIX_PATH "A:\\CppProjects\\CppLibraries\\Qt5\\5.15.2\\mingw81_64\\lib\\cmake\\")
find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED)
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED})
This is main.cpp:
#include <QCoreApplication>
#include <QDebug>
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
qDebug() << "Hello World";
return QCoreApplication::exec();
}
These are the errors I'm getting:
CMakeFiles\jerseytraffic.dir/objects.a(main.cpp.obj): In function `main':
A:/CppProjects/jerseytraffic/main.cpp:5: undefined reference to `_imp___ZN16QCoreApplicationC1ERiPPci'
A:/CppProjects/jerseytraffic/main.cpp:6: undefined reference to `_imp___ZNK14QMessageLogger5debugEv'
A:/CppProjects/jerseytraffic/main.cpp:6: undefined reference to `_imp___ZN6QDebugD1Ev'
A:/CppProjects/jerseytraffic/main.cpp:7: undefined reference to `_imp___ZN16QCoreApplication4execEv'
A:/CppProjects/jerseytraffic/main.cpp:5: undefined reference to `_imp___ZN16QCoreApplicationD1Ev'
A:/CppProjects/jerseytraffic/main.cpp:6: undefined reference to `_imp___ZN6QDebugD1Ev'
A:/CppProjects/jerseytraffic/main.cpp:5: undefined reference to `_imp___ZN16QCoreApplicationD1Ev'
CMakeFiles\jerseytraffic.dir/objects.a(main.cpp.obj): In function `ZN7QString8fromUtf8EPKci':
A:/CppProjects/CppLibraries/Qt5/5.15.2/mingw81_64/include/QtCore/qstring.h:706: undefined reference to `_imp___ZN7QString15fromUtf8_helperEPKci'
CMakeFiles\jerseytraffic.dir/objects.a(main.cpp.obj): In function `ZN6QDebug10maybeSpaceEv':
A:/CppProjects/CppLibraries/Qt5/5.15.2/mingw81_64/include/QtCore/qdebug.h:129: undefined reference to `_imp___ZN11QTextStreamlsEc'
CMakeFiles\jerseytraffic.dir/objects.a(main.cpp.obj): In function `ZN6QDebuglsEPKc':
A:/CppProjects/CppLibraries/Qt5/5.15.2/mingw81_64/include/QtCore/qdebug.h:159: undefined reference to `_imp___ZN11QTextStreamlsERK7QString'
CMakeFiles\jerseytraffic.dir/objects.a(main.cpp.obj): In function `ZN15QTypedArrayDataItE10deallocateEP10QArrayData':
A:/CppProjects/CppLibraries/Qt5/5.15.2/mingw81_64/include/QtCore/qarraydata.h:239: undefined reference to `_imp___ZN10QArrayData10deallocateEPS_jj'
This is what I get in the CMake output when it reloads:
"A:\ProgramFiles\CLion 2020.3.2\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" A:\CppProjects\jerseytraffic
-- Configuring done
-- Generating done
-- Build files have been written to: A:/CppProjects/jerseytraffic/cmake-build-debug
[Finished]
One thing I have tried is modifying the cmake file by adding this around the add_executable line, but it did not make any changes:
find_package(Qt5 COMPONENTS Core REQUIRED)
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} Qt5::Core)
Some other things that I have attempted include:
set(CMAKE_BUILD_TYPE "Debug -G \"MinGW Makefiles\"")
set(CMAKE_CXX_COMPILER "C:\\MinGW\\bin\\gcc.exe")
option(G "MinGW Makefiles")
I added this CMake option in Settings > Build, Execution, Deployment > CMake > Debug > CMake options: -G "MinGW Makefiles" %*
However, none of these seemed to fix the error.

build openCV & Qt project and link error:undefined reference and bad reloc address 0x1 in section

I have built the release version of openCV 4.5.1 shared library with CMake 3.91.2 & MinGW-W64 GCC-6.4.0 i686-posix-dwarf with default compile config.
Tried to use the libraries with Qt 5.4 & MinGW 4.9.1 32 bit.
I'm in Windows 10 64 bit.
Here's my .pro:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = test05
TEMPLATE = app
CONFIG += c++11
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
INCLUDEPATH += $$PWD/include
LIBS += -L$$PWD/lib/opencv -lopencv_highgui451.dll -lopencv_core451.dll -lopencv_imgcodecs451.dll -lopencv_imgproc451.dll
Here's main.cpp
#include "mainwindow.h"
#include <QApplication>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
int main(int argc, char** argv) {
Mat image = imread("D:\\test.jpg");
namedWindow("window", WINDOW_AUTOSIZE );
imshow("window",image);
waitKey(0);
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Here's build output:
E:\workspace\test05\main.cpp:-1: error: undefined reference to `cv::imread(std::string const&, int)'
E:\workspace\test05\main.cpp:-1: error: undefined reference to `cv::namedWindow(std::string const&, int)'
E:\workspace\test05\main.cpp:-1: error: undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
:-1: error: release/main.o: bad reloc address 0x1 in section `.text$_ZN7cvflann7anyimpl21typed_base_any_policyIbE4typeEv[__ZN7cvflann7anyimpl21typed_base_any_policyIbE4typeEv]'
collect2.exe:-1: error: error: ld returned 1 exit status
I have googled a lot,these two questions seems close to my situation but could not solve my problem:
Undefined reference to cv::imread(std::string const&, int)
C++ Qt MingW bad reloc 0xc address in section rdata
EDIT:
Thank for your advice,I deleted the GUI part and tried these:
LIBS += E:\workspace\test05\lib\opencv\libopencv_core451.dll.a
LIBS += E:\workspace\test05\lib\opencv\libopencv_core451.dll
Both don't work,the issues keep the same as above.I don't know if I built the shared library correctly or it's a simple link error?

Programm crashes using Taglib

I am trying to use Taglib with Qt5.7. But my programm crashes:
The GDB process terminated unexpectedly (exit code 0).
Cannot continue debugged process: The program is not being run.
During startup program exited with code 0xc0000135
There did not seem to be any error messages while building Taglib.
*.pro file
QT += core
QT -= gui
CONFIG += c++11
TARGET = untitled2
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
TAGLIBDIR = $$quote(D:/D/Programmieren/Libraries/taglib)
INCLUDEPATH += $$quote( $${TAGLIBDIR}/include )
LIBS += -L$$quote($${TAGLIBDIR}/lib) -ltag
main.cpp
#include <QCoreApplication>
#include <taglib/tag.h>
#include <taglib/fileref.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
TagLib::FileRef ref("D:/D/Media/Musik/Disturbed/The Sickness/Disturbed - Stupify.mp3");
return a.exec();
}
Edit:
I created Taglib-buildfiles using CMake-GUI and native MinGW-Installation since I did not manage to create them using Qt. My OS is Windows 10 x64 Education and I use Qt-Creator. I downloaded the latest Taglib-Release (1.11.1)
Why I cant create buildfiles using CMake:
First some *dll-files where missing, i coud solve that by just copying the ones from native MinGW-Installation to QT-MinGW.
Second I could not find any of the *exe-files desired by CMake so i could not change the path from native MinGW-Installation.
After rebuilding Taglib in debugmode I get this errors:
main.cpp:10: error: undefined reference to `_imp___ZN6TagLib8FileNameC1EPKc'
main.cpp:10: error: undefined reference to `_imp___ZN6TagLib7FileRefC1ENS_8FileNameEbNS_15AudioProperties9ReadStyleE'
main.cpp:11: error: undefined reference to `_imp___ZNK6TagLib7FileRef3tagEv'
main.cpp:11: error: undefined reference to `_imp___ZNK6TagLib6String9toCStringEb'
main.cpp:11: error: undefined reference to `_imp___ZN6TagLib6StringD1Ev'
main.cpp:10: error: undefined reference to `_imp___ZN6TagLib7FileRefD1Ev'
main.cpp:11: error: undefined reference to `_imp___ZN6TagLib6StringD1Ev'
main.cpp:10: error: undefined reference to `_imp___ZN6TagLib7FileRefD1Ev'
collect2.exe:-1: error: error: ld returned 1 exit status
Solved it by using new version (5.8 instead of 5.7) of Qt.

Can't identify why the code can't be executed [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I've just started with Qt. This is an example from a book. I've tried to compare the code with that in the book. It seems identical.
I am getting errors. How can I fix them?
The NetBeans shows as follows:
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE=/C/Qt/4.8.4/bin/qmake.exe SUBPROJECTS= .build-conf
make[1]: Entering directory `/c/Documents and Settings/Deloitte/��� ���������/NetBeansProjects/HelloWorld1'
/C/Qt/4.8.4/bin/qmake.exe VPATH=. -spec win32-g++ -o qttmp-Debug.mk nbproject/qt-Debug.pro
mv -f qttmp-Debug.mk nbproject/qt-Debug.mk
"/usr/bin/make" -f nbproject/qt-Debug.mk dist/Debug/MinGW_Qt-Windows/HelloWorld1.exe
make[2]: Entering directory `/c/Documents and Settings/Deloitte/��� ���������/NetBeansProjects/HelloWorld1'
g++ -mthreads -Wl,-subsystem,windows -o dist/Debug/MinGW_Qt-Windows/HelloWorld1.exe build/Debug/MinGW_Qt-Windows/main.o build/Debug/MinGW_Qt-Windows/moc_Counter.o -L'c:/Qt/4.8.4/lib' -lmingw32 -lqtmaind build/Debug/MinGW_Qt-Windows/HelloWorld1_resource_res.o -lQtGuid4 -lQtCored4
build/Debug/MinGW_Qt-Windows/main.o: In function `Z5qMainiPPc':
C:\Documents and Settings\Deloitte\��� ���������\NetBeansProjects\HelloWorld1/main.cpp:16: undefined reference to `Counter::Counter()'
build/Debug/MinGW_Qt-Windows/moc_Counter.o: In function `ZN7Counter18qt_static_metacallEP7QObjectN11QMetaObject4CallEiPPv':
C:\Documents and Settings\Deloitte\��� ���������\NetBeansProjects\HelloWorld1/moc_Counter.cpp:56: undefined reference to `Counter::slotInc()'
collect2: ���������� ld ����������� � ����� �������� 1
make[2]: *** [dist/Debug/MinGW_Qt-Windows/HelloWorld1.exe] Error 1
make[2]: Leaving directory `/c/Documents and Settings/Deloitte/��� ���������/NetBeansProjects/HelloWorld1'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/c/Documents and Settings/Deloitte/��� ���������/NetBeansProjects/HelloWorld1'
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 1s)
counter.h
#ifndef _Counter_h_
#define _Counter_h_
#include <QObject>
class Counter:public QObject{
Q_OBJECT
private:
int m_nValue;
public:
Counter();
public slots:
void slotInc();
signals:
void goodbye();
void counterChanged(int);
};
main.cpp
#include <QtGui/QApplication>
#include <QtGui>
#include "Counter.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QLabel lbl("0");
QPushButton cmd ("ADD");
Counter counter;
lbl.show();
cmd.show();
QObject::connect(&cmd, SIGNAL(clicked()),
&counter, SLOT (slotInc()));
QObject::connect(&counter, SIGNAL(counterChanged(int)),
&lbl, SLOT(setNum(int)));
QObject::connect(&counter, SIGNAL (goodbye()), &app, SLOT(quit()));
return app.exec();
}
Your code is missing the actual definition for the Counter class member functions, notably Counter::Counter the constructor and Counter::slotInc. You must provide implementations for them.
Counter::goodbye and Counter::counterChanged are signals for which the definition is provided by the Qt MOC.

Why am I getting "undefined reference to vtable..." errors when linking this Qt 5.0 application?

I've got a relatively simple Qt 5.0 project that uses CMake 2.8.9:
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.9)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
project(hello-world)
find_package(Qt5Widgets REQUIRED)
qt5_wrap_ui(hello-world_UI MainWindow.ui)
add_executable(hello-world MainWindow.cpp main.cpp ${hello-world_UI})
qt5_use_modules(hello-world Widgets)
MainWindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
virtual ~MainWindow();
private:
Ui::MainWindow * const ui;
};
#endif // CMAINWINDOW_H
MainWindow.cpp:
#include "MainWindow.h"
#include "ui_MainWindow.h"
MainWindow::MainWindow()
: ui(new Ui::MainWindow)
{
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp:
#include <QApplication>
#include "MainWindow.h"
int main(int argc, char * argv[])
{
QApplication app(argc, argv);
MainWindow win;
win.show();
return app.exec();
}
The project also includes a .ui file created with Qt Creator 2.6.1 (MainWindow.ui).
When I attempt to build the file with g++ on Linux, I receive the following errors:
CMakeFiles/hello-world.dir/MainWindow.cpp.o: In function `MainWindow::MainWindow()':
MainWindow.cpp:(.text+0x3b): undefined reference to `vtable for MainWindow'
MainWindow.cpp:(.text+0x4d): undefined reference to `vtable for MainWindow'
CMakeFiles/hello-world.dir/MainWindow.cpp.o: In function `MainWindow::~MainWindow()':
MainWindow.cpp:(.text+0xaf): undefined reference to `vtable for MainWindow'
MainWindow.cpp:(.text+0xc1): undefined reference to `vtable for MainWindow'
collect2: error: ld returned 1 exit status
What could possibly be causing this sort of error? I recently switched to CMake from qmake and I never remember running into this much trouble getting a trivial example to compile. What am I doing wrong?
Edit: here is the command being used to link everything:
/usr/bin/c++ CMakeFiles/hello-world.dir/MainWindow.cpp.o
CMakeFiles/hello-world.dir/main.cpp.o -o hello-world -rdynamic
/usr/local/Qt-5.0.0/lib/libQt5Widgets.so.5.0.0
/usr/local/Qt-5.0.0/lib/libQt5Gui.so.5.0.0
/usr/local/Qt-5.0.0/lib/libQt5Core.so.5.0.0
-Wl,-rpath,/usr/local/Qt-5.0.0/lib
Turns out I forgot:
set(CMAKE_AUTOMOC ON)
At the top of the CMakeLists.txt file.
I struggled with this for a long time using all the hints published here:
http://doc.qt.io/qt-5/cmake-manual.html
And here
https://www.kdab.com/using-cmake-with-qt-5/
What I had to do was specify things in the right order. For example, the following is the top of my CMakeLists.txt. Note that the two CMAKE set directives come before add_executable. Once I did this, I was able to link without undefined symbols and vtable references. I just thought I'd post this for the benefit of others.
cmake_minimum_required (VERSION 2.8)
set (CMAKE_AUTOMOC ON)
set (CMAKE_INCLUDE_CURRENT_DIR ON)
add_executable(FHSpectrumSensor wideband_seq_spectrum_sensor.cpp sensor.cpp gui.cpp ${gui_SRC})
Later in the CMakeLists.txt I have the following:
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Charts REQUIRED)
find_package(Qt5Core REQUIRED)
qt5_use_modules(FHSpectrumSensor Widgets Charts)
qt5_wrap_cpp(gui_SRC gui.h gui.cpp)
That did the trick.
I also ran into this problem yesterday and the above mentioned answers did't help. I already used set (CMAKE_AUTOMOC ON) and also qt5_wrap_cpp.
I tried to remember what I did, because I had a working version but it stopped working after "some" changes. I finally remembered that I tried to split the include files into a separate directory hierarchy. After reverting that and putting the include files back into the CMakeLists.txt it worked again. I sure don't know why, and I would like to know what went wrong, but I settled now for keeping the includes near the cpp files.
set(SOURCES
buffer.h
ITVSet.h
MainWindow.h
MainWindow.cpp
TVSet.h
TVSet.cpp
)

Resources