我有几个相互依赖的Qt项目。两个项目构建具有不同目的的可执行文件(即我的主应用程序和单元测试)并依赖于相同的库。但是,在构建单元测试时,需要使用不同的编译器设置编译这些库。因此,假设我的应用程序需要一组编译器标志,并且在编译库时这组标志也应该是默认值。我的单元测试应用程序需要另一组(或仅一组)编译器标志,以便在编译它们时设置库。
如何编写qmake *.pro
文件,以便我可以运行qmake一次,然后使用生成的makefile在不同的make运行中为两个应用程序编译库?我正在考虑将默认的制作目标debug
和release
与'默认'标记以及为我的单元测试构建的额外目标test
,但我似乎无法弄清楚如何做这个。我坚持使用Qt 4.8。
一些背景:我使用C ++编程,需要为库中定义的类创建一些模拟对象。但是,这些类不包含virtual
方法,因此我添加了一个预处理程序标志,在编译单元测试时定义为virtual
,否则为空。这样我可以覆盖模拟对象中的方法,但不会在发布版本中遇到虚函数调用的开销。
答案 0 :(得分:0)
可能我的解决方案适合您。 我使用make-file创建我的divert pro-Files。 在项目树中创建程序的示例: 首先是一个适合程序的简单makefile:
SHELL = /bin/bash
BUILD_PATH := $(shell cd ../../../; pwd)
CONF_PATH := $(BUILD_PATH)/sources/config
BIN_PATH := $(BUILD_PATH)/bin
LIB_PATH := $(BUILD_PATH)/lib
INS_PATH := /usr/local
MODULES := core gui widgets printsupport
TARGET := CCDDataDisplay
TEMPLATE := app
DEFINES :=
INCLUDES := $(BUILD_PATH)/sources/include
LIBS := -L$(LIB_PATH) -lhrlLib
DESTDIR := $(BIN_PATH)
MOCS := MainWindow.h
include $(CONF_PATH)/conf_qmake.mk
conf_qmake.mk:
QMAKE := qmake-qt5
HEADERS := $(wildcard *.h)
C_SOURCES := $(wildcard *.c)
CPP_SOURCES := $(wildcard *.cpp)
SOURCES := $(C_SOURCES)
SOURCES += $(CPP_SOURCES)
FORMS := $(wildcard *.ui)
.PHONY: all
all: depdirs
@echo -e "\n\x1b[32;01m Building \x1b[34;01m$(TARGET)\x1b[32;01m in $(shell pwd) ... \x1b[0m"
@if [ ! -d .build ]; \
then \
rm -f qmake.pro; \
cp $(CONF_PATH)/qmake.pro qmake.pro;\
mkdir -p .build; \
cd .build; \
$(QMAKE) -makefile "MODULES_IN=$(MODULES)" \
"TARGET_IN=$(TARGET)" \
"TEMPLATE_IN=$(TEMPLATE)" \
"DEFINES_IN=$(DEFINES)" \
"INCLUDES_IN=$(INCLUDES)" \
"LIBS_IN=$(LIBS)" \
"DESTDIR_IN=$(DESTDIR)" \
"SOURCES_IN=$(SOURCES)" \
"MOCS=$(MOCS)" \
"FORMS_IN=$(FORMS)" \
../qmake.pro >> /dev/null; \
fi;
@$(MAKE) -s -C .build;
.PHONY: depdirs
depdirs:
@$(foreach dir, $(DEPDIRS), $(MAKE) -s -C $(dir);)
.PHONY: install
install:
@$(MAKE) -s -C .build install;
.PHONY: realclean clean distclean
clean:
@if [ -d .build ]; then $(MAKE) -s -C .build clean; fi
@$(RM) *~;
@$(RM) $$(BUILD_PATH)/include/*~;
realclean:
@$(RM) -r .build;
@$(RM) qmake.pro;
@$(RM) *~;
@$(RM) $(BUILD_PATH)/include/*~;
distclean: realclean
@$(RM) $(BUILD_PATH)/bin/$(TARGET);
@$(RM) $(BUILD_PATH)/lib/lib$(TARGET)*;
和最后一次qmake.pro:
QT += $$MODULES_IN
TARGET = $$TARGET_IN
TEMPLATE = $$TEMPLATE_IN
CONFIG += staticlib
DEFINES += $$DEFINES_IN
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
INCLUDEPATH += $$INCLUDES_IN
LIBS += $$LIBS_IN
DESTDIR = $$DESTDIR_IN
SOURCES += $$SOURCES_IN
HEADERS += $$MOCS
FORMS += $$FORMS_IN
unix {
target.path = /usr/local
INSTALLS += target
}
有了这个我只有安静的简单makefile用于生成。 整个项目构建是在一个makefile上完成的 项目树。
也许这有帮助...