Quickly generate makefile with QMake

xiaoxiao2021-03-06  14

Quickly generate makefile with QMake

Summary

QMake is a tool for writing makefile for different platforms and compilers created by Trolltech. It is part of the QT toolkit. People who write programs on UNIX & Linux have passed Makefile. It is really convenient to develop and compile the program with make, but it is not easy to write Makefile. Handwriting makefile is more difficult and easy to make mistakes, which blocks a lot of Linux enthusiasts to join Linux program development.

Author: Sun Gaoyong

1 Introduction:

QMake is a tool for writing makefile for different platforms and compilers created by Trolltech. It is part of the QT toolkit. People who write programs on UNIX & Linux have passed Makefile. It is really convenient to develop and compile the program with make, but it is not easy to write Makefile. Handwriting makefile is more difficult and easy to make mistakes, which blocks a lot of Linux enthusiasts to join Linux program development.

Although Open Source Software also has two softwares for GNU Automake and GNU AutoConf to generate a Makefile file, but for a simple item, use Automake and AutoConf and use the slaughter knife. Use qmake to fully meet your requirements. TrollTech Company Use qmake as the main series of tools for the tools provided by QT libraries and QT.

2. Install QMAKE

On the Linux platform, QT and related Qt kits are installed, QMake has been installed. You should pay attention to the setting of the qtdir value, this must be set to the place where the QT is installed. As: / usr / lib / qt3 /, and qmake executable path plus the Path path.

3. A simple example

Write a Hello.c with VI,

#include

INT main (int Argc, char ** argv)

{

Printf (/ "Hello, World! // N /");

Return 0;

}

Create a project file (Hello.Pro) required for qmake,

Sources = hello.cpp

Config = Qt Warn_on Release

Makefile can be generated by this / ".pro/ "file:

qmake -o makefile hello.pro

Now you have generated a Makefile file in your directory, enter / "make /" instructions can start compiling hello.c into execution files, execute ./hello and world sound! Open this Makefile file to see, is it very professional!

4. Advanced operation skills

Of course, in the actual use process, our project is impossible to simply be simple, it may have multiple directories, multiple header files, multiple source files, need to linked its different link libraries, etc. Don't worry, let me go slowly with you. These are very easy to be implemented with qmake. We take QMake's advanced techniques from a more complex project document and your detailed tip of QMAKE:

Sample of the project file:

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

INCLUDEPATH = ../../../../xsim //

. ../../../../xsim/imdkit

Defines = bdb_version4 // os_linux

From this file, you 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 in each source file in the project until the end:

Sources = Hello.cpp

Sources = main.cpp

Of course, if you like to use a grammar like make, you can write it into this, write a source file, and end with the anti-sliding line, then you will get a new line:

Sources = hello.cpp //

Main.cpp

The Headers variable points to the header file in the project, when multiple header files, and the solution to multiple source files is consistent.

The Forms variable points to the form file used in the project (QtDesign design .ui file), QMake also pays attention to QT's special needs, which can automatically include MOC and UIC's compilation rules. No words or non-QT programs can not write.

Template variable tells QMake which makefile generated for this application. The following is available for use:

app - Create an app's makefile. This is the default, so if the template is not specified, this will be used.

LIB - builds a chakefile of a link library.

VcApp - Create a Visual Studio project file for an application.

Vclib - Create a Visual Studio project file for a library.

Subdirs - This is a special template that creates a mkefile that enters a specific directory and generates makefile for a project file and calls Make.

The Config variable variable specifies the options you want to use and the library you want to connect to the compiler. Anything can be added in the configuration variable, but only the following options can be identified by qmake.

Below these options control which compiler logo is used:

Release - The application will be connected in Release mode. If / "debug /" is specified, it will be ignored.

Debug - The application will be connected in DEBUG mode.

The WARN_ON-The compiler outputs as many warning messages as possible. If / "warn_off /" is specified, it will be ignored.

The WARN_OFF-Compiler outputs as little warning information as possible.

Below these options define the type of library / application to be compiled:

Qt - Application is a QT application, and the QT library will be connected.

Thread - Application is a multi-thread application.

X11 - The app is an X11 application or library.

Windows - only for / "app /" template: The application is a window application under Windows.

Console - only for / "app /" template: The application is a console application under Windows.

DLL - only for / "lib /" template: library is a shared library (DLL).

StaticLib - only for / "lib /" template: library is a static library.

Plugin - only for / "lib /" template: library is a plugin, which will make the DLL option take effect.

The Target variable specifies the path and file name of the generated binary code. If it is created a link library, it will automatically add / "lib /" in front of the file name, and in the last automatic plus / ".".

We may use some other function libraries, link libraries, etc. during use. The header file of the function library specifies the use of the includePath variable, and the specification of the other link library can be specified by the libs variable. Specify the specified of the example libs = -lmath -l / usr / local / libdefines variable as the Make's -d option.

Conclude

AutoConf and Automake feature are very powerful, but for ordinary users, it is too complicated. QMake is convenient, simple, fast, is a lightweight Makefile generating tool, although it is part of the Qt toolkit, but it can be used to generate other program Makefile files, for most people, it already It is very enough. You can also find related. Pro project files from many existing source programs provided by Qt, they are the best examples of learning QMake more techniques.

This introduction only uses some fur of qmake, I hope this document can help you have a simple basis for generating makefile.

转载请注明原文地址:https://www.9cbs.com/read-49602.html

New Post(0)