C + + to QT compilation Process method

Source: Internet
Author: User

C + + to Qt



QT is a C + + library, QT has a little expansion on the basis of ANSI C + +.

But the domestic seems more impetuous, learning qt A lot even basic C + + How to compile seems to be not very clear. This article discards the constraints of the IDE or Qmake, CMake, and other tools, and attempts to transition from standard C + + to Qt compilation through a few examples.

This article is all about the most basic things, perhaps, as long as you use C + + Qt, regardless of which tool (Qmake, CMake, Boost.build, Qtcreator, vs2008, Eclipse 、...), the content of this article is to be understood ( Although we do not actually write the program, we do not directly use the C + + compiler to compile the QT program.

If you're afraid of the command line.

Example one: Simple console program

A very simple example of a QT extension is useless: (that is, this is a normal C + + program)

#include <QtCore/QCoreApplication> #include <qtcore/qdebug>int main (int argc, char** argv) {    Qcoreapplication app (argc, argv);    Qdebug () << "Hello qt!"; App.exec ();} []http://blog.hehehehehe.cn/a/4033.htm on the net of drinking small wine

As we all know, compiling a C + + program is nothing but compiling preprocessing, compiling, linking

    • Compile preprocessor: Header file path and necessary macros

    • Compiler: Some compilation parameters

    • Linker: Some link parameters and libraries to link to

g++

Simple one-line command to generate Main.exe (under Linux, generate executable program main)

g++ Main.cpp-dqt_core_lib-ie:qt4.7.0include-o Main-le:qt4.7.0lib-lqtcore4

Single-line command, very simple:

    • -I specify header file path

    • -L Specify the library file path

    • -l Specifies the library that needs to be linked

    • -D define the necessary macros (in fact, this macro is not necessary for this applet)

    • -o Specifies the generated executable file name

Cl

Simple one-line command to generate Main.exe

CL Main.cpp-id:/qt/4.7.0/include-dqt_core_lib-femain-link-libpath:d:/qt/4.7.0/lib QtCore4.lib

It's still simple.

    • -I header file path

    • -D Define necessary macros

    • -fe specifying executable program file names

    • -link is followed by linker parameters

      • -libpath Library file path

Example two: a simple GUI program

This time it's a little bit more complicated, not a single console program, but a simple GUI program

    • Main.cpp

#include <QtGui/QApplication> #include "widget.h" int main (int argc, char** argv) {qapplication app (argc, argv);    Widget W;    W.show (); return app.exec ();}
    • Widget.h

#include <qtgui/qwidget>class widget:public qwidget{public:widget (Qwidget * parent=null);};
    • Widget.cpp

#include "widget.h" Widget::widget (Qwidget * parent): Qwidget (parent) {}

Similarly, the program does not use QT extensions, which are compiled directly from the C + + compiler:

g++
g++ main.cpp Widget.cpp-dqt_core_lib-dqt_gui_lib-ie:qt4.7.0-beta2include-o MAIN-LE:QT4.7.0-BETA2LIB-LQTCORE4-LQTG Ui4

Because we used the Qtgui module, we compared it with the previous:

    • Added-dqt_gui_lib and-LQTGUI4.

    • One more file Widget.cpp

Note: Under Windows

This command is available if you are on a non-Windows platform. But under Windows, you know: Sub-console and Windows two link subsystem, and the entry function is divided into main and WinMain.

This command, the compiled Main.exe, will pop up the console. To avoid the console, use the following command:

g++ main.cpp widget.cpp-dqt_core_lib-dqt_gui_lib-dqt_needs_qmain-ie:qt4.7.0-beta2include-o Main-le: Qt4.7.0-beta2lib-lqtcore4-lqtgui4-lqtmain-wl,-subsystem,windows

There are two more options:

    • Qtmain a WinMain function in the library, it calls the main function of our code. That is, for the compiler: the name of the entry function has changed.

    • -wl,-subsystem,windows you know, link windows subsystem

    • For the MinGW, there is a macro qt_needs_qmain here, this thing is very interesting. We mentioned this in detail in the QT Windows link subsystem and entry function (the final version). (Here, but you can ignore it, it's not going to go wrong, and there's no difference to feel)

Cl

As with the g++ under Windows, with the console:

CL main.cpp widget.cpp-id:/qt/4.7.0/include-dqt_core_lib-dqt_gui_lib-femain-link-libpath:d:/qt/4.7.0/lib QtCore4. Lib QtGui4.lib

Without console:

CL Main.cpp widget.cpp-id:/qt/4.7.0/include-dqt_core_lib-dqt_gui_lib-femain/md-link-libpath:d:/qt/4.7.0/lib-subs Ystem:windows qtmain.lib QtCore4.lib QtGui4.lib

Ibid.: Specify link subsystem, enable WinMain entry function

How multi-file programs are managed

What's the downside of calling the compiler directly?

    • Many parameters, each time the manual input, it is inevitable error. (for example, we've used as few parameters as possible, so you're probably dizzying).

    • Second, it's important that everything is recompiled whenever a file is modified.

The traditional way to change this is to write Makefile, and then compile with just the input make, and he will determine which files are changed and need to be recompiled.

The other features that are provided by the IDE itself, such as vs. Here's a quick look at the corresponding makefile file for this example:

Mingw32-make's Makefile File

Cppflags =-dqt_core_lib-dqt_gui_lib-ie:qt4.7.0includeldflags =-le:qt4.7.0lib-lqtcore4-lqtgui4-lqtmain-wl,- Subsystem,windowsobjects = MAIN.O widget.odest = main$ (dest): $ (objects) g++-o [email protected] $ (objects) $ (LDF LAGS)

NMAKE's Makefile file

Cppflags =-id:/qt/4.7.0/include-dqt_core_lib-dqt_gui_lib-mdldflags =-libpath:d:/qt/4.7.0/lib-subsystem:windows  Qtmain.lib QtCore4.lib qtgui4.libobjects = main.obj widget.objdest = main.exe$ (dest): $ (objects) Link $ (objects) $ (ldflags)

Do not introduce this, because makefile writing is also a science. Very difficult to write, all have Qmake, cmake these tools to help us generate makefile files

Example three: introduction of MOC

The extension of Qt to C + + is mainly 3 aspects:

    • Meta-object system, files that contain q_object macros (. h,. CPP, etc.) require MOC preprocessing

    • Resource system,. qrc files require RCC for preprocessing

    • Interface System,. ui files need to be preprocessed by UIC

<p Post-footer "=" "style=" margin-top:0px; margin-bottom:6px; padding:0px; Text-indent:2em; Line-height:normal; " >



This article is from the "11486263" blog, please be sure to keep this source http://11496263.blog.51cto.com/11486263/1837980

C + + to QT compilation Process method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.