Nice programing

QMake의 하위 디렉토리 템플릿을 사용하는 방법은 무엇입니까?

nicepro 2020. 11. 21. 09:16
반응형

QMake의 하위 디렉토리 템플릿을 사용하는 방법은 무엇입니까?


Qt를 배우기 시작했습니다. 저는 Visual Studio 세계에서 벗어나 QMake를 사용하여 프로젝트 구조를 구성하는 방법을 찾고 있습니다. 'subdirs'템플릿을 찾았지만 이해하기가 꽤 어렵습니다.

내 프로젝트 구조는 다음과 같습니다.

project_dir/
    main.cpp
    project.pro
    logic/
      logic.pro
      some logic files
    gui/
      gui.pro
      gui files

project.pro 는 다음과 같습니다.

TEMPLATE = subdirs
SUBDIRS = logic \
          gui
SOURCES += main.cpp

하위 디렉토리 .pro 파일에는 적절한 SOURCES , HEADERSRESOURCES 변수가 설정되어 있습니다.

.pro 파일에 설정해야하는 TARGET , TEMPLATE 및 기타 필요한 값을 알려주세요 .

또한 공식 튜토리얼 외에 좋은 QMake 튜토리얼이 있습니까?


Troubadour의 의견 외에도 SUBDIRS대상이 하위 디렉터리를 지정하는 데만 유용 하다는 점에 주목합니다 . 따라서 추가 라인

SOURCES += main.cpp

project.pro 파일이 잘못되어 최악의 경우 main.cpp 파일을 빌드하지 못할 수 있습니다. 기껏해야 qmake는 파일에 충돌하는 사양이 있기 때문에 파일 구문 분석을 거부합니다.

나는 SUBDIRS템플릿을 몇 번 사용했으며 , 로직과 GUI를 분리하여 가지고있는 것처럼 다소 독립적 인 라이브러리로 파트를 빌드 할 수 있다면 잘 작동합니다. 이를 수행하는 한 가지 방법은 다음과 같습니다.

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

project.pro :

TEMPLATE = subdirs
SUBDIRS = logic \
          gui

# build must be last:
CONFIG += ordered
SUBDIRS += build

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.a (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.a (in linux).  Uncomment to override.
# TARGET = target

build / build.pro :

TEMPLATE = app

SOURCES += main.cpp

LIBS += -L../logic -L../gui -llogic -lgui

# Will build the final executable in the main project directory.
TARGET = ../project

You use subdirs if the logic and gui folders actually repesent some sort of target, eg. a library, that can be built independently of anything else. If that's the case then just use

TEMPLATE = lib
TARGET = logic
CONFIG += dll

in logic.pro.

If they are not independent targets but are just folders that exist to organise the sources files then you can just use a .pri file in each instead and include them within the .pro using

include(logic/logic.pri)
include(gui/gui.pri)

Just remember that the file paths in the .pri files are relative to the .pro file and not the .pri. BTW, the use of a .pri file is optional as you can still list the files in those folders directly in the .pro file. The .pri file just makes it that bit neater and helps keep the .pro file shorter.

참고URL : https://stackoverflow.com/questions/1417776/how-to-use-qmakes-subdirs-template

반응형