Successfully execute a QT application build with codeblocks on windows - qt

For some reason, I am forced to create a distribution of my application (originally developed on windows with visual studio) running on a Linux server. But even the smallest step towards Linux is full of pain because nothing seems to work anymore. I'm currently trying to transfer my project to the IDE codeblocks on Windows because I want to build the project with GCC first. If I succeed, I am probably able to copy and paste the project on a Linux machine and simply rebuild it.
Unfortunately, I am not able to run a Qt HelloWorld project build with MinGW and Codeblocks. I followed these instructions setting up the project:
http://forums.codeblocks.org/index.php?topic=23059.0
and besides:
http://www.kerrywong.com/2008/07/12/codeblocks-settings-for-qt-development/
There is no problem during the build. However, if I try to execute the application, I immediately get a runtime error "Cannot start the application (0xc0000...7b)"
So I also tried to deploy Qt for windows applications without any effect. What am I missing?
cmd: "C:\Qt\5.11.2\mingw53_32\bin\windeployqt.exe" "C:\Users\Steph\ownCloud\Codeblocks\cb_fail\bin\Debug\cb_fail.exe"
code:
#include <QString>
#include <iostream>
int main(int argc, char* argv[])
{
QString smth = "Hello Qt";
std::cout << smth.toStdString();
}
Codeblocks compiler settings

Related

Qt and LNK1104 cannot open file

I am trying to accomplish the "hello world" tutorial with Qt 5.0.2 for Windows 64-bit (VS 2012, 500 MB) (Info).
However when I press Run the project I have an LNK1104: cannot open file 'debug/Hello.exe' error. However I see that this file is created under project folder "F:\QT\Hello\build-Hello-Desktop_Qt_5_0_2_MSVC2012_64bit-Debug". Thank you in advance
Main.cpp
#include <QApplication>
#include <QPushButton>
int main(int argc, char **argv)
{
QApplication app (argc, argv);
QPushButton button;
button.setText("Hello world !");
button.setToolTip("heheheheheheheh");
button.show();
return app.exec();
}
Hello.pro
TEMPLATE = app
TARGET = Hello
QT = core gui
QT += widgets
SOURCES += \
main.cpp
Most likely cause for the problem: .exe file of your program is still running. Check with Task Manager.End this process from task manager and again try to built it.It will definitely run.
I had the same issue to fix it I went to project right above the run (play button)
clicked on build and switched to debug. Not sure why but it worked for me.
Hope that helped
It might be caused by file permission or linking issues.
Additionally, Microsoft Development Network has an extensive list of possible causes (and solutions) for your problem:
http://msdn.microsoft.com/en-us/library/ts7eyw4s%28v=vs.110%29.aspx

QSqlDatabase works fine in Debug but not Release (QCoreApplication needed)

I have an application that is using SQLite within Qt using QSqlDatabase and everything works just fine if I compile in Debug mode. My problem is that, when I switch to Release mode nothing works. My first errors seemed to be related to the code bringing in the QtCore4.dll and QtGui4.dll files. Once I moved those files into the same directory as the compiled code, the program then loaded but the following error was written to the Qt Creator console:
QSqlDatabase: QSQLITE driver not loaded
QSqlDatabase: available drivers:
QSqlDatabase: an instance of QCoreApplication is required for loading driver plugins
The only line that I think this could be from is this one which I just have at the top to make it global:
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
My PRO file is as follows:
QT += core gui sql
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = LessonsLearned
TEMPLATE = app
SOURCES += main.cpp\
lessonslearned.cpp
HEADERS += lessonslearned.h
FORMS += lessonslearned.ui
I've even tried reinstalling Qt to see if that fixes the problem.
Just to reiterate...everything works just fine if I compile Debug. It's Release that the problems appear.
I'm using Qt 4.7.3 and Visual Studio 2008 in case there's some sort of reason that might cause the issue.
UPDATE
I've moved the declaration of "QSqlDatabase db" inside the header file for the class and have it as private. I've also moved the call to addDatabase("QSQLITE") to the constructor:
LessonsLearned::LessonsLearned(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::LessonsLearned)
{
ui->setupUi(this);
db = QSqlDatabase::addDatabase("QSQLITE");
}
This made the error about needing QCoreApplication dissappear, but I still get the first two messages. Still works in Debug but not Release. I'm wondering if the program is still pointing to a wrong DLL somewhere. I've tried dependency walker some more, but I haven't been able to find one that's wrong, yet.
UPDATE 2
I found something helpful here: QSQLITE driver not loaded - where to put qt database driver plugins. I think this is the same issue that I had. I still don't know why I had to modify the Release directory to add a sqldrivers folder, but it works.
For the release problem:
If bringing QtCode4.dll and QtGui4.dll solved some of the problems then probably bringing the qsqlite4.dll would solve the remaining issue. Just note that you have to put it in a folder named sqldrivers near the executable file.
Probably your issue is that you have a global variable:
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
That means addDatabase() will get called before main(), which means before the QCoreApplication object is created.
So you can only call addDatabase() after you created your QCoreApplication object (which is usually one of the first things in main()).
Avoid global variables, especially ones with complex types and even more so ones that call functions.
I don't know why you only get the problem in release mode though.

Testing a simple qt console program on non-qt-installed pc

The code is very simple:
#include <QApplication>
#include <QDebug>
int main(int argc,char* argv[])
{
QApplication a(argc,argv);
qDebug()<<"Hello world!";
return a.exec();
}
And the .pro file's content is:
CONFIG+=qt debug console
SOURCES+=a.cpp
TARGET=trytoshow
I used the dumpbin utility to find out on which .dll the console program depends:
C:\Program Files\Microsoft Visual Studio 9.0\VC>dumpbin -dependents "G:\Public\H
ello world\trytoshow.exe"
Microsoft (R) COFF/PE Dumper Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file G:\Public\Hello world\trytoshow.exe
File Type: EXECUTABLE IMAGE
Image has the following dependencies:
QtGuid4.dll
QtCored4.dll
MSVCR90D.dll
KERNEL32.dll
Summary
1000 .data
1000 .idata
2000 .rdata
1000 .rsrc
4000 .text
So I put the trytoshow.exe, qtguid4.dll, qtcored4.dll and a folder named "Debug_NonRedist", which is from
C:\Program Files\Microsoft Visual Studio 9.0\VC\redist, into a directory. Then I released it to a non-qt-installed pc.
It does not run.
Did I do anything wrong? Thank you.
As winterTTr already pointed out in the comment to your post, it would be important to know what "It does not run" really means.
There could be more dependencies used by the (Qt) Dll's, so I would suggest to have a look at the ouput of Dependency Walker when you start the program to find out what might be missing (you can use the "Profiling" functionality of the program to do that). You can find it here: http://www.dependencywalker.com/
If you want to use a debug version of your program, you should also use the debug version of the relevant dlls. If i remember correctly, there is debug version of Qt dll, such as QtCored4.dll, QtGui4d.dll, and etc.
On the other hand,
if you find "This application has failed to start because the application configuration is incorrect", normally, this is because the debug version runtime library on the target machine may not be same as where you compile it. So, you can use static runtime library link for your application, that should solve this problem.
For vs2010, Under Configuration Properties, C/C++, Code Generation, Runtime Library -> MultiThreaded (/MTd), which is the /MTd compile parameter for cl.exe. You can also try to find how to set it in qt application.

Problem with code::blocks;Qt4;MingW;The procedure entry point.. could not be located in the dynamic link library QtCore.dll:

I am running Code::Blocks 10.05 with the MingW compiler package on a Win7-32 box.
I downloaded and installed the Qt libs for Windows/MingW (qt-win-opensource-4.7.3-mingw.exe) - installation went smoothly - BUT when it prompted me for the directory for MingW and I pointed it to Code::Blocks installation directory, Qt installer told me my Qt package was for MingW 4.4 and I had 4.4.1 installed - 'installation may not work'. I installed anyhow, figuring there's no significant difference between 4.4 and 4.4.1 and the installation finished without error, all the Qt libs and tools are installed.
Afterwards, in Code::Blocks I created a small test project using the Code::Blocks wizard - here is the code:
#include <QApplication>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
return app.exec();
}
The code built fine in the IDE - no errors or warnings, but when I ran it, I immediately received this error message, and the app exited with an error code:
"The procedure entry point _ZI3qFlagLocationPKc could not be located
in the dynamic link library QtCore.dll"
Anybody know what's going on here? Is it because of the MingW version discrepancy I was warned on, or some other reason? How can I fix this? I want to use Qt Code::blocks, not Nokia QtCreator.
TIA
Add %qtDir%\lib to your path before trying to copy all to system32. Reboot after adding, then all should run fine.
Also, you may need to rebuild qt libs with your compiler if all is still not working...
I used to have that errors, too. And having Qt libs in PATH doesn't seem to help, someone told me to put them into %WINDIR%\system32 and everything works fine after that...
You should create the variables:
QTDIR - C:\Qt\4.7.3 (it's mine, you should write yours)
QMAKESPEC - win32-g++ (for MinGW)
PATH - C:\Qt\4.7.3\bin;C:\mingw\bin

cannot run simple Qt example program, vague error

I'm trying to run a simple example program Qt dialog example.
I compiled it using cmake and nmake, but upon running I get:
Cannot correctly start the application (0xc0150002). Click OK to close the application.
The main.cpp I'm using:
#include <QApplication>
#include "dialog.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Dialog dialog;
return dialog.exec();
}
The simplest thing I can get to work is
#include <QApplication>
#include <iostream>
#include "dialog.h"
using namespace std;
int main(int argc, char *argv[])
{
cout << "test!" << endl;
// QApplication app(argc, argv);
// Dialog dialog;
//return dialog.exec();
return 0;
}
So I can't use anything Qt related, any ideas on how to solve this issue?
Thanks!
It might be that your Qt dlls aren't found when the program starts.
You can check this by copying Qt dll files from Qt's Bin folder to the folder where your program executable is.
Or set the PATH system variable to contain the folder where the Qt libraries are, dlls under windows.
If you are under windows, then there is a tool you can use.
Depedency Walker, to start it, start the "visual studio command prompt" and type "depends"
Drag & Drop your application file to the dependency walker and you should see what dlls it cannot load.
Note that the program isn't always accurate though, but in your case it should work.
I used the tool and found that a few dll's seem to be missing. These being MSVCP90D.DLL, MSVCR90D.DLL, GPSVC.DLL, IESHIMS.DLL.
After some googling it seemed that the problem could be using VS2010 with precompiled Qt binaries for VS 2008. Now I recompiled Qt with VS 2010 but the problem remains the same.
But when recompiling I also compiled the examples, including the one I was trying to get to run. Seems the compiled example of Qt runs perfectly, but my own compiled version keeps giving the same error and the dll's are still missing..
How is this possible, I'm compiling with the same Qt include/binary dir?
The information asked:
the CMakeLists.txt:
cmake_minimum_required (VERSION 2.6)
PROJECT(test)
FIND_PACKAGE(Qt4)
INCLUDE(${QT_USE_FILE})
ADD_DEFINITIONS(${QT_DEFINITIONS})
LINK_LIBRARIES( ${QT_LIBRARIES} )
set(all_SOURCES main.cpp)
QT4_AUTOMOC(${all_SOURCES})
add_executable(Test ${all_SOURCES})
target_link_libraries(Test
${LINK_LIBRARIES})
I'm using Visual Studio 2010 on Windows 7. I'm using the VS2010 command prompt to build and run the executable.
Thanks!

Resources