Sök…


Standard "pro" -fil.

qmake är ett build-automatiseringsverktyg som levereras med Qt- ramverk. Det gör liknande jobb som verktyg som CMake eller GNU Autotools , men det är utformat för att användas specifikt med Qt . Som sådan är den väl integrerad med Qt- ekosystem, särskilt Qt Creator IDE.

Om du startar Qt Creator och väljer File -> New File or Project -> Application -> Qt Widgets applikation kommer Qt Creator att generera ett projektskelett åt dig tillsammans med en "pro" -fil. "Pro" -filen bearbetas av qmake för att generera filer, som i sin tur behandlas av underliggande build-system (till exempel GNU Make eller nmake ).

Om du namngav projektet "myapp" kommer filen "myapp.pro" att visas. Så här ser en sådan standardfil ut med kommentarer som beskriver varje variabel qmake , tillagd.

# 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

Bevara källkatalogstrukturen i ett build (odokumenterat "object_parallel_to_source" -alternativ).

Om du vill organisera ditt projekt genom att hålla källfiler i olika underkataloger, bör du veta att qmake under en build inte kommer att bevara denna katalogstruktur och det kommer att behålla alla ".o" -filer i en enda build-katalog. Detta kan vara ett problem om du hade motstridiga filnamn i olika kataloger som att följa.

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

Nu beslutar qmake att skapa två "file1.o" -filer i en build-katalog, vilket gör att en av dem skrivs över av en annan. Bulden kommer att misslyckas. För att förhindra detta kan du lägga till CONFIG += object_parallel_to_source konfigurationsalternativ till din "pro" -fil. Detta säger qmake att generera build-filer som bevarar din källkatalogstruktur. På så sätt kommer din build-katalog att återspegla källkatalogstrukturen och objektfiler skapas i separata underkataloger.

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

Komplett exempel.

QT += core
TARGET = myapp
TEMPLATE = app

CONFIG += object_parallel_to_source

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

HEADERS  += src/plugin/file1.h

Observera att object_parallel_to_source CONFIG inte är officiellt dokumenterat .

Enkelt exempel (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.

Kommandorad

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

SUBDIRS-förmågan hos qmake kan användas för att sammanställa en uppsättning bibliotek, som var och en beror på en annan. Exemplet nedan är något invecklat för att visa variationer med förmågan SUBDIRS.

Katalogstruktur

Vissa av följande filer kommer att utelämnas i korthet. De kan antas vara formatet som icke-underkatalogexempel.

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

project.pro

Detta är huvudfilen som möjliggör exemplet. Detta är också filen som skulle ringas med qmake på kommandoraden (se nedan).

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

(*) Se referensdokumentationen för de andra alternativen för ett undermappmål.

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

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

Kommandorad

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

Biblioteksexempel

Ett enkelt exempel för att göra ett bibliotek (snarare än ett körbart, som är standard). TEMPLATE variabeln anger typen av projekt som du gör. lib alternativet tillåter makefile att bygga ett bibliotek.

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

När du bygger ett bibliotek kan du lägga till alternativ dll (standard), staticlib eller plugin till CONFIG .

Skapa en projektfil från befintlig kod

Om du har en katalog med befintliga källfiler kan du använda qmake med -project -optionen för att skapa en projektfil.

Låt oss anta att mappen MyProgram innehåller följande filer:

  • main.cpp
  • foo.h
  • foo.cpp
  • bar.h
  • bar.cpp
  • en underkatalog / foobar.h
  • en underkatalog / foobar.cpp

Sedan genom att ringa

qmake -project

en fil MyProgram.pro skapas med följande innehåll:

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

Koden kan sedan byggas som beskrivs i detta enkla exempel .



Modified text is an extract of the original Stack Overflow Documentation
Licensierat under CC BY-SA 3.0
Inte anslutet till Stack Overflow