Cannot get a QT "hello world" to work! - qt

#include <QtCore/QCoreApplication>
#include <QtGui/QLabel>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QLabel label("Hello world");
label.show();
return a.exec();
}
I am using QTCreator, I installed QT 4.7 on windows.
Now every time I compile I get this error:
:: error: collect2: ld returned 1 exit status
With mingw32 giving me the following
tmp/obj/debug_shared/main.o: In function `main':
E:\Qt\2010.04\qt\QTHello-build-desktop/../QTHello/main.cpp:6: undefined reference to `_imp___ZN6QLabelC1ERK7QStringP7QWidget6QFlagsIN2Qt10WindowTypeEE'
E:\Qt\2010.04\qt\QTHello-build-desktop/../QTHello/main.cpp:8: undefined reference to `_imp___ZN6QLabelD1Ev'
E:\Qt\2010.04\qt\QTHello-build-desktop/../QTHello/main.cpp:8: undefined reference to `_imp___ZN6QLabelD1Ev'
collect2: ld returned 1 exit status
mingw32-make[1]: *** [debug\QTHello.exe] Error 1
mingw32-make: *** [debug-all] Error 2
The process "E:/Qt/2010.04/mingw/bin/mingw32-make.exe" exited with code %2.
Error while building project QTHello (target: Desktop)
When executing build step 'Make'
Whats the problem??

Create a Qt GUI project and olny then write what you have written.

Your project file is probably not including the required stuff. Do you have either
TEMPLATE = app
or
QT += gui
in your .pro file?

Are you linking with QtGui? If you don't, do it.

Also, for UI applications you must use QApplication. QCoreApplication is for CLI-only tools.

You want to create a Hello World app with QT? Really simple:
Create a new GUI app into QtCreator
Open the ui file and add a label using the QtDesigner embedded into QtCreator
Compile and launch
You have your Hello World application done with Qt
Then if you want to know how to write a QTGui application, just open files in project to see what is written in them.

Related

Qt. Copying file error while trying to run the project. How to fix a mistake and why is it happening?

I’m trying to install and create the first Qt project. I am using CLion with CMake and Qt 6.
I set up CMake:
cmake_minimum_required(VERSION 3.17)
project(QSnake)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(QT_VERSION 6)
set(REQUIRED_LIBS Core Gui Widgets)
set(REQUIRED_LIBS_QUALIFIED Qt6::Core Qt6::Gui Qt6::Widgets)
set(CMAKE_PREFIX_PATH D:/Qt/6.0.2/mingw81_64/lib/cmake)
add_executable(${PROJECT_NAME} main.cpp)
if(NOT CMAKE_PREFIX_PATH)
message(WARNING "CMAKE_PREFIX_PATH is not defined, you may need to set it "
"(-DCMAKE_PREFIX_PATH=\"path/to/Qt/lib/cmake\" or -DCMAKE_PREFIX_PATH=/usr/include/{host}/qt{version}/ on Ubuntu)")
endif()
find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED)
target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED})
if(WIN32)
set(DEBUG_SUFFIX)
if (CMAKE_BUILD_TYPE MATCHES "Debug")
set(DEBUG_SUFFIX "d")
endif()
set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}")
if(NOT EXISTS "${QT_INSTALL_PATH}/bin")
set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
if(NOT EXISTS "${QT_INSTALL_PATH}/bin")
set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
endif()
endif()
if(EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll")
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory
"$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll"
"$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
endif()
foreach(QT_LIB ${REQUIRED_LIBS})
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${QT_INSTALL_PATH}/bin/Qt${QT_VERSION}${QT_LIB}${DEBUG_SUFFIX}.dll"
"$<TARGET_FILE_DIR:${PROJECT_NAME}>")
endforeach(QT_LIB)
endif()
Actually, this is how it was generated when the CLion project was created. I only set the path: D:/Qt/6.0.2/mingw81_64/lib/cmake
Just in case, I will also add the main.c code. It is also generated by CLion.
#include <QApplication>
#include <QPushButton>
int
main(int argc,
char *argv[])
{
QApplication a(argc, argv);
QPushButton button("Hello world!", nullptr);
button.resize(200, 100);
button.show();
return QApplication::exec();
}
I don’t see any problems with CMake, but when I try to run the project there is an error:
Error copying file "D:/Qt/6.0.2/mingw81_64/lib/cmake/../../bin/Qt6Cored.dll" to "D:/QSnake/cmake-build-debug".
mingw32-make[3]: *** [CMakeFiles\QSnake.dir\build.make:126: QSnake.exe] Error 1
mingw32-make[3]: *** Deleting file 'QSnake.exe'
mingw32-make[2]: *** [CMakeFiles\Makefile2:96: CMakeFiles/QSnake.dir/all] Error 2
mingw32-make[1]: *** [CMakeFiles\Makefile2:103: CMakeFiles/QSnake.dir/rule] Error 2
mingw32-make: *** [Makefile:137: QSnake] Error 2
What is this error and how do I fix it? I understand that there is no problem with build - only a copying error. And should copied dll be called "Cored" and not "Core"?
Thank you for your attention! If you need any additional information, please let me know - I will try to clarify the question.
Problem solved.
Whether it is useful to anyone, but the CMake automatically generated by CLion contains rows that cause an error in debug mode (adds 'd' suffix to all dll files):
if (CMAKE_BUILD_TYPE MATCHES "Debug")
set(DEBUG_SUFFIX "d")
endif()
Deleting those lines saved me :)
The real answer to this is that you are using a CMake profile defined as "debug", while you have not installed a debug version of Qt.
To solve, and not patch, if you don't want to download the debug DLLs, then create a CMake profile for "release".
I had the same problem and none of the answers here solved the problem.
Here is how I could solved it for myself:
TL;DR:
set prefix-path when creating project
copy missing Qt6Cored.dll into bin-folder
When creating the new project in Clion be sure to set the correct prefix-path:
You can see in your error message that /bin/Qt6Cored.dll gets added to the prefix path automatically, so you don't have to select the bin folder when setting the prefix-path.
After this I still got an error message (but path was displayed correctly without any "/../..").
I then realized that there was no Qt6Cored.dll in my bin-folder:
So I simply copied the missing file from C:\Qt\6.1.3\msvc2019_64\bin into C:\Qt\6.1.3\mingw81_64\bin
And then it worked!
If you don't have the msvc2019_64 folder you can install it by modifying Qt via app-settings:

