Undefined reference error while compiling Qt hello world program - c++

I am trying to compile a Qt hello world program. I am using Qt 5.2.0 on Windows 64 bit.
This is my code
#include <QCoreApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << "Hello world";
return a.exec();
}
When i run the application i get the following run time errors:
mingw32-make[1]: Entering directory 'C:/Users/test/qt_creator_projects/build-QtTest- TestLocal_PC-Debug'
g++ -c -pipe -fno-keep-inline-dllexport -g -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_CORE_LIB -I..\QtTest -
I"D:\Qt\5.2.0\msvc2012_64\include" -I"D:\Qt\5.2.0\msvc2012_64\include\QtCore" -I"debug" -
I"." -I"D:\Qt\5.2.0\msvc2012_64\mkspecs\win32-g++" -o debug\main.o ..\QtTest\main.cpp
g++ -Wl,-subsystem,console -mthreads -o debug\QtTest.exe debug/main.o -
LD:\Qt\5.2.0\msvc2012_64\lib -lQt5Cored
debug/main.o: In function `main':
C:\Users\test\qt_creator_projects\build-QtTest-TestLocal_PC-
Debug/../QtTest/main.cpp:5: undefined reference to `_imp___ZN16QCoreApplicationC1ERiPPci'
C:\Users\test\qt_creator_projects\build-QtTest-TestLocal_PC- Debug/../QtTest/main.cpp:7: undefined reference to `_imp___ZN16QCoreApplication4execEv'
Makefile.Debug:77: recipe for target 'debug\QtTest.exe' failed
C:\Users\test\qt_creator_projects\build-QtTest-TestLocal_PC-
Debug/../QtTest/main.cpp:7: undefined reference to `_imp___ZN16QCoreApplicationD1Ev'
mingw32-make[1]: Leaving directory 'C:/Users/test/qt_creator_projects/build-QtTest- TestLocal_PC-Debug'
Makefile:34: recipe for target 'debug' failed
C:\Users\test\qt_creator_projects\build-QtTest-TestLocal_PC-Debug/../QtTest/main.cpp:7: undefined reference to `_imp___ZN16QCoreApplicationD1Ev'
collect2.exe: error: ld returned 1 exit status
mingw32-make[1]: *** [debug\QtTest.exe] Error 1
mingw32-make: *** [debug] Error 2
16:37:34: The process "D:\pen drive data\MinGW\bin\mingw32-make.exe" exited with code 2.
Error while building/deploying project QtTest (kit: TestLocal PC)
When executing step 'Make'
.pro file
QT += core
QT -= gui
TARGET = QtTest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
I want to use Qt with MinGW .I am currently using Qt creatore for 64 bit Visual Studio 2012 compiler. Since 64 bit MinGW is not available in the downloads for 5.2.0
Can someone help me solve these reference errors?

Why would you download a VS2012 version of Qt if you don't have, well, VS2012?
Download a version that matches the exact compiler you have. If there is none, download the sources and compile it yourself.
If you want to avoid the complications of self-compilation, then VS2012 Express is perfectly adequate to develop with Qt. It's the same compiler as the paid-for versions, and if you develop using Qt Creator, then you really don't care for whatever limitations the express edition IDE might have. It works great.
Another possibility is to use the mingw build of Qt. During setup, you can enable the installation of the MinGW 4.8.0 toolchain. This gives you a complete development environment, with the IDE, the Qt headers and binaries, and a toolchain.

Related

undefined reference to `WinMain' in eclipse IDE with cygwin (setup-x86_64)

