Ricerca…


Profilo base.

qmake è uno strumento di automazione build, fornito con framework Qt . Funziona in modo simile a strumenti come CMake o GNU Autotools , ma è progettato per essere utilizzato specificamente con Qt . Come tale è ben integrato con l'ecosistema Qt , in particolare Qt Creator IDE.

Se avvii Qt Creator e selezioni File -> New File or Project -> Application -> Qt Widgets , Qt Creator genererà per te un progetto scheletro insieme a un file "pro". Il file "pro" viene elaborato da qmake per generare file, che vengono a loro volta elaborati dai sistemi di compilazione sottostanti (ad esempio GNU Make o nmake ).

Se hai chiamato il tuo progetto "myapp", apparirà il file "myapp.pro". Ecco come appare questo file predefinito, con commenti, che descrivono ogni variabile qmake , aggiunta.

# Tells build system that project uses Qt Core and Qt GUI modules.
QT       += core gui

# Prior to Qt 5 widgets were part of Qt GUI module. In Qt 5 we need to add Qt Widgets module.
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

# Specifies name of the binary.
TARGET = myapp

# Denotes that project is an application.
TEMPLATE = app

# List of source files (note: Qt Creator will take care about this list, you don't need to update is manually).
SOURCES += main.cpp\
        mainwindow.cpp

# List of header files (note: Qt Creator will take care about this list).
HEADERS  += mainwindow.h

# List of "ui" files for a tool called Qt Designer, which is embedded into Qt Creator in newer versions of IDE (note: Qt Creator will take care about this list).
FORMS    += mainwindow.ui

Conservazione della struttura della directory di origine in una build (opzione "object_parallel_to_source" non documentata).

Se ti piace organizzare il tuo progetto mantenendo i file di origine in diverse sottodirectory, dovresti sapere che durante una generazione qmake non preserverà questa struttura di directory e manterrà tutti i file ".o" in una singola directory di compilazione. Questo può essere un problema se avessi nomi di file in conflitto in diverse directory come segue.

src/file1.cpp
src/plugin/file1.cpp

Ora qmake deciderà di creare due file "file1.o" in una directory di build, facendo in modo che uno di essi venga sovrascritto da un altro. Il buld fallirà. Per evitare ciò, puoi aggiungere l'opzione di configurazione CONFIG += object_parallel_to_source al tuo file "pro". Questo dirà a qmake di generare file di build che preservano la struttura della directory di origine. In questo modo la directory di costruzione rifletterà la struttura della directory di origine e i file oggetto verranno creati in sottodirectory separate.

src/file1.o
src/plugin/file1.o

Esempio completo

QT += core
TARGET = myapp
TEMPLATE = app

CONFIG += object_parallel_to_source

SOURCES += src/file1.cpp \
           src/plugin/file1.cpp

HEADERS  += src/plugin/file1.h

Nota che l'opzione CONFIG object_parallel_to_source non è documentata ufficialmente .

Semplice esempio (Linux)

Window.h

#include <QWidget>

class Window : public QWidget
{
    Q_OBJECT
public:
    Window(QWidget *parent = Q_NULLPTR) : QWidget(parent) {}
}

main.cpp

#include <QApplication>
#include "Window.h"

int main()
{
    QApplication app;
    Window window;
    window.show();
    return app.exec();
}

example.pro

# The QT variable controls what modules are included in compilation.
# Note that the 'core' and 'gui' modules are included by default.
# For widget-based GUI applications, the 'widgets' module needs to be added.
QT += widgets  

HEADERS = Window.h # Everything added to the HEADER variable will be checked 
                   # to see if moc needs to run on it, and it will be run if
                   # so.

SOURCES = main.cpp # Everything added to the SOURCES variable will be compiled
                   # and (in the simple example) added to the resulting
                   # executable.

Riga di comando

# Assuming you are in a folder that contains the above files.
> qmake         # You can also add the example.pro file if needed
> make          # qmake creates a Makefile, this runs make on it.
> ./example     # The name of the executable defaults to the .pro file name.

Esempio di SUBDIRS

L'abilità SUBDIRS di qmake può essere utilizzata per compilare un set di librerie, ognuna delle quali dipende da un'altra. L'esempio sotto è leggermente contorto per mostrare variazioni con l'abilità SUBDIRS.

Struttura della directory

Alcuni dei seguenti file saranno omessi nell'interesse della brevità. Si può presumere che siano il formato come esempi non-subdir.

