简体   繁体   English

如何将 qmake 文件与谷歌测试和共享库一起使用

[英]How to use qmake file with google test and shared library

I started with a makefile that would generate the dependencies of my C++ files.我从 makefile 开始,它将生成我的 C++ 文件的依赖项。 It was a C++ project using google test.这是一个使用谷歌测试的 C++ 项目。 Later, I started a Qt project which uses qmake and links to a shared library which the old makefile builds.后来,我开始了一个 Qt 项目,它使用 qmake 并链接到旧 makefile 构建的共享库。 Needless to say, the old makefile is really complicated now.不用说,旧的 makefile 现在真的很复杂。

I would like to make a qmake file which can do the following:我想制作一个可以执行以下操作的 qmake 文件:

  • Build a shared library for a list a sources为列表源构建共享库
  • Build google test (optionally, I would accept a separate makefile for this)构建谷歌测试(可选,我会为此接受单独的 makefile)
  • Build my Qt executable with a different list of sources linking to the first shared library使用链接到第一个共享库的不同源列表构建我的 Qt 可执行文件
  • All builds should have debug and release versions which will output to different directories所有构建都应该有调试和发布版本,这些版本将 output 到不同的目录

Can someone point me in the right direction for making a *.pro file which will do that?有人可以指出我制作 *.pro 文件的正确方向吗? I'm really not clear on how to do things like multiple targets in qmake.我真的不清楚如何在 qmake 中执行多个目标等操作。

Here is the current makefile I am using (clearly a mess):这是我正在使用的当前 makefile(显然是一团糟):

GTEST_DIR = /home/matt/lib/gtest-1.5.0
GMOCK_DIR = /home/matt/lib/gmock-1.5.0
SRC_DIR = /home/matt/Documents/myproject

QTINC := -I/usr/share/qt4/mkspecs/linux-g++ -I/usr/include/qt4/QtCore \
    -I/usr/include/qt4/QtGui -I/usr/include/qt4

TEST_SRCS = test/TestRunner.cpp test/CellTest.cpp test/PuzzleTest.cpp \
    test/SingleCandidateMethodTest.cpp test/ExclusionMethodTest.cpp \
    test/BlockIntersectionMethodTest.cpp test/CoveringSetMethodTest.cpp \
    test/SimpleValidatorTest.cpp test/PuzzleMarkerTest.cpp \
    test/PlayerValidatorTest.cpp test/SolverHelperTest.cpp \
    test/GuessCommandTest.cpp test/MarkCommandTest.cpp \
    test/UnmarkCommandTest.cpp test/MethodSolverTest.cpp \
    test/SimplePuzzleImporterTest.cpp test/SolvedPuzzleImporterTest.cpp \
    test/AddHintMarksCommandTest.cpp test/CellControllerTest.cpp \
    test/PuzzleControllerTest.cpp
QT_SRCS = 
LIB_SRCS = Puzzle.cpp Cell.cpp SingleCandidateMethod.cpp ExclusionMethod.cpp \
    BlockIntersectionMethod.cpp CoveringSetMethod.cpp SimpleValidator.cpp \
    PuzzleMarker.cpp PlayerValidator.cpp SolverHelper.cpp GuessCommand.cpp \
    MarkCommand.cpp UnmarkCommand.cpp MethodSolver.cpp \
    SimplePuzzleImporter.cpp SolvedPuzzleImporter.cpp GameManager.cpp \
    CellController.cpp AddHintMarksCommand.cpp GameController.cpp \
    PuzzleController.cpp

DEPDIR = .deps
df = $(DEPDIR)/$(@F)

# preprocessor
CPPFLAGS += -I$(GTEST_DIR)/include -I$(GMOCK_DIR)/include -I$(SRC_DIR) $(QTINC)

# C++ compiler
CXXFLAGS = -Wall -std=c++0x
# qt defines
QTDEF = -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED
# stuff to link for Qt
QTFLAGS = -L/usr/lib -lQtCore -lQtGui -lpthread

