Cmake vs qmake

Source: Internet
Author: User

Cmake vs qmake
    • Qmake is tailored for QT and easy to use.
    • Cmake is not as simple and straightforward as qmake, but it is a powerful function.
      • Built-in out-of source build. (This function is also enabled by default when qtcreator is qmake. Reference: Shadow build of qmake)

      • Provides Conditional compilation for various platforms and scenarios
      • It can process multiple executable files and works well with qttest.

How to choose?

Using cmake to build QT Projects The article says:

    • For simple QT projects, use qmake
    • Cmake

Even so, if a simple QT project doesn't know how to build it with cmake, or how to use cmake for complicated projects. Start with simple learning.

Simple QT Program
#Include <qtcore/Qcoreapplication> 
#Include <qtcore/Qdebug>
Int Main(Int Argc,Char**Argv)
{
Qcoreapplication App(Argc,Argv);
Qdebug() <"Hello QT!";
App.Exec();
}

If you do not use the build tool to directly call the compiler for compilation, you only need a command like this:

G ++ main. cpp-ie: \ QT \ 4.7.0 \ include-O main-Le: \ QT \ 4.7.0 \ Lib-lqtcore4

Specify the header file directory and the library to be linked

Qmake

Qmake requires a. Pro file:

 
Config + = QT
Qt-= Gui
Sources + = Main. cpp
    • Because we need the QT library and header file, we needConfig + = QT.

      • This is the default option. You can remove this line directly.
    • After QT is enabled, you can use QT-= GUI to further tune the required modules.
      • The default value is core GUI. We do not need the GUI module, so we can remove it.
Cmake

Cmake requires a cmakelists.txt file:

 Project(Example) 
Find_package(Qt4 Required)
Set(Qt_dont_use_qtgui True)
Include($ {Qt_use_file})
Add_executable(Example Main. cpp)
Target_link_libraries(Example$ {Qt_libraries})

    • Use find_package to enable qt4
    • The core and GUI are used by default, so the qtgui is disabled manually.
      • These two lines can be used directly Find_package (qt4 components qtcore required), Unspecified modules will be disabled

    • Contains a configuration file provided by cmake for Qt. The $ {qt_use_file} variable is a file name.
    • Add executable program target
    • Link to the QT Library
More complicated

Consider a general QT program:

    • Main. cpp
    • Mainwindows. UI
    • Mainwindows. h
    • Mainwindows. cpp

If manually compiled:

    • Mainwindow. UI requires UIC preprocessing

UIC mainwindow. UI-O ui_main1_1_h
    • Mainwindow. H requires MOC preprocessing

 
MoC mainwindow. H-o moc_main1_1_cpp
    • Call the compiler for compilation

G ++ main. cpp mainwindow. cpp moc_main1_1_cpp-ie: \ QT \ 4.7.0 \ include-O main-Le: \ QT \ 4.7.0 \ Lib-lqtcore4-lqtgui4
Qmake

If qmake is used, a simple pro File

Target = example
Template = app
Sources + = Main. cpp mainwindow. cpp
Headers + = mainwindow. h
Forms + = mainwindow. UI

Whether the files in headers need to be pre-processed by MOC. During the qmake operation, the system automatically determines whether the files contain q_object.

This is why many people do not run qmake again after adding the q_object macro.

Cmake

Check the cmakelists.txt file of cmake.

 Project(Example) 
Cmake_minimum_required(Version 2.6)
Find_package(Qt4 Required)
Include($ {Qt_use_file})
Include_directories($ {Cmake_current_binary_dir})
Qt4_wrap_cpp (example_mocs mainwindow. h)
Qt4_wrap_ui (example_uis mainwindow. UI)
Add_executable(Example Main. cpp Mainwindow. cpp$ {Example_mocs})
Target_link_libraries(Example$ {Qt_libraries})

    • Use qt4_wrap_cpp to process MOC files.

      • The generated file is put in the variable example_mocs, And the last part is linked to the executable program.
    • Use qt4_wrap_ui to process UIC files.
Windows

Because the connection time in Windows is the console and Windows subsystems, some problems in windows need special processing.

When using qmake:

    • The default is Windows subsystem.
    • You can use the console subsystem through config + = console

Use cmake:

    • Console subsystem by default
    • To use Windows subsystems, you must

 
Set (qt_use_qtmain true)
Add_executable (example Win32 main. cpp mainwindow. cpp $ {example_mocs })

The former enables the qtmain. Lib library to provide windows winmain entry functions. The latter is linked to the Windows Subsystem

More complicated
    • Main. cpp
    • Mainwindows. UI
    • Mainwindows. h
    • Mainwindows. cpp
    • Main. qrc
    • Main. RC

Previously, we used the MOC and UIC of QT. This time, we added the resource system to use RCC.

 
RCC main. qrc-O qrc_main.cpp

In addition, the resource file. RC in Windows is used (for example, adding an icon to a program)

    • Use rc.exe in mvsc to process the. RC file
    • Use windres.exe in mingw to process. RC files
Qmake

Target = example
Template = lib
Headers = mainwindow. h widget. h
Sources = Main. cpp widget. cpp mainwindow. cpp
Resources = Main. qrc
Rc_file = Main. RC
Cmake
 Project(Example) 
Cmake_minimum_required(Version 2.6)
Find_package(Qt4 Required)
Set(Qt_use_qtmain True)
Include($ {Qt_use_file})
Include_directories($ {Cmake_current_binary_dir})

If(Mingw)
Set(Cmake_rc_compiler_init Windres)
Enable_language(RC)
Set(Cmake_rc_compile_object
"<Cmake_rc_compiler> <flags>-O coff <defines>-I <source>-O <Object>")
Endif(Mingw)

Set(Example_srcs Main. cpp Mainwindow. cpp Widget. cpp Res/Main. RC)
Set(Example_moc_srcs Mainwindow. h Widget. h)
Qt4_wrap_cpp (example_mocs $ {example_moc_srcs })
Qt4_add_resources (example_rcc_srcs main. qrc)
Set(Example_srcs$ {Example_srcs} $ {Example_mocs} $ {Example_rcc_srcs})

Add_executable(Example Win32 Main. cpp Mainwindow. cpp$ {Example_srcs})
Target_link_libraries(Example$ {Qt_libraries})

    • Use qt4_add_resources to call RCC for preprocessing of QT resource files
    • For Windows resource files, add them to the list just like the source files. Only:
      • This is not enough for mingw. The above mingw block is used to fix this problem.
Debug and releaseqmake

When using qmake, you can set different options for the two modes in the Pro file.

You can directly make release or make debug to compile different versions.

Cmake

Unlike qmake, cmake uses the out-of-source method. Therefore:

    • Create the debug release directory and execute cmake-dcmake_build_type = debug (or release)
    • Make

Cmake_build_type does not work for msvc project generation. After the project is generated, select the mode that comes with IDE.

Reference
    • Http://developer.qt.nokia.com/quarterly/view/using_cmake_to_build_qt_projects

    • Http://www.cmake.org/cmake/help/cmake-2-8-docs.html

    • Http://www.qtcentre.org/wiki/index.php? Title = compiling_qt4_cmd_with_cmake

    • Http://stackoverflow.com/questions/3526794/how-do-i-build-a-win32-app-with-a-resource-file-using-cmake-and-mingw

    • Http://stackoverflow.com/questions/1372105/cmake-variable-or-property-to-discern-betwen-debug-and-release-builds

Http://hi.baidu.com/cyclone/blog/item/41651d95d86028187bf480eb.html

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.