I am currently doing unit testing for a project of c/c++ in eclipse IDE (Version: Luna Service Release 2 (4.4.2)) and I have recently upgraded my cygwin to cygwin64 to make it compatible with c++17 standard and my g++ version is g++ (GCC) 10.2.0, But I am not able to build my testsuites in eclipse, I have set the environment path to "C:\cygwin64\bin", But still I am getting the below error. Could you please help me with the below error:
Console output after building my project :
make all
Building file:
C:/Accurev/AngioJet_Development_ver1/CS/AngioJet/Firmware/src/ClinicalFeatures/door.cpp
Invoking: Cygwin C++ Compiler
g++ -DUNIT_TESTING - "C:/Accurev/AngioJet_Development_ver1/CS/ut/testsuites/Firmware/hell/../../../../AngioJet/Firmware/build/PicoZed_FSBL_bsp/ps7_cortexa9_0/include" -I"C:/Accurev/AngioJet_Development_ver1/CS/ut/testsuites/Firmware/hell/../../../testsupport/" -I"C:/Accurev/AngioJet_Development_ver1/CS/ut/testsuites/Firmware/hell/../../../testsupport/Source" -I"C:/Accurev/AngioJet_Development_ver1/CS/ut/testsuites/Firmware/hell/../../../../AngioJet/Firmware/src/Platform/Zynq7000" - I"C:/Accurev/AngioJet_Development_ver1/CS/ut/testsuites/Firmware/hell/../../../../AngioJet/Firmware/src/HostInterface" -I"C:/Accurev/AngioJet_Development_ver1/CS/ut/testsuites/Firmware/hell/../../../../AngioJet/Firmware/soup/treck/6_0_1_56/include" -I"C:/Accurev/AngioJet_Development_ver1/CS/ut/testsuites/Firmware/hell/../../../../Einstein/src/Platform/Zynq7000" -I"C:/Accurev/AngioJet_Development_ver1/CS/ut/testsuites/Firmware/hell/../../../../Einstein/src/Common" -I"C:/Accurev/AngioJet_Development_ver1/CS/ut/testsuites/Firmware/hell/../../../../AngioJet/Firmware/src/Common" -I"C:/Accurev/AngioJet_Development_ver1/CS/ut/testsuites/Firmware/hell/../../../../AngioJet/Firmware/src/ClinicalFeatures" -O0 -g -fprofile-arcs -ftest-coverage -Wall -c -fmessage-length=0 -MMD -MP -MF"src/door.d" -MT"src/door.d" -o "src/door.o" "C:/Accurev/AngioJet_Development_ver1/CS/AngioJet/Firmware/src/ClinicalFeatures/door.cpp"
Finished building: C:/Accurev/AngioJet_Development_ver1/CS/AngioJet/Firmware/src/ClinicalFeatures/door.cpp
Building target: helll.exe
Invoking: Cygwin C++ Linker
g++ -fprofile-arcs -o "helll.exe" ./src/door.o -lgcov -lbfd
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: /usr/lib/gcc/x86_64-pc-cygwin/10/../../../../lib/libcygwin.a(libcmain.o): in function `main':
/usr/src/debug/cygwin-3.3.6-1/winsup/cygwin/lib/libcmain.c:37: undefined reference to `WinMain'
collect2: error: ld returned 1 exit status
make: *** [makefile:47: helll.exe] Error 1
07:05:17 Build Finished (took 4s.531ms)
`*
I have uninstalled and reinstalled the softwares. Also I have built a new project from scratch which is building fine but whenever i try to build my project it is not building.
While doing unit testing in eclipse we create the project as c++ project and import the source files mentioned below and the structure is as follow:
Source file - door.cpp, door.h
door.h:
#include "global.h"
namespace lDOOR { void close( BasicTypes::UInt8 mode ); }
door.cpp:
#include "door.h"
void lDOOR::close(void)
{
//Normal Mode
close(BscMsg::NORMAL);
}
and we do the unit testing for the above functions present in .cpp file in the file created by us door_testsuite.h which is as follows:
#include <cxxtest/TestSuite.h>
class door_TestSuite: public CxxTest::TestSuite {
public:
void setUp(){}
void teardown(){}
void testDoorLDoorClose()
{
BscMsg::UInt8 mode = 2;
lDOOR::close(mode);
TS_ASSERT_EQUALS();
}};
And when I test the above code in cygwin terminal it executes completely fine.
This means there is no WinMain function, which is the entry point to Windows windows programs. Yes, confusing. You can choose to either build a program as a console program or windows program. (There are a few other types that are irrelevant) In a console program, the entry point is main (As with the entire world of computing), but in a windows program, it is WinMain. This question has a good answer that goes into depth about it, but you either need to switch the type of program to console, or (If your program is creating and using windows) add a WinMain function.
Sorry if this is confusing, it is difficult to differentiate between Windows, the OS, and windows, the type of program. Also, my knowledge on this subject is all from VSCode, so may be completely or partially incorrect.
TLDR: Either add a WinMain function, or switch to building your program as a windows program.

Adding ObjectBox to Qt application

I have just tried to add ObjectBox to an empty qt application, but somehow I do not get any further.
I tried to follow the instructions: https://cpp.objectbox.io/installation
I took the win x64 lib from here: https://github.com/objectbox/objectbox-c/releases/tag/v0.17.0
I am always getting undefined reference errors and I do not know why. I guess there must be something wrong with linking in der .pro file?
Thats my .pro file (just edited the last 4 lines):
QT -= gui
CONFIG += c++11 console
CONFIG -= app_bundle
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/objectbox/lib/ -lobjectbox
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/objectbox/lib/ -lobjectbox
INCLUDEPATH += $$PWD/objectbox/include
DEPENDPATH += $$PWD/objectbox/include
And here ist my main.cpp:
#include <QCoreApplication>
#include "objectbox.hpp"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
printf("Using ObjectBox version %s\n", obx_version_string());
return a.exec();
}
Compiler says the following:
C:/Qt/Tools/mingw730_64/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory 'C:/temp/obxTut/ObjectBoxTutorial'
g++ -c -fno-keep-inline-dllexport -g -std=gnu++11 -Wall -W -Wextra -fexceptions -mthreads -DUNICODE -D_UNICODE -DWIN32 -DMINGW_HAS_SECURE_API=1 -DQT_QML_DEBUG -DQT_CORE_LIB -I. -Iobjectbox\include -I..\..\..\Qt\5.12.11\mingw73_64\include -I..\..\..\Qt\5.12.11\mingw73_64\include\QtCore -Idebug -I..\..\..\Qt\5.12.11\mingw73_64\mkspecs\win32-g++ -o debug\main.o main.cpp
In file included from main.cpp:2:0:
objectbox\include/objectbox.hpp:711:21: warning: 'std::vector<long long unsigned int> obx::{anonymous}::idVectorOrThrow(OBX_id_array*)' defined but not used [-Wunused-function]
std::vector<obx_id> idVectorOrThrow(OBX_id_array* cIds) {
^~~~~~~~~~~~~~~
objectbox\include/objectbox.hpp:703:14: warning: 'OBX_id_array obx::{anonymous}::cIdArrayRef(const std::vector<long long unsigned int>&)' defined but not used [-Wunused-function]
OBX_id_array cIdArrayRef(const std::vector<obx_id>& ids) {
^~~~~~~~~~~
g++ -Wl,-subsystem,console -mthreads -o debug\ObjectBoxTutorial.exe debug/main.o -LC:\temp\obxTut\ObjectBoxTutorial\objectbox\lib -lobjectbox C:\Qt\5.12.11\mingw73_64\lib\libQt5Cored.a
debug/main.o: In function `idVectorOrThrow':
C:\temp\obxTut\ObjectBoxTutorial/objectbox/include/objectbox.hpp:712: undefined reference to `obx::internal::throwLastError(int, char const*)'
C:\temp\obxTut\ObjectBoxTutorial/objectbox/include/objectbox.hpp:718: undefined reference to `obx::internal::throwIllegalStateException(char const*, char const*)'
debug/main.o: In function `applyTo':
C:\temp\obxTut\ObjectBoxTutorial/objectbox/include/objectbox.hpp:828: undefined reference to `obx::internal::throwIllegalStateException(char const*, char const*)'
collect2.exe: error: ld returned 1 exit status
mingw32-make[1]: *** [Makefile.Debug:65: debug/ObjectBoxTutorial.exe] Error 1
mingw32-make: *** [Makefile:38: debug] Error 2
mingw32-make[1]: Leaving directory 'C:/temp/obxTut/ObjectBoxTutorial'
23:05:08: The process "C:\Qt\Tools\mingw730_64\bin\mingw32-make.exe" exited with code 2.
Error while building/deploying project ObjectBoxTutorial (kit: Desktop Qt 5.12.11 MinGW 64-bit)
When executing step "Make"
Qt 5.12 with MinGW 7.3
The problem is the compiler that you are using, because the library is probably compiled with MSVC and you are trying to build your application with MinGW and use the C++ interface of the library.
this won't work because of name mangling and etc. see How to use libraries compiled with MingW in MSVC?
Thankfully this library also provides a pure C interface "objectbox.h", so including this header instead of the one with "hpp" would also fix your issue.
I was able to build and link a test app by using MSVC 2015 64 Bit.

Freeglut 64 bit program linking errors

I'm trying to compile a 64 bit version of an OpenGL C++ program using freeglut. I followed the exact instructions on this website to set up freeglut with MinGW. I have the header files in C:\MinGW\include\GL, I have the 32 bit libraries in C:\MinGW\lib and 64 bit libraries in C:\MinGW\lib\x64, and I have the 64 bit freeglut.dll in my project directory. However, even the simplest of OpenGL programs don't link successfully...
My code is minimal:
// test.cpp
#include <GL/glut.h>
int main(int argc, char *argv[]) {
glutInit(&argc, argv);
}
And I compile it with the exact commands given on the readme/on the website:
g++ -c -o test.o test.cpp -I"C:\MinGW\include"
g++ -o test.exe test.o -L"C:\MinGW\lib\x64" -lfreeglut -lopengl32 -Wl,--subsystem,windows
(except of course I changed the directories and changed gcc to g++)
The compile runs fine, but linking throws these error messages:
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: test.o:test.cpp:(.text+0x1c): undefined reference to `_imp____glutInitWithExit#12'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: test.o:test.cpp:(.text+0x3f): undefined reference to `_imp____glutCreateWindowWithExit#8'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: test.o:test.cpp:(.text+0x61): undefined reference to `_imp____glutCreateMenuWithExit#8'
collect2.exe: error: ld returned 1 exit status
I've tried to change -lopengl32 in the command to -lopengl64 and -lopengl, but the linker couldn't find those libraries.
I can't answer your specific case, but another approach would be to use mingw-w64, with MSYS2 as a package manager. The old mingw.org project is not well maintained, unlike mingw-w64 and the MSYS2 package database. There is probably some mismatch between how your precompiled binaries were built and the compiler you are currently using.
On my system I compiled your code with g++ -o gl gl.cpp -lfreeglut and it worked first time .
How to install MinGW-w64 and MSYS2?
Then use pacman -Ss glut to find the freeglut package.