# gtest headers, don't need to change
GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
                $(GTEST_DIR)/include/gtest/internal/*.h
# gmock stuff, don't need to change
GMOCK_HEADERS = $(GMOCK_DIR)/include/gmock/*.h \
                $(GMOCK_DIR)/include/gmock/internal/*.h \
                $(GTEST_HEADERS)

MAKEDEPEND = $(CXX) $(CPPFLAGS) -MM -o $(df).d $<
MAKEDEPEND_TEST = $(CXX) $(CPPFLAGS) -MM -o $(df).d -MT $(basename $<).o $<
MAKEDEPEND_QT = $(CXX) $(CPPFLAGS) -MM -o $(df).d -MT $(basename $<).o $<

SRCS := main.cpp $(LIB_SRCS)
OBJS := $(SRCS:%.cpp=%.o)
LIB_OBJS := $(LIB_SRCS:%.cpp=%.o)
QT_OBJS := $(QT_SRCS:%.cpp=%.o)
TEST_OBJS := $(TEST_SRCS:%.cpp=%.o)

# targets:
debug : CXXFLAGS += -g -O0
# removed this warning because it sucks: -Wconversion (int to size_t!)
debug_warn : CXXFLAGS += -pedantic -Wextra 
debug_warn : debug
debug : all
release : CXXFLAGS += -O2
release : all

lib : CXXFLAGS += -fPIC
lib : libSudokuLib.so

libSudokuLib.so : $(LIB_OBJS)
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -shared -o libSudokuLib.so $(LIB_OBJS)

all : sudoku run_tests

sudoku : $(OBJS) $(QT_OBJS)
    $(CXX) $(CPPFLAGS) $(QTDEF) $(CXXFLAGS) $(QTFLAGS) $^ -o $@

run_tests : $(LIB_OBJS) $(TEST_OBJS) gtest.a gmock.a
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@

# dependency stuff
.D_TARGET:
    mkdir -p $(DEPDIR)
    touch $@

.PRECIOUS: .D_TARGET

# GTEST building stuff don't touch me
GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)
GMOCK_SRCS_ = $(GMOCK_DIR)/src/*.cc $(GMOCK_HEADERS)

gtest-all.o : $(GTEST_SRCS_)
    $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) -I$(GMOCK_DIR) $(CXXFLAGS) -c \
           $(GTEST_DIR)/src/gtest-all.cc

gmock-all.o : $(GMOCK_SRCS_)
    $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) -I$(GMOCK_DIR) $(CXXFLAGS) \
            -c $(GMOCK_DIR)/src/gmock-all.cc

gmock_main.o : $(GMOCK_SRCS_)
    $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) -I$(GMOCK_DIR) $(CXXFLAGS) \
            -c $(GMOCK_DIR)/src/gmock_main.cc

gmock.a : gmock-all.o gtest-all.o
    $(AR) $(ARFLAGS) $@ $^

gtest_main.o : $(GTEST_SRCS_)
    $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
           $(GTEST_DIR)/src/gtest_main.cc

gtest.a : gtest-all.o
    $(AR) $(ARFLAGS) $@ $^

gtest_main.a : gtest-all.o gtest_main.o
    $(AR) $(ARFLAGS) $@ $^

# QT stuff
%Qt.o : %Qt.o .D_TARGET
    $(MAKEDEPEND_QT);
    @cp $(df).d $(df).P;
#   sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
#       -e '/^$$/ d' -e 's/$$/ :/' < $(df).d >> $(df).P; 
    @rm -f $(df).d
    $(CXX) $(CPPFLAGS) $(QTDEF) $(CXXFLAGS) -o $@ -c $<

# tests
%Test.o : %Test.cpp .D_TARGET $(GMOCK_HEADERS)
    $(MAKEDEPEND_TEST);
    @cp $(df).d $(df).P;
#   sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
#       -e '/^$$/ d' -e 's/$$/ :/' < $(df).d >> $(df).P; 
    @rm -f $(df).d
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ -c $<

# objects from sources
%.o : %.cpp .D_TARGET
    $(MAKEDEPEND);
    @cp $(df).d $(df).P; \
#   sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
#       -e '/^$$/ d' -e 's/$$/ :/' < $(df).d >> $(df).P; 
    @rm -f $(df).d
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ -c $<

-include $(QT_SRCS:%.cpp=$(DEPDIR)/%.o.P)
-include $(TEST_SRCS:test/%.cpp=$(DEPDIR)/%.o.P)
-include $(SRCS:%.cpp=$(DEPDIR)/%.o.P)

clean:
    $(RM) $(OBJS) $(TEST_OBJS) $(QT_OBJS) \
        gtest.a gtest_main.a gtest-all.o gtest_main.o \
        .D_TARGET sudoku run_tests
    rm -rf $(DEPDIR)

And here is the project.pro file for qmake (which relies on that first makefile instead of building the library itself)这是 qmake 的 project.pro 文件(它依赖于第一个 makefile 而不是构建库本身)

TEMPLATE = app
TARGET = qtsudoku
DEPENDPATH += .
INCLUDEPATH += . ../myproject
CONFIG += qt warn_on debug
QMAKE_CXXFLAGS += -std=c++0x
LIBS += -L/home/matt/Documents/myproject -lSudokuLib

# Input
HEADERS += QtPuzzleModel.h QtPuzzleView.h QtGameApplication.h QtDirector.h \
    QtMainWindow.h QtFactory.h
SOURCES += main.cpp QtPuzzleModel.cpp QtGameApplication.cpp QtDirector.cpp \
    QtMainWindow.cpp QtFactory.cpp

In general, a good way to do something like this is with the SUBDIRS qmake template.通常,使用 SUBDIRS qmake 模板来做这样的事情的一个好方法。 You would make a qmake file for each of the items you want to build (shared library, google test, and the executable), then make a SUBDIRS template to do those in order.您将为要构建的每个项目(共享库、google 测试和可执行文件)制作一个 qmake 文件,然后制作一个 SUBDIRS 模板来按顺序执行这些操作。 I think the subdirs template will provide the debug/release flags to each underlying make file.我认为 subdirs 模板将为每个底层 make 文件提供调试/发布标志。

For the shared library, the qmake library template should be fine.对于共享库, qmake库模板应该没问题。

I don't know about google test, I assume you could generate a qmake file for it if desired, or you could continue with the makefile.我不了解谷歌测试,我假设您可以根据需要为其生成一个 qmake 文件,或者您可以继续使用 makefile。

For linking the two, you could make a qmake file that has a main.cpp, specifies the others as libraries, and builds an executable .为了链接这两者,您可以制作一个包含 main.cpp 的 qmake 文件,将其他文件指定为库,然后 构建一个可执行文件

You can use DESTDIR , MOC_DIR , OBJECTS_DIR , and UI_DIR to change where generated files go.您可以使用DESTDIRMOC_DIROBJECTS_DIRUI_DIR更改生成文件 go 的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM