Suche…


Standard Profil.

qmake ist ein Build-Automatisierungstool, das mit dem Qt- Framework ausgeliefert wird. Es funktioniert ähnlich wie Tools wie CMake oder GNU Autotools , es wurde jedoch speziell für die Verwendung mit Qt entwickelt . Daher ist es gut in das Qt- Ökosystem integriert, insbesondere in die Qt Creator IDE.

Wenn Sie Qt Creator starten und File -> New File or Project -> Application -> Qt Widgets Anwendung von File -> New File or Project -> Application -> Qt Widgets auswählen, erstellt Qt Creator ein Projektgerüst zusammen mit einer "Pro" -Datei. Die "Pro" -Datei wird von qmake verarbeitet, um Dateien zu generieren, die wiederum von darunter liegenden Build-Systemen (z. B. GNU Make oder nmake ) verarbeitet werden.

Wenn Sie Ihr Projekt "myapp" genannt haben, wird die Datei "myapp.pro" angezeigt. So sieht eine solche Standarddatei aus, mit Kommentaren, die jede qmake- Variable beschreiben.

# 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

Erhalten der Quellverzeichnisstruktur in einem Build (undokumentierte Option "object_parallel_to_source").

Wenn Sie Ihr Projekt organisieren möchten, indem Sie die Quelldateien in verschiedenen Unterverzeichnissen speichern, sollten Sie wissen, dass qmake diese Verzeichnisstruktur während eines Builds nicht beibehält und alle ".o" -Dateien in einem einzigen Build-Verzeichnis speichert. Dies kann ein Problem sein, wenn Sie in unterschiedlichen Verzeichnissen wie folgt folgende Dateinamen miteinander in Konflikt haben.

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

Jetzt beschließt qmake , zwei "file1.o" -Dateien in einem Build-Verzeichnis zu erstellen, wodurch eine von ihnen durch eine andere überschrieben wird. Das Ganze wird versagen. Um dies zu verhindern, können Sie Ihrer "pro" CONFIG += object_parallel_to_source Konfigurationsoption CONFIG += object_parallel_to_source hinzufügen. Dadurch wird qmake angewiesen, Build-Dateien zu generieren, die Ihre Quellverzeichnisstruktur beibehalten. Auf diese Weise spiegelt Ihr Build-Verzeichnis die Quellverzeichnisstruktur wider, und Objektdateien werden in separaten Unterverzeichnissen erstellt.

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

Vollständiges Beispiel.

QT += core
TARGET = myapp
TEMPLATE = app

CONFIG += object_parallel_to_source

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

HEADERS  += src/plugin/file1.h

Beachten Sie, dass object_parallel_to_source CONFIG Option CONFIG nicht offiziell dokumentiert ist .

Einfaches Beispiel (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();
}

beispiel.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.

Befehlszeile

# 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.

SUBDIRS Beispiel

Die SUBDIRS-Fähigkeit von qmake kann verwendet werden, um eine Reihe von Bibliotheken zu kompilieren, die jeweils voneinander abhängig sind. Das folgende Beispiel ist leicht gewunden, um Variationen mit der SUBDIRS-Fähigkeit zu zeigen.

Verzeichnisaufbau

Einige der folgenden Dateien werden der Kürze halber weggelassen. Es kann davon ausgegangen werden, dass sie das Format als Beispiele für Nicht-Unterverzeichnisse sind.

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

project.pro

Dies ist die Hauptdatei, die das Beispiel ermöglicht. Dies ist auch die Datei, die mit qmake in der Befehlszeile (siehe unten) aufgerufen würde.

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

(*) Weitere Informationen zu einem Subdirs-Ziel finden Sie in der Referenzdokumentation .

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

logic / 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

Befehlszeile

# 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.

Bibliotheksbeispiel

Ein einfaches Beispiel zum Erstellen einer Bibliothek (und nicht einer ausführbaren Datei, die die Standardeinstellung ist). TEMPLATE Variable TEMPLATE gibt den Typ des Projekts an, das Sie TEMPLATE . lib Option lib können Sie mit Makefile eine Bibliothek erstellen.

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

Wenn Sie eine Bibliothek staticlib , können Sie die Optionen dll (Standard), staticlib oder plugin zu CONFIG .

Projektdatei aus vorhandenem Code erstellen

Wenn Sie ein Verzeichnis mit dem bestehenden Quelldateien haben, können Sie qmake mit dem -project -option zu einer Projektdatei erstellen.

Nehmen wir an, der Ordner MyProgram enthält die folgenden Dateien:

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

Dann durch anrufen

qmake -project

Eine Datei MyProgram.pro wird mit folgendem Inhalt erstellt:

######################################################################
# 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

Der Code kann dann wie in diesem einfachen Beispiel beschrieben erstellt werden .



Modified text is an extract of the original Stack Overflow Documentation
Lizenziert unter CC BY-SA 3.0
Nicht angeschlossen an Stack Overflow