Linking Qt5.6 yields error not finding required references

I’m trying to extend an existing makefile C++ project with a Qt GUI (Qt 5.6.0 because I cannot rely on Cxx11 features). The problem is I can’t get Qt to link properly. I’d like to build a X86 application (32bit) under Win7 (x86-64). I’ve installed qt-opensource-windows-x86-mingw492-5.6.0.exe obtained from the Qt download page https://download.qt.io/archive/qt/5.6/5.6.0/. Everything compiles. However, linking yields the following error:
C:/sofit/inuit_workspace/inuit_development/tools/makefiles/inuit/MakefileCatenaISS.mk:145:
recipe for target 'build/model/model.exe' failed
C:/sofit/inuit_workspace/inuit_development/tools/inuit/inuit.cpp:175:
undefined reference to `_imp___ZN12QApplicationC1ERiPPci'
C:/sofit/inuit_workspace/inuit_development/tools/inuit/inuit.cpp:176:
undefined reference to
`_imp___ZN7QWidgetC1EPS_6QFlagsIN2Qt10WindowTypeEE'
C:/sofit/inuit_workspace/inuit_development/tools/inuit/inuit.cpp:180:
undefined reference to `_imp___ZN7QWidget4showEv'
C:/sofit/inuit_workspace/inuit_development/tools/inuit/inuit.cpp:181:
undefined reference to `_imp___ZN12QApplication4execEv'
C:/sofit/inuit_workspace/inuit_development/tools/inuit/inuit.cpp:307:
undefined reference to `_imp___ZN12QApplicationD1Ev'
C:/sofit/inuit_workspace/inuit_development/tools/inuit/inuit.cpp:307:
undefined reference to `_imp___ZN12QApplicationD1Ev'
collect2.exe: error: ld returned 1 exit status
make: *** [build/model/model.exe] Error 1
I use the following command to link:
C:\Qt\Qt5.6.0\Tools\mingw492_32\bin\g++.exe -g
-LC:\Qt\Qt5.6.0\5.6\mingw49_32\lib -lQt5Widgets -lQt5Core -lQt5Gui -lQt5Cored -lQt5Guid -lQt5Widgetsd -lqtmain -lqtmaind -o build/model/model.exe ./build/model/objects/a.obj ./build/model/objects/b.obj
I’ve added a bunch of libraries and various combinations. Solely using Widgets, Core, and Gui doesn’t do the trick either.
Potential causes identified in answers in related questions (see below) aimed at
- Mismatch architecture lib/compiler
- Missing -lQt5Widgets
However, I don’t think this applies here. Please note that I’m not using CMake or QMake (hence no .pro file or CMake files) – just plain Gnu make called within Eclipse Mars.
The source (in case of interest):
int main(int argc, char *argv[])
QApplication app(argc, argv);
QWidget *widget = new QWidget;
Ui_MainWindow ui;
widget->show();
return app.exec();
}
And the compiler command (in case of interest):
C:\Qt\Qt5.6.0\Tools\mingw492_32\bin\g++.exe -IC:\Qt\Qt5.6.0\5.6\mingw49_32\include -IC:/my_project -Wall -g -c -DWIN32 -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT C:/my_project/1.cpp -o C:/my_project/build/model/objects/1.obj
I've tried it with/without the Qt symbols.
Does anyone know what might be the problem?
Related:
Compiling Qt5 hello world .cpp file under Windows using MinGW-w64 gives "undefined reference" error
C++ Qt: undefined reference to `_imp___ZN12QApplicationC1ERiPPci'
Compiling Qt5 hello world .cpp file under Windows using MinGW-w64 gives "undefined reference" error
http://www.qtcentre.org/archive/index.php/t-34997.html
Qt5 linking error
Any help is highly appreciated.
There is precisely one way to get it to work without wasting a lot of time:
Use qmake to generate a Makefile for your project.
Ensure that it builds.
Clean up the Makefile according to your needs, if desired.
You're of course free to keep regenerating the Makefile using qmake whenever you alter your sources. The major thing you'll find out is that maintaining makefiles manually is a royal pain, since you also need to maintain the dependency lists and that requires recursively scanning the files included by each translation unit. In other words, whatever Makefiles you come up with yourself will not properly rebuild the project as you modify it. Thus you'll be abandoning the benefits of the Makefile during the development process. Most manually-generated Makefiles are sorely lacking the proper dependencies of each source file - because nobody sane will manually maintain it, and once you're maintaining it automatically, you might as well use qmake or cmake to do the job.
Finally, there's no such thing as Qt that doesn't have qmake, so not using qmake is silly. If you depend on Qt, you're not making anything any easier by pretending that qmake isn't there: your project will use many other binary tools that Qt provides anyway, so giving qmake a special status just wastes your time. If your project is going to be big, you'll save lots of build time by using cmake with the Ninja generator instead, as qmake is currently stuck generating recursive Makefiles, and those lead to poor build performance.

Qt Creator can't find library

I'm using ubuntu(32 bit) on virtualbox. I am trying to add a the rtlsdr library but Qt creator can't find the rtlsdr library.
The library is located in /usr/local/lib/librtlsdr.so and I have tried adding the following to my .pro file
LIBS += -L/usr/local/lib -lrtlsdr
INCLUDEPATH +=usr/local/lib
But I get the following error:
/home/luis/qt-bbb/gcc-linaro-arm-linux-gnueabihf-4.7-2013.04-20130415_linux/bin/arm-linux-gnueabihf-g++ -Wl,-rpath-link=/home/luis/qt-bbb/tslib/lib -Wl,-rpath,/opt/qt-arm/lib -o qt-test main.o mainwindow.o moc_mainwindow.o -L/home/luis/qt-bbb/tslib/lib -L/opt/qt-arm/lib -L/home/luis/qt-arm/rtl-sdr/build/src/ -lrtlsdr -lQtGui -L/opt/qt-arm/lib -L/home/luis/qt-bbb/tslib/lib -lQtNetwork -lQtCore -lpthread
/home/luis/qt-bbb/gcc-linaro-arm-linux-gnueabihf-4.7-2013.04-20130415_linux/bin/../lib/gcc/arm-linux-gnueabihf/4.7.3/../../../../arm-linux-gnueabihf/bin/ld: *skipping incompatible /home/luis/qt-arm/rtl-sdr/build/src//librtlsdr.so when searching for -lrtlsdr*
/home/luis/qt-bbb/gcc-linaro-arm-linux-gnueabihf-4.7-2013.04-20130415_linux/bin/../lib/gcc/arm-linux-gnueabihf/4.7.3/../../../../arm-linux-gnueabihf/bin/ld: *skipping incompatible /home/luis/qt-arm/rtl-sdr/build/src//librtlsdr.a when searching for -lrtlsdr*
Makefile:105: recipe for target 'qt-test' failed
/home/luis/qt-bbb/gcc-linaro-arm-linux-gnueabihf-4.7-2013.04-20130415_linux/bin/../lib/gcc/arm-linux-gnueabihf/4.7.3/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lrtlsdr
collect2: error: ld returned 1 exit status
make: *** [qt-test] Error 1
I'm using ubuntu 32bit to cross compile for my beaglebone running debian. Deploying on my beaglebone works ok and I verified the libraries are 32 bit so I don't know why it can't find them or why it's giving me “skipping incompatible” message. I also tried adding them by right clicking on the .pro->add library but still can't find them.
skipping incompatible tells you the library cannot be linked. Probably compiled with the wrong compiler?
– Sebastian Lange
Ok i figured out my problem, the library was built for x86 and not arm. Thanks for the hints! – luismbedder