project_dir/
-project.pro
-common.pri
-build.pro
-main.cpp
-logic/
----logic.pro
----some logic files
-gui/
----gui.pro
----gui files

project.pro

Questo è il file principale che abilita l'esempio. Questo è anche il file che verrebbe chiamato con qmake sulla riga di comando (vedi sotto).

TEMPLATE = subdirs  # This changes to the subdirs function.  You can't combine 
                    # compiling code and the subdirs function in the same .pro
                    # file.

# By default, you assign a directory to the SUBDIRS variable, and qmake looks
# inside that directory for a <dirname>.pro file.
SUBDIRS = logic

# You can append as many items as desired.  You can also specify the .pro file
# directly if need be.
SUBDIRS += gui/gui.pro

# You can also create a target that isn't a subdirectory, or that refers to a
# different file(*).
SUBDIRS += build
build.file = build.pro # This specifies the .pro file to use
# You can also use this to specify dependencies.  In this case, we don't want 
# the build target to run until after the logic and gui targets are complete.
build.depends = logic gui/gui.pro

(*) Vedere la documentazione di riferimento per le altre opzioni per un obiettivo di sottodirectory.

common.pri

#Includes common configuration for all subdirectory .pro files.
INCLUDEPATH += . ..
WARNINGS += -Wall

TEMPLATE = lib

# The following keeps the generated files at least somewhat separate 
# from the source files.
UI_DIR = uics
MOC_DIR = mocs
OBJECTS_DIR = objs

logica / logic.pro

# Check if the config file exists
! include( ../common.pri ) {
    error( "Couldn't find the common.pri file!" )
}

HEADERS += logic.h
SOURCES += logic.cpp

# By default, TARGET is the same as the directory, so it will make 
# liblogic.so (in linux).  Uncomment to override.
# TARGET = target

gui / gui.pro

! include( ../common.pri ) {
    error( "Couldn't find the common.pri file!" )
}

FORMS += gui.ui
HEADERS += gui.h
SOURCES += gui.cpp

# By default, TARGET is the same as the directory, so it will make 
# libgui.so (in linux).  Uncomment to override.
# TARGET = target

build.pro

TEMPLATE = app

SOURCES += main.cpp

LIBS += -Llogic -Lgui -llogic -lgui

# This renames the resulting executable
TARGET = project

Riga di comando

# Assumes you are in the project_dir directory
> qmake project.pro # specific the .pro file since there are multiple here.
> make -n2 # This makes logic and gui concurrently, then the build Makefile.
> ./project # Run the resulting executable.

Esempio di libreria

Un semplice esempio per creare una libreria (piuttosto che un eseguibile, che è l'impostazione predefinita). TEMPLATE variabile TEMPLATE specifica il tipo del progetto che stai creando. lib opzione lib consente a makefile di creare una libreria.

library.pro

HEADERS += library.h
SOURCES += library.cpp

TEMPLATE = lib

# By default, qmake will make a shared library.  Uncomment to make the library 
# static.
# CONFIG += staticlib

# By default, TARGET is the same as the directory, so it will make 
# liblibrary.so or liblibrary.a (in linux).  Uncomment to override.
# TARGET = target

Quando si staticlib una libreria, è possibile aggiungere le opzioni dll (default), staticlib o plugin a CONFIG .

Creazione di un file di progetto dal codice esistente

Se hai una directory con i file sorgente esistenti, puoi usare qmake con -project -option per creare un file di progetto.

Supponiamo che la cartella MyProgram contenga i seguenti file:

  • main.cpp
  • foo.h
  • foo.cpp
  • bar.h
  • bar.cpp
  • subdir / foobar.h
  • subdir / foobar.cpp

Quindi chiamando

qmake -project

un file MyProgram.pro viene creato con il seguente contenuto:

######################################################################
# Automatically generated by qmake (3.0) Mi. Sep. 7 23:36:56 2016
######################################################################

TEMPLATE = app
TARGET = MyProgram
INCLUDEPATH += .

# Input
HEADERS += bar.h foo.h subdir/foobar.h
SOURCES += bar.cpp foo.cpp main.cpp subdir/foobar.cpp

Il codice può quindi essere costruito come descritto in questo semplice esempio .



Modified text is an extract of the original Stack Overflow Documentation
Autorizzato sotto CC BY-SA 3.0
Non affiliato con Stack Overflow