首页 > 解决方案 > QT Subdir - 未定义的参考

问题描述

我正在开发一个带有子目录的 QT 项目,它在 linux (ubuntu) 上运行良好,但是当我尝试在 Windows 上运行时,我得到了对 `moduleInterface::...'的未定义引用,其中...指的是所有方法在我的子目录上。我使用的Qt版本(5.11.2)在linux和windows中都是一样的。子目录配置如下:

主程序

TEMPLATE = subdirs

SUBDIRS += \
         tests \
         coreSerialTerm \
         serialTermView

serialTermView.depends = coreSerialTerm
tests.depends = coreSerialTerm

coreSerialTerm.pro

QT       -= gui
QT       += serialport
QT       += core
CONFIG   += c++14
TARGET = coreSerialTerm
TEMPLATE = lib

DEFINES += CORESERIALTERM_LIBRARY

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has 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

SOURCES += \
         coreserialterm.cpp \
         serialInterface/serialinterface.cpp \
         moduleInterface/moduleinterface.cpp \
         serialProtocol/serialProtocol.c \
         moduleInterface/progressbar.cpp \
         agroflashinterface.cpp \
         Storage/event_list.c \
         Storage/storage.c \
         flashfilehandler.cpp

HEADERS += \
    coreserialterm.h \
    coreserialterm_global.h \ 
   serialInterface/serialinterface.h \
moduleInterface/moduleinterface.h \
serialProtocol/serialProtocol.h \
devRetCode.h \
moduleInterface/progressbar.h \
Sensors/sensor.h \
Storage/storage.h \
Utils/configManager.h \
Utils/configStates.h \
Utils/delayPlus.h \
Utils/io.h \
Utils/rtcManager.h \
Utils/wdg.h \
version.h \
module.h \
flashfilehandler.h

serialTermView.pro

QT       += core gui
QT       += serialport

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = serialTermView
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has 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

CONFIG += c++11

SOURCES += \
       main.cpp \
       mainwindow.cpp

HEADERS += \
       mainwindow.h

FORMS += \
       mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../coreSerialTerm/release/ -lcoreSerialTerm
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../coreSerialTerm/debug/ -lcoreSerialTerm
else:unix: LIBS += -L$$OUT_PWD/../coreSerialTerm/ -lcoreSerialTerm

INCLUDEPATH += $$PWD/../coreSerialTerm
DEPENDPATH += $$PWD/../coreSerialTerm

当我编译时,我得到以下错误:

debug/mainwindow.o: In function `ZN10MainWindowC2EP7QWidget':
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:13: undefined reference to `flashFileHandler::flashFileHandler()'
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:18: undefined reference to `moduleInterface::Instance()'
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:18: undefined reference to `moduleInterface::availablePorts()'
debug/mainwindow.o: In function `operator()':
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:58: undefined reference to `moduleInterface::Instance()'
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:58: undefined reference to `moduleInterface::connect(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:65: undefined reference to `moduleInterface::Instance()'
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:65: undefined reference to `moduleInterface::close()'
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:106: undefined reference to `moduleInterface::Instance()'
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:106: undefined reference to `moduleInterface::getTime(unsigned int*)'
debug/mainwindow.o: In function `ZN10MainWindow7getTimeEv':
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:98: undefined reference to `moduleInterface::Instance()'
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:98: undefined reference to `moduleInterface::isConnected()'
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:138: undefined reference to `moduleInterface::Instance()'
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:138: undefined reference to `moduleInterface::close()'
debug/mainwindow.o: In function `operator()':
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:163: undefined reference to `moduleInterface::Instance()'
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:163: undefined reference to `moduleInterface::setTime()'
debug/mainwindow.o: In function `ZN10MainWindow7setTimeEv':
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:148: undefined reference to `moduleInterface::Instance()'
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:148: undefined reference to `moduleInterface::isConnected()'
debug/mainwindow.o: In function `operator()':
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:229: undefined reference to `moduleInterface::Instance()'
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:229: undefined reference to `moduleInterface::memoryDump(unsigned char*, unsigned int)'
debug/mainwindow.o: In function `ZN10MainWindow7memDumpEv':
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:210: undefined reference to `moduleInterface::Instance()'
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:210: undefined reference to `moduleInterface::isConnected()'
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:226: undefined reference to `moduleInterface::pageChanged(int)'
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:223: undefined reference to `moduleInterface::Instance()'
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:261: undefined reference to `moduleInterface::Instance()'
debug/mainwindow.o: In function `ZN10MainWindow6saveAsEv':
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:279: undefined reference to `flashFileHandler::open(QString)'
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:281: undefined reference to `flashFileHandler::save(QString)'
debug/mainwindow.o: In function `ZN10MainWindow4saveEv':
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:288: undefined reference to `flashFileHandler::isReady()'
debug/mainwindow.o: In function `ZN10MainWindow9closePortEv':
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:297: undefined reference to `moduleInterface::Instance()'
C:\Users\Augusto\Documents\Workspace\serialTerm\build-serialTerm-Desktop_Qt_5_11_2_MinGW_32bit-Debug\serialTermView/../../serialTerm/serialTermView/mainwindow.cpp:297: undefined reference to `moduleInterface::close()'
debug/mainwindow.o: In function `connect<void (moduleInterface::*)(int), MainWindow::memDump()::<lambda(int)> >':
C:/Qt/5.11.2/mingw53_32/include/QtCore/qobject.h:333: undefined reference to `moduleInterface::staticMetaObject'
collect2.exe: error: ld returned 1 exit status

标签: c++windowsqtqt5mingw32

解决方案


它看起来像一个编译顺序问题。可能链接器找不到某些符号,因为它们属于其他子项目之一,但尚未处理它们。

尝试更改您的 subdirs .pro 文件,如下所示:

TEMPLATE = subdirs

CONFIG += ordered

SUBDIRS += \             
         coreSerialTerm \
         tests \
         serialTermView

.depends 属性在 Windows 上从来没有真正为我工作过。


推荐阅读