Use qmake to quickly generate makefile

Source: Internet
Author: User
Tags automake
Article Title: Use qmake to quickly generate makefile. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
   1. Introduction:
Qmake is a tool created by Trolltech to write Makefile for different platforms and compilers. Is part of the qt toolkit. Anyone who writes programs on Unix and linux may have touched Makefile. It is really convenient to use make to develop and compile programs, but it is not easy to write a Makefile. Handwriting Makefile is difficult and error-prone, this blocks many linux enthusiasts from joining the linux Program Development camp.
Although Open Source Software also has the GNU Automake and GNU Autoconf Software that can generate makefile files, for a simple project, using Automake and Autoconf is a bit cool and also a cool-killing tool. qmake can fully meet your requirements. trolltech uses qmake as the main concatenation tool provided by Qt library and Qt.
  
   2. Install qmake
On the linux platform, qmake has been installed after qt and related qt kits are installed. the only thing you need to pay attention to is the setting of the QTDIR value, which must be set to the place where Qt is installed. For example:/usr/lib/qt3/, and add the PATH of the qmake executable file to the PATH.
  
   3. A simple example
Write hello. c In vi,
# Include
Int main (int argc, char ** argv)
{
Printf ("Hello, world! \ N ");
Return 0;
}
Create the project file (hello. pro) required by qmake ),
SOURCES = hello. cpp
CONFIG + = qt warn_on release
Makefile can be generated by the. pro file as follows:
Qmake-o Makefile hello. pro
Now a Makefile file has been generated in your directory. Enter the "make" command to compile hello. c and execute./hello and world! Open the Makefile file to see if it is very professional!
  
   4. Advanced Operation Skills
Of course, in actual use, our project cannot be as simple as this program. It may have multiple directories, multiple head files, and multiple source files, different link libraries of the linker are required. Don't worry. Let me come with you slowly. These are very easy to implement with qmake. Let's take a more complex project file as an example to explain the Advanced Skills of qmake in detail:
Project File example:
SOURCES + = myqt. cpp
SOURCES + = main. cpp
HEADERS + = myqt. h
FORMS + = xsimform. ui
TEMPLATE = lib
CONFIG + = debug warn_on qt thread x11 plugin
TARGET = ../bin/panel_qt
Export depath =.../xsim.../../xsim/IMdkit
DEFINES = BDB_VERSION4 OS _LINUX
From this file, we can know that the SOURCES variable points to the source file in the project. When there are multiple source files in the project, we need to do this for each source file in the project until the end:
SOURCES + = hello. cpp
SOURCES + = main. cpp
Of course, if you like to use a syntax of the same style as Make, you can also write a line to write a source file, end with a backslash, and then start a new line:
SOURCES = hello. cpp main. cpp
The HEADERS variable points to the header file in the project. When multiple HEADERS are used, the solution for multiple source files is the same.
The FORMS variable points to the form file used in the project (the. ui file designed by qtdesign). qmake also pays attention to the special requirements of Qt and can automatically include the connection rules of moc and uic. If you do not have any or non-qt programs, you can leave them empty.
The TEMPLATE variable tells qmake which makefile is generated for this application. The following are available options:
App-create a makefile for the application. This is the default value, so if the template is not specified, this will be used.
Lib-create a makefile for the Linked Library.
Vcapp-create a Visual Studio project file for the application.
Vclib-create a Visual Studio project file for the library.
Subdirs-this is a special template. It can create an mkefile that can enter a specific directory, generate makefile for a project file, and call make for it.
The CONFIG variable specifies the options used by the compiler and the library to be connected. You can add anything to the configuration variable, but only the following options can be identified by qmake.
The following options control which compiler flags are used:
Release-the application will be connected and programmed in the release mode. If "debug" is specified, it is ignored.
Debug-the application will be connected and programmed in debug mode.
Warn_on-the compiler will output as many warning messages as possible. If "warn_off" is specified, it is ignored.
Warn_off-the compiler outputs as few warning messages as possible.
The following options define the types of libraries/applications to be connected:
Qt-the application is a Qt application, and the Qt library will be connected.
Thread-an application is a multi-threaded application.
X11-the application is an X11 application or library.
Windows-only for "app" templates: an application is a Windows application.
Console-only used for "app" templates: an application is a console application under Windows.
Dll-used only for the "lib" template: the Library is a shared library (dll ).
Staticlib-used only for the "lib" template: the Library is a static library.
Plugin-only used for "lib" template: the Library is a plug-in, which will make the dll option take effect.
The TARGET variable specifies the path and file name of the generated binary code. If a link library is created, it will automatically add "lib" before the file name and automatically add ". so ".
Some other function libraries and linked libraries may be used during usage. The header file of the function library uses the INCLUDEPATH variable. Other linked libraries can be specified using the LIBS variable. For example, LIBS + =-lmath-L/usr/local/lib
The definition variable is the same as the-D option of make.
  
   Conclusion
Autoconf and Automake functions are very powerful, but they are too complicated for common users. Qmake is convenient, simple, and fast. It is a lightweight makefile generation tool. Although it is part of the qt toolkit, it can be used to generate makefile files of other programs, for most people, it is enough. You can also find related. pro project files from many existing source programs provided by qt, which are the best examples for learning more qmake techniques.
This introduction only uses some of the limitations of qmake. I hope this file will help you to have a simple basis for generating Makefile.
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.