How do I configure cross-compile toolchain on QtCreator?

I have built a Qt toolchain using Yocto. I installed it and set the environment variables running the script generated by Yocto.
I open QtCreator and configure my cross-compiler kit following these instructions.
I'm unable to build this:
#include <QApplication>
#include <QPushButton>
int main(int argc, char **argv)
{
QApplication a( argc, argv );
QPushButton hello( "Hello world!", 0 );
hello.resize( 100, 30 );
hello.show();
return a.exec();
}
Getting this error:
(.qtversion[qt_version_tag]+0x0):-1: error: undefined reference to `qt_version_tag'
So I try to build this instead:
#include <stdio.h>
int main()
{
printf("Hello world!\n");
return 0;
}
It's ok. But when I deploy to my target and try to run it can't because it hasn't been compiled for target architecture (arm).
helloworld: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ldd
There is also this warning which could be very indicative:
:-1: warning: "/usr/bin/gcc" is used by qmake, but "/opt/poky/2.1.1/sysroots/i686-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc" is configured in the kit.
Please update your kit or choose a mkspec for qmake that matches your target environment better.
I tried to set mkspec on my kit configuration but the result it's the same.
Solved. Maybe I "lied" about saying that setting the mkspec manually it wasn't working neither. That's not true, it does. It's just I was having another issues and couldn't test it properly. The problem was mkspec it wasn't the right. It was pointing to linux-g++ and I fixed it pointing to the proper mkspec. Anyway Qt cretator is telling me that he can't find it! so it's that a bug? The first issue (compiling GUI) was solved too thanks to this.

[ERROR]: CallNamedPipe failed. Error=2 (Qt)

I get the following error every time I run my program in Qt-Creator
[ERROR]: CallNamedPipe failed. Error=2
Usually twice: once at startup and again at shutdown.
I found the error is related to QtCreator itself, as running the same compiled program from the console, it do not generate this message.
I also found it is probably related to some configuration or miss-configuration as the installation on my other machine do not produce this behavior.
Related to:
[ERROR]:CallNamedPipe failed. Error=2 (Qt)
Minimal snippet that produce this behavior:
Project:
QT += core
TARGET = test
TEMPLATE = app
SOURCES += main.cpp
Code:
#include <QDebug>
int main(int , char *[])
{
qDebug() << "Hellow world!";
return 0;
}
Is not that it is very annoying, as the software work correctly and the debugger more or less correctly. But that fill the log with those 2 lines again and again.
I tried to install a messageHandler (qInstallMessageHandler()) but the error is printed outside its scope.
Where this message come from? and more important: how to avoid it?
Additional info: I am on Windows (sad). I have the same behavior using G++ and MSVC2013

Linker error caused by Qt qjpeg4?

I cannot link my project because of some issues with the qjpeg4 library.
Its DLL is located here:
PROJECT_ROOT/../Release/obj/imageformats/qjpeg4.dll
I get this linker error:
Linking CXX executable test.exe
CMakeFiles\test.dir/objects.a(entrypoint.cpp.obj):entrypoint.cpp:(.text.startup+0x2692): undefined reference to `qt_plugin_instance_qjpeg()'
collect2: ld returned 1 exit status
In the file containing main() I have: Q_IMPORT_PLUGIN(qjpeg)
I tried adding these lines, with no improvement:
QApplication app(argc, argv);
QString sDir = QCoreApplication::applicationDirPath();
app.addLibraryPath(sDir + "/plugins");
The previous error should even not depend on the fact that the DLL has not been found, since it is still in the link phase...
If in the file containing main() I remove Q_IMPORT_PLUGIN(qjpeg), the linker is successful, but the executable does absolutely nothing.
The only Qt headers I include are <QApplication> and <QtPlugin>; adding <QtGui> has been useless.
From this link, it seems to be a bug in Qt:
https://bugreports.qt-project.org/browse/QTBUG-24177

qt add path for 3rd party header and libraries

I am using qt and developing a desktop app that will run under win xp/vista.
I have a 3rd party library UserAgentLib (static, and shared). But I am not sure how to link in qt creator.
I have opened the *.pro file and added my library and header path.
The library is called UserAgentLib and the header file is called UserAgentLib.h
TARGET = Dialer
TEMPLATE = app
LIBS += D:\Projects\qtDialer\tools\lib\UserAgentLib
INCLUDEPATH += D:\Projects\qtDialer\tools\inc
SOURCES += main.cpp\
catdialer.cpp
HEADERS += catdialer.h
FORMS += catdialer.ui
I think it does find the header file, as I get about 100 errors for declarations in the UserAgentLib.h file. However, I don't think it is linking with the library.
Many thanks for any suggestions,
======================
I have create a very simple library in VS C++ 2008. Here is the code for the header and source file.
Header:
// mathslibrary.hpp
int add_numbers(const int a, const int b);
Source:
// mathslibrary.cpp
#include "mathslibrary.hpp"
int add_numbers(const int a, const int b)
{
return a + b;
}
I have compiled this into a library. And tested by linking with a WIN32 console application in VS 2008. The library worked as expected.
Now when I try and link with qt.
#include <QtCore/QCoreApplication>
#include <iostream>
#include "mathslibrary.hpp"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::cout << "add numbers 40 + 60 = " << add_numbers(40, 60) << std::endl;
return a.exec();
}
This is my qmake file:
QT -= gui
TARGET = testlibrary
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
LIBS = D:\Projects\TestLibrary\mathsLibrary\Debug\mathsLibrary.lib
INCLUDEPATH = D:\Projects\TestLibrary\mathsLibrary\
SOURCES += main.cpp
These are the errors I get when I try and build:
c:/Qt/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../libmingw32.a(main.o):main.c::-1: error: undefined reference to `WinMain#16'
:-1: error: collect2: ld returned 1 exit status
And these are the compile issues:
Running build steps for project testlibrary...
Creating gdb macros library...
Configuration unchanged, skipping QMake step.
Starting: C:/Qt/mingw/bin/mingw32-make.exe debug -w
mingw32-make: Entering directory `D:/Projects/TestQTLibrary/testlibrary'
C:/Qt/mingw/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory `D:/Projects/TestQTLibrary/testlibrary'
g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -Wl,-subsystem,console -mthreads -Wl -o debug\testlibrary.exe -L"c:\Qt\qt\lib"
D:\Projects\TestLibrary\mathsLibrary\Debug\mathsLibrary.lib -lQtCored4
mingw32-make[1]: Leaving directory `D:/Projects/TestQTLibrary/testlibrary'
mingw32-make: Leaving directory `D:/Projects/TestQTLibrary/testlibrary'
c:/Qt/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../libmingw32.a(main.o):main.c:
(.text+0x104): undefined reference to `WinMain#16'
collect2: ld returned 1 exit status
mingw32-make[1]: * [debug\testlibrary.exe] Error 1
mingw32-make: * [debug] Error 2
Exited with code 2.
Error while building project testlibrary
When executing build step 'Make'
Many thanks for any advice,
Don't know if this changes anything, but maybe you have to define it like this:
LIBS += -LD:/Projects/qtDialer/tools/lib -lUserAgentLib
If you are getting compiler errors then your UserAgentLib.h probably didn't get included. You can test it with:
!exists( UserAgentLib.h ) {
error( "No UserAgentLib.h file found" )
}
You put the above in one of the .pro file and not the constructor.See this.
If the library didn't get linked (which is after your application has compiled well) -- then you need to tinker with your LIBS += ... line, though which appears fine on first glance.
Try this with the simple library first and then try it with the library you are actually trying to get working.
LIBS += D:\Projects\qtDialer\tools\lib\mathsLibrary.lib
In your .hpp file, add extern "C" before your function declarations:
// mathslibrary.hpp
extern "C" int add_numbers(const int a, const int b);
Rebuild the library from Visual Studio.
Now you should be able to compile your test app with Qt Creater. Then copy the corresponding dll into the directory with your new executable and give it a run.
As far as i understood, you generated a dll using MSVC and now you are trying to link it in Qt using mingw. right?
Object files and static libraries created with different compilers, or
even with significantly different releases of the same compiler, often
cannot be linked together. This issue is not specific to MinGW: many
other compilers are mutually incompatible. Build everything from
source with the same version of the same compiler if you can
chech this out : http://chadaustin.me/cppinterface.html

Resources