How to compile a Qt application through the command line with MinGW? - qt

I have downloaded the latest Qt version for MinGW, and I have the correct MinGW version which is compatible with Qt. When I try to make the project, g++ is unable to find my source file even if it's in the same folder as the project file.
Those are the steps I followed (all project and source files are in T:\QtTest ):
T:\QtTest> qmake -project
T:\QtTest> qmake
T:\QtTest> make
And the output is this:
T:\QtTest> make
mingw32-make -f Makefile.Debug
mingw32-make1: Entering directory `T:/QtTest’
g++ -c -g -frtti -fexceptions -mthreads -Wall -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 -DQT_NEEDS_QMAIN -I“c:\Qt\4.8.0\include\QtCore” -I“c:\Qt\4.8.0\include\QtGui” -I“c:\Qt\4.8.0\include” -I”.” -I“c:\Qt\4.8.0\include\ActiveQt” -I“debug” -I“c:\Qt\4.8.0\mkspecs\default” -o debug\main.o main.cpp
g++: main.cpp: no such file or directory
g++: no input files
mingw32-make1: *** [debug/main.o] Error 1
mingw32-make1: Leaving directory `T:/QtTest’
mingw32-make: *** [debug] Error 2
I have no idea why it can't find "main.cpp" file when it's in the same directory as the
project files. Even if I replace "main.cpp" with the full path ("T:\QtTest\main.cpp") it still won't find it. What am I doing wrong?
The following is my project structure:
(main.cpp is the only file that I wrote, all the others were generated by qmake)
T:\QtTest\main.cpp
T:\QtTest\Makefile
T:\QtTest\Makefile.debug
T:\QtTest\Makefile.release
T:\QtTest\QtTest.pro
T:\QtTest\debug\
T:\QtTest\release\
My g++ version is 4.4.0, which is the version suggested by the Qt installer and available for download in the same page as the Qt for MinGW installer.

Problem solved.
It had nothing to do with MinGW or Qt or the makefiles generated by qmake. I found it was caused solely by a custom entry in the Windows registry. I'm posting this solution for anyone who encounters the same problem:
Sometime ago I had created an entry in the Windows registry under HKEY_CURRENT_USER \ Software \ Microsoft \ Command Processor called Autorun, which makes CMD.exe start in a custom working directory, which was something I wanted to do (so I followed the steps detailed in this page about "How to change the default startup directory for Command Prompt": http://windowsxp.mvps.org/autoruncmd.htm).
Well, I completely neglected the CAUTION part in that page, which states that "Changing the current directory using Autorun value as mentioned in this article, might affect the functionality of batch scripts". Yes, shame on me.
So, if you have the same problem of being unable to make your Qt projects using qmake, and everything else looks OK in your project structure and makefiles, verify that you don't have something in the Windows registry which might change the startup directory for the command prompt.

Do you have MSYS installed? If sh.exe from MSYS is on the system path, it may conflict with the interpretation of windows style paths. Check the documentation here.
If that isn't the problem, then delete everything in your project folder except the source files and start over again with qmake -project. After that step, open the .pro file and verify that things look correct. For a very basic project, I get something like this:
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
# Input
HEADERS += widget.h
FORMS += widget.ui
SOURCES += main.cpp widget.cpp
The next two steps, running qmake and then make should work correctly. If not, there is nothing wrong with the steps you followed and there is something wrong with your system/environment.

Related

gcc Compling and linking object file and source file at the same time

In a nutshell , the issue is I have some object file (say a.o , b.o ) and some source file (f.c and g.c)
I have to compile and link in a single step.
This is what I am doing but I don't think this is the write way atleast I should give something like -l or -L for linking
gcc -Wall -O0 -ggdb -fPIC a.o b.o f.c g.c -o executable
This is the correct way, -l is used to link to a library (for example, the ptheads library needs -lpthread) and -L is used to add directories to the directories in which the linker looks for library files.
There is nothing wrong with compiling the software this way.
Most build systems build files one at a time in order to avoid having to rebuild the object file in case the source file was not modified and the object file still exists. If you are fine with rebuilding code, then your approach is perfectly valid.

How to put gstab+ option to mingw makefile generated by qmake

I am newby in Qt, and try to compile tutorial application with MinGW. The only problem, that I need to put -gstab+ option to gcc compiler instead of -g option that qmake generated by default.
Currently, I select debug configuration (it add CONFIG += debug to the qmake command-line).
I've tried to add CONFIG += gstab+ to the .pro file, but it does not work. qmake generates makefile.Debug file with -g option.
What do I make wrong? And how should it be?
I've found a solution here http://qt-project.org/forums/viewthread/2412
I've added the following lines to my .pro file:
QMAKE_CXXFLAGS_DEBUG += "-gstabs+"
QMAKE_CFLAGS_DEBUG += "-gstabs+"

How to solve undefined reference errors when linking log4cxx in qmake project

It's the second day I'm trying to marry Qt5, MinGW and log4cxx.
Even after I've compiled everything successfully, linked apr, apr-util and log4cxx libraries, ld gives me a bunch of "undefined reference" problems.
It looks like different settings were specified during log4cxx compilation (I'm using ant).
Was anyone able to successfully compile and use log4cxx with MinGW?
Environment:
log4cxx trunk
apr 1.4.6
apr-util 1.5.2
latest MinGW
I'm using Qt 5 with latest MinGW as a compiler
What I've done:
compiled log4cxx using ant with the following command: "ant
-Dcompiler=gcc -Dfind=false -DLOG4CXX_STATIC=1 -Dlib.type=static"
added result libraries to my project in pro file: "LIBS +=
-L../log4cxx/lib LIBS += -llibapr-1 -llibaprutil-1 -lliblog4cxx"
And now, as I try to link my project, I'm getting the next:
*C:/mingw/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory 'C:/Work/SPP_Development/AutoHaul/Sub-systems/TCS/Source/build-SimulatorEngine-Standalone_MinGW-Debug'
g++ -Wl,-subsystem,windows -mthreads -o debug\SimulatorEngine.exe object_script.SimulatorEngine.Debug -lmingw32 -lqtmaind -L../log4cxx/lib -llibapr-1 -llibaprutil-1 -lliblog4cxx -LC:\Qt\Qt5.0.1\5.0.1\mingw47_32\lib -lQt5XmlPatternsd -lQt5Widgetsd -lQt5Networkd -lQt5Xmld -lQt5Guid -lQt5Cored -llibEGLd -llibGLESv2d -lgdi32 -luser32
Makefile.Debug:200: recipe for target 'debug\SimulatorEngine.exe' failed
mingw32-make[1]: Leaving directory 'C:/Work/SPP_Development/AutoHaul/Sub-systems/TCS/Source/build-SimulatorEngine-Standalone_MinGW-Debug'
Makefile:34: recipe for target 'debug' failed
../log4cxx/lib/liblog4cxx.lib(mutex.o): In function `ZN7log4cxx7helpers5MutexC2ERNS0_4PoolE':
c:/Work/log4cxx/apache-log4cxx-trunc/src/main/cpp/mutex.cpp:35: undefined reference to `apr_thread_mutex_create#12'
../log4cxx/lib/liblog4cxx.lib(mutex.o): In function `ZN7log4cxx7helpers5MutexC2EP10apr_pool_t':
c:/Work/log4cxx/apache-log4cxx-trunc/src/main/cpp/mutex.cpp:45: undefined reference to `apr_thread_mutex_create#12'
../log4cxx/lib/liblog4cxx.lib(mutex.o): In function `ZN7log4cxx7helpers5MutexD2Ev':
c:/Work/log4cxx/apache-log4cxx-trunc/src/main/cpp/mutex.cpp:55: undefined reference to `apr_thread_mutex_destroy#4'*
It looks like log4cxx library cannot findfunctions declared and defined in the apr library by whatever reason.
Is there any way to analyze the problem further to see why is this happening?
I will describe the 1st problem you have and give the solution for it. If it does not solve your problem, do not revert back because that would be just the 1st step of fixing process, and I will gradually expand the answer to solve more incoming issues as long as you provide relevant feedback on each fix until we finally nail it down. So, lets begin.
First of all, you add libraries into LIBS variable in a wrong way. You have 2 options to do it right:
#1
LIBS += $${PWD}/../log4cxx/lib/libapr-1.a
LIBS += $${PWD}/../log4cxx/lib/libaprutil-1.a
LIBS += $${PWD}/../log4cxx/lib/liblog4cxx-1.a
#2
LIBS += -L$${PWD}/../log4cxx/lib
LIBS += -lapr-1
LIBS += -laprutil-1
LIBS += -llog4cxx-1
NOTE: Of course one liner is possible too:
LIBS += -L$${PWD}/../log4cxx/lib -lapr-1 -laprutil-1 -llog4cxx-1
NOTE: Using line continuation (\) for readability is possible too:
LIBS += -L$${PWD}/../log4cxx/lib \
-lapr-1 \
-laprutil-1 \
-llog4cxx-1
You need to take care of the proper link order: liblog4cxx depends on libapr*, so the libapr entries need to come after liblog4cxx:
LIBS += -L../log4cxx/lib \
-llog4cxx-1 \
-lapr-1 \
-laprutil-1
The reason is that dependencies are resolved left to right, so that in your case the apr libraries have been read, and forgotten, when log4cxx comes along that has some external symbols that need resolving. ld won't read the apr libraries again (has to do with cyclic dependency issues, and historical reasons).
To test if this works, you can try running the command
g++ -Wl,-subsystem,windows -mthreads -o debug\SimulatorEngine.exe object_script.SimulatorEngine.Debug -lmingw32 -lqtmaind -L../log4cxx/lib -lliblog4cxx -llibapr-1 -llibaprutil-1 -LC:\Qt\Qt5.0.1\5.0.1\mingw47_32\lib -lQt5XmlPatternsd -lQt5Widgetsd -lQt5Networkd -lQt5Xmld -lQt5Guid -lQt5Cored -llibEGLd -llibGLESv2d -lgdi32 -luser32
from the directory
C:/Work/SPP_Development/AutoHaul/Sub-systems/TCS/Source/build-SimulatorEngine-Standalone_MinGW-Debug
yourself first. But perhaps just modifying the .pro file is simplest.

Qt Creator Error

Launching debugging for a project and then,
Running build steps for project Text_Editor...
Starting: "c:/qt/2010.05/qt/bin/qmake.exe" C:/Qt/2010.05/Text_Editor/Text_Editor.pro -r -spec win32-g++
The process "c:/qt/2010.05/qt/bin/qmake.exe" exited normally.
Starting: "C:/Qt/2010.05/mingw/bin/mingw32-make.exe" -w
mingw32-make: Entering directory `C:/Qt/2010.05/Text_Editor-build-desktop'
C:/Qt/2010.05/mingw/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory `C:/Qt/2010.05/Text_Editor-build-desktop'
g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\qt\include\QtCore" -I"..\qt\include\QtGui" -I"..\qt\include" -I"..\qt\include\ActiveQt" -I"debug" -I"..\Text_Editor" -I"." -I"..\qt\mkspecs\win32-g++" -o debug\main.o ..\Text_Editor\main.cpp
mingw32-make[1]: Leaving directory `C:/Qt/2010.05/Text_Editor-build-desktop'
mingw32-make: Leaving directory `C:/Qt/2010.05/Text_Editor-build-desktop'
g++: ..\Text_Editor\main.cpp: No such file or directory
g++: no input files
mingw32-make[1]: *** [debug/main.o] Error 1
mingw32-make: *** [debug] Error 2
The process "C:/Qt/2010.05/mingw/bin/mingw32-make.exe" exited with code %2.
Error while building project Text_Editor (target: Desktop)
When executing build step 'Make'
I have no clue on that. I just installed it, created a new project and got that error instantly.
I had pretty much the same problem just now.
Without getting into "why" - I unticked "Shadow build" from project options and my build was succesful. Funny part is - "Shadow build" worked yesterday, and nothing really changed that much in between.
For reason or another it is not able to find your main.cpp (g++: ..\Text_Editor\main.cpp: No such file or directory). Basically that path should be ok if you have shadow build toggled on.
Location where the main.cpp is tried to find is:
C:/Qt/2010.05/Text_Editor/main.cpp
Double check that this is proper location (notice also case sensitivity to be sure)
you can try toggling off the shadow build from project -> check off shadow build. And also you should check that Text_Editor.pro contains properly the main.cpp.
Edit: It might be also wise to create that project outside the Qt binary folder, to some other more logical folder.
Try to change your build directory from C:/Qt/2010.05/Text_Editor-build-desktop to C:/Qt/2010.05/Text_Editor.
To do this just open your project in Qt creator, left click on Project on the left pane, then open build settings tab and edit Build directory input field.
What worked for me was the way I added headers in a .pri.
This works...
HEADERS += ../serialportwidget.h
HEADERS += ../serialportworker.h
SOURCES += ../serialportwidget.cpp
SOURCES += ../serialportworker.cpp
or this
HEADERS += ../serialportwidget.h \
../serialportworker.h
SOURCES += ../serialportwidget.cpp \
../serialportworker.cpp
This doesn't...
HEADERS += ../serialportwidget.h \ ../serialportworker.h
SOURCES += ../serialportwidget.cpp \ ../serialportworker.cpp
Run Qt Creator with "Run as Administrator".
Problem Solved for [debug/main.o] error5
-
Vivin NL

Basic compile issue with QT4

I've been trying to get a dead simple listing from a university textbook to compile with the newest QT SDK for Windows I downloaded last night. After struggling through the regular nonsense (no make.bat, need to manually add environment variables and so on) I am finally at the point where I can build. But only one of the two libraries seem to work.
The .pro file I use is dead simple:
SUBDIRS += utils \
dataobjects
TEMPLATE = subdirs
In each of these two subfolders I have the source for a library. Running QMAKE generates a makefile and running Make runs through all the preliminaries and then fails on the g++ call:
g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc --out-implib,libdataobjects.a -shared -mthreads -Wl -Wl,--out-implib,c:\Users\Cobus\workspace\lib\libdataobjects.a -o ..\..\lib\dataobjects.dll object_script.dataobjects.Debug -L"c:\Users\Cobus\Portab~1\Qt\2010.02.1\qt\lib" -LC:\Users\Cobus\workspace\lib -lutils -lQtXmld4 -lQtGuid4 -lQtCored4
c:/users/cobus/portab~1/qt/2010.02.1/mingw/bin/../lib/gcc/mingw32/4.4.0/../../../../mingw32/bin/ld.exe: cannot find -lutils
The problem seems to be right near the end of the command line, where -lutils is added, indicating that there is a library by the name of utils. While I would have expected to see that, you'll notice the library names after --out include lib in the name, so they become libutils and libdataobjects. I have tried to figure out why this is happening, to no avail.
Anyone have an idea what's going on?
You .pro file uses the SUBDIRS template which requires a .pro file matching the directory name for each subdir. You should have a utils\utils.pro and dataobjects\dataobject.pro file as well.
Is c:\Users\Cobus\workspace\lib\libdataobjects.a built correctly? If not, check the sub directory .pro files.
With what you posted, I would not expect anything to compile. You need targets in the sub-directory .pro files to build anything.
Their seems to be another pro file as the generated Makefile is pulling QtXml which it wont do by default.
Please check the subdirs for any .pro or .pri files .. one of them will have answer to your confusion

Resources