How to compile a source (cpp) without IDE or qmake but just with command g++...
-
Hello,
I'm very new to QT but I have a little practise in c++. For the first steps, I'd like to create a simple GUI like this:
@#include <QApplication>
#include <QTextEdit>int main(int argv, char **args)
{
QApplication app(argv, args);QTextEdit textEdit; textEdit.show(); return app.exec();
}
@For little codes like this, I really don't want to create projects. I know, I could use qmake now to compile this program, but I'd really like to do it completely alone without any aids.
When I try running the compiler (g++) like this:
@g++ main.cpp -I D:\QT\5.3\mingw482_32\includes\QtWidgets -I D:\QT\5.3\mingw482_32\includes\QtCore -o main.exe@
I get the following linker errors:
@undefined reference to ...
undefined reference to..@I know the problem, the libraries are missing, that is what leads me to this question:
How can I find out which libraries I need depending on which headers I include (in this example: QApplication & QTextEdit) ?I know it is inconvenient and a lot of work to write every lib into the file but I'm interested in it and want to do it on my own.
Any solutions? Thank's a lot for it :)
-
Hi, welcome to devnet.
The libraries correspond to the Qt module of the needed object. In practice you always need at least the core library and (unless it's a console program) the gui module. From there it's just checking where the particular class lives.
The usual suspects are widgets, networking, qtquick etc. The full list is "here":http://qt-project.org/doc/qt-5/modules.html
The error message spits out the class name of the missing link so you can go from there. Go to the doc page an check near the top. It will say something likebq. qmake: QT += core
This means you need core module and so on.
-
Hi Chris,
thanks for your reply! Well, I followed your link and found those two classes QApplication and QTextEdit that I use in my example, they both need the QWidgets lib (qmake += Widgets) and I already include the lib/Qt5Widgets.a. Also I include the lib/Qt5Gui.a, lib/Qt5Core.a and so on...
Is it maybe necessary to include other files like Qt5Widgets.prl or stuff like that?
I always get the following errors:
"undefined reference to _imp___ZN12QApplication4ExecEv
undefined reference to _imp___ZN9QTextEditD1Ev
... and so on...Which includes do I forget?
-
Hi,
I don't know the exact answer, but I suggest you run qmake once. It will produce some Makefiles which you can then study to see how it's done.
The Makefiles contain lines such as:
@
LIBS = -lglu32 -lopengl32 -lgdi32 -luser32 -lmingw32 -lqtmain -LC:/Qt/Qt5.3.0/5.3/mingw482_32/lib -lQt5Widgets -lQt5Gui -lQt5Core
@Note that, if you implement your own QObject subclasses (including QWidget subclasses), you need to run the "meta-object compiler":http://qt-project.org/doc/qt-5/moc.html
-
.prl files are qmake specific files and you don't need to pass them to g++. All you need for linking are the .a files. Core, Gui and Widgets modules should be enough for the errors you pasted.
Can you show the entire command you use? How do you exactly link the libs?
There are subtleties that you need to remember when dealing with the command line directly eg.If you want to link to C:\SomeDir\libFoo.a the options are
-LC:/SomeDir -lFoo
Note no spaces and shortened library name (preceded by lower case L).Also for some options the order you give them in matters.
-
@Chris:
Amazing, really amazing, man! Thank's a lot, I already thought to give it up but your example was all I needed!I was linking libraries like -L followed by absolute lib path with extension... did not know how to link against a qt lib... it is interesting that I even do not need to type the whole file name "libQt5Core" but Qt5Core instead. How does g++ know which file I mean? Or is that the usual way to link against libs? Never seen this in C++...
-
It's not a c++ or Qt thing. It's a gcc thing (copied by others later on).
Why? Who knows? Maybe somebody three decades ago decided it's too much typing and it stayed that way till today. Penguin guys are sometimes weird that way ;) -
[quote author="Chris Kawa" date="1412585275"]Penguin guys are sometimes weird that way ;)[/quote]
Hehe you said it! :)