mainwindow.o 和 ***.o 文件中的 QT 重复符号

QT duplicate symbol in mainwindow.o and ***.o file

我有点困惑,我正在尝试开发一个简单的应用程序。

申请流程: 主窗口 -> 对话框 1 -> 对话框 2。

MainWindow 导入 Dialog1.h 和 Dialog1.h 导入 Dialog2.h

1) 如果我在 Dialog1.cpp 和 Dialog2.cpp 中导入 TinyXML 库, 编译器抛出以下错误: 重复符号 **RANDOM TINYXML CLASS* 在: Dialog1.o Dialog2.o

2) 由于 Dialog1.h 导入 Dialog2.h,即使我尝试仅在 Dialog2.h 中导入 TinyXML 库,我仍然会收到以下错误: 重复符号 **RANDOM TINYXML CLASS* 在: mainwindow.o moc_Dialog2.o

我没有在主窗口中导入任何东西。错误仍然存​​在。

对我出错的地方有帮助吗?

错误日志(有732条,随便复制粘贴一堆):

duplicate symbol __ZN11TiXmlString7reserveEm in:
    mainwindow.o
    selectorteach.o
duplicate symbol __ZN11TiXmlString8nullrep_E in:
    mainwindow.o
    selectorteach.o
duplicate symbol __ZN11TiXmlString6assignEPKcm in:
    mainwindow.o
    selectorteach.o
duplicate symbol __ZN11TiXmlString6appendEPKcm in:
    mainwindow.o
    selectorteach.o
duplicate symbol __ZplRK11TiXmlStringS1_ in:
    mainwindow.o
    selectorteach.o
duplicate symbol __ZplRK11TiXmlStringPKc in:
    mainwindow.o
    selectorteach.o
duplicate symbol __ZplPKcRK11TiXmlString in:
    mainwindow.o
    selectorteach.o
duplicate symbol __Z10TiXmlFOpenPKcS0_ in:
    mainwindow.o
    selectorteach.o
duplicate symbol __ZN9TiXmlBase12EncodeStringERK11TiXmlStringPS0_ in:
    mainwindow.o
    selectorteach.o
duplicate symbol __ZN9TiXmlBase6entityE in:
    mainwindow.o
    selectorteach.o
duplicate symbol __ZN9TiXmlNodeC2ENS_8NodeTypeE in:
    mainwindow.o
    selectorteach.o
duplicate symbol __ZTV9TiXmlNode in:
    mainwindow.o
    selectorteach.o
duplicate symbol __ZN9TiXmlNodeD2Ev in:
    mainwindow.o
    selectorteach.o
duplicate symbol __ZN9TiXmlNodeD1Ev in:
    mainwindow.o
    selectorteach.o
duplicate symbol __ZN9TiXmlNodeD0Ev in:
    mainwindow.o
    selectorteach.o
duplicate symbol __ZNK9TiXmlNode6CopyToEPS_ in:
    mainwindow.o
    selectorteach.o
duplicate symbol __ZN9TiXmlNode5ClearEv in:
    mainwindow.o
    selectorteach.o

--------------------------------------------------------------------

duplicate symbol __ZNK12TiXmlElement22QueryUnsignedAttributeEPKcPj in:
    mainwindow.o
    teachmode.o
duplicate symbol __ZNK12TiXmlElement18QueryBoolAttributeEPKcPb in:
    mainwindow.o
    teachmode.o
duplicate symbol __ZN9TiXmlBase11StringEqualEPKcS1_b13TiXmlEncoding in:
    mainwindow.o
    teachmode.o
duplicate symbol __ZNK12TiXmlElement20QueryDoubleAttributeEPKcPd in:
    mainwindow.o
    teachmode.o
duplicate symbol __ZN12TiXmlElement12SetAttributeEPKci in:
    mainwindow.o
    teachmode.o
duplicate symbol __ZN17TiXmlAttributeSet12FindOrCreateEPKc in:
    mainwindow.o
    teachmode.o
duplicate symbol __ZN14TiXmlAttribute11SetIntValueEi in:
    mainwindow.o
    teachmode.o
duplicate symbol __ZN17TiXmlAttributeSet3AddEP14TiXmlAttribute in:
    mainwindow.o
    teachmode.o
duplicate symbol __ZN12TiXmlElement18SetDoubleAttributeEPKcd in:
    mainwindow.o
    teachmode.o
duplicate symbol __ZN14TiXmlAttribute14SetDoubleValueEd in:
    mainwindow.o
    teachmode.o
duplicate symbol __ZNK12TiXmlElement5PrintEP7__sFILEi in:
    mainwindow.o
    teachmode.o
duplicate symbol __ZNK12TiXmlElement6AcceptEP12TiXmlVisitor in:
    mainwindow.o
    teachmode.o
duplicate symbol __ZNK12TiXmlElement5CloneEv in:
    mainwindow.o
    teachmode.o
duplicate symbol __ZNK12TiXmlElement7GetTextEv in:
    mainwindow.o
    teachmode.o
duplicate symbol __ZN13TiXmlDocumentC2Ev in:

--------------------------------------------------------------------

duplicate symbol __ZN9TiXmlBase6entityE in:
    mainwindow.o
    moc_selectorteach.o
duplicate symbol __ZN9TiXmlNodeC2ENS_8NodeTypeE in:
    mainwindow.o
    moc_selectorteach.o
duplicate symbol __ZTV9TiXmlNode in:
    mainwindow.o
    moc_selectorteach.o
duplicate symbol __ZN9TiXmlNodeD2Ev in:
    mainwindow.o
    moc_selectorteach.o
duplicate symbol __ZN9TiXmlNodeD1Ev in:
    mainwindow.o
    moc_selectorteach.o
duplicate symbol __ZN9TiXmlNodeD0Ev in:
    mainwindow.o
    moc_selectorteach.o
duplicate symbol __ZNK9TiXmlNode6CopyToEPS_ in:
    mainwindow.o
    moc_selectorteach.o
duplicate symbol __ZN9TiXmlNode5ClearEv in:
    mainwindow.o
    moc_selectorteach.o
duplicate symbol __ZN9TiXmlNode12LinkEndChildEPS_ in:
    mainwindow.o
    moc_selectorteach.o
duplicate symbol __ZN13TiXmlDocument8SetErrorEiPKcP16TiXmlParsingData13TiXmlEncoding in:
    mainwindow.o
    moc_selectorteach.o
duplicate symbol __ZNK9TiXmlNode11GetDocumentEv in:
    mainwindow.o
    moc_selectorteach.o
duplicate symbol __ZN9TiXmlBase11errorStringE in:
    mainwindow.o
    moc_selectorteach.o
duplicate symbol __ZN16TiXmlParsingData5StampEPKc13TiXmlEncoding in:
    mainwindow.o
    moc_selectorteach.o
duplicate symbol __ZN9TiXmlNode14InsertEndChildERKS_ in:
    mainwindow.o
    moc_selectorteach.o
duplicate symbol __ZN9TiXmlNode17InsertBeforeChildEPS_RKS_ in:
    mainwindow.o
    moc_selectorteach.o
duplicate symbol __ZN9TiXmlNode16InsertAfterChildEPS_RKS_ in:
    mainwindow.o
    moc_selectorteach.o
duplicate symbol __ZN9TiXmlNode12ReplaceChildEPS_RKS_ in:

--------------------------------------------------------------------

    moc_teachmode.o
duplicate symbol __ZN9TiXmlText5ParseEPKcP16TiXmlParsingData13TiXmlEncoding in:
    mainwindow.o
    moc_teachmode.o
duplicate symbol __ZN16TiXmlDeclaration5ParseEPKcP16TiXmlParsingData13TiXmlEncoding in:
    mainwindow.o
    moc_teachmode.o
duplicate symbol __ZTV12TiXmlPrinter in:
    mainwindow.o
    moc_teachmode.o
duplicate symbol __ZTV14TiXmlAttribute in:
    mainwindow.o
    moc_teachmode.o
duplicate symbol __ZTV12TiXmlUnknown in:
    mainwindow.o
    moc_teachmode.o
duplicate symbol __ZTV9TiXmlText in:
    mainwindow.o
    moc_teachmode.o
duplicate symbol __ZN11TiXmlString4nposE in:
    mainwindow.o
    moc_teachmode.o
duplicate symbol __ZTI9TiXmlNode in:
    mainwindow.o
    moc_teachmode.o
duplicate symbol __ZTI12TiXmlElement in:
    mainwindow.o
    moc_teachmode.o
duplicate symbol __ZTI13TiXmlDocument in:
    mainwindow.o
    moc_teachmode.o
duplicate symbol __ZTI12TiXmlComment in:
    mainwindow.o
    moc_teachmode.o
duplicate symbol __ZTI16TiXmlDeclaration in:
    mainwindow.o
    moc_teachmode.o
duplicate symbol __ZTI14TiXmlAttribute in:
    mainwindow.o
    moc_teachmode.o
duplicate symbol __ZTS14TiXmlAttribute in:
    mainwindow.o
    moc_teachmode.o
duplicate symbol __ZTS9TiXmlNode in:
    mainwindow.o
    moc_teachmode.o
duplicate symbol __ZTS12TiXmlComment in:
    mainwindow.o
    moc_teachmode.o
duplicate symbol __ZTI9TiXmlText in:
    mainwindow.o
    moc_teachmode.o
duplicate symbol __ZTS9TiXmlText in:
    mainwindow.o
    moc_teachmode.o
duplicate symbol __ZTS16TiXmlDeclaration in:
    mainwindow.o
    moc_teachmode.o
duplicate symbol __ZTI12TiXmlUnknown in:
    mainwindow.o
    moc_teachmode.o
duplicate symbol __ZTS12TiXmlUnknown in:
    mainwindow.o
    moc_teachmode.o
duplicate symbol __ZTS13TiXmlDocument in:
    mainwindow.o
    moc_teachmode.o
duplicate symbol __ZTI12TiXmlPrinter in:
    mainwindow.o
    moc_teachmode.o
duplicate symbol __ZTS12TiXmlPrinter in:
    mainwindow.o
    moc_teachmode.o
duplicate symbol __ZTS12TiXmlElement in:
    mainwindow.o
    moc_teachmode.o
ld: 732 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [untitled.app/Contents/MacOS/untitled] Error 1
19:03:15: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project untitled (kit: Desktop Qt 5.4.0 clang 64bit)
When executing step "Make"

注意:当我只包含 Dialog2.cpp 中的文件时,项目编译并且所有功能都按预期工作。只有当我将它放在头文件中时才会出现问题。 我的 TinyXML 包含指令:-

   #include <tinystr.h>
   #include <tinyxml.h>
   #include <tinystr.cpp>
   #include <tinyxmlerror.cpp
   #include <tinyxml.cpp>
   #include <tinyxmlparser.cpp>

如果我只包含源文件如:

#include <tinystr.h>
#include <tinyxml.h>

我收到以下错误日志:

Undefined symbols for architecture x86_64:
  "TiXmlString::assign(char const*, unsigned long)", referenced from:
      TiXmlString::operator=(char const*) in teachmode.o
  "TiXmlString::nullrep_", referenced from:
      TiXmlString::quit() in teachmode.o
  "TiXmlElement::SetAttribute(char const*, char const*)", referenced from:
      saveData(product) in teachmode.o
  "TiXmlElement::SetAttribute(char const*, int)", referenced from:
      saveData(product) in teachmode.o
  "TiXmlElement::TiXmlElement(char const*)", referenced from:
      saveData(product) in teachmode.o
  "TiXmlDocument::LoadFile(char const*, TiXmlEncoding)", referenced from:
      saveData(product) in teachmode.o
  "TiXmlDocument::TiXmlDocument()", referenced from:
      saveData(product) in teachmode.o
  "TiXmlDeclaration::TiXmlDeclaration(char const*, char const*, char const*)", referenced from:
      saveData(product) in teachmode.o
  "TiXmlNode::LinkEndChild(TiXmlNode*)", referenced from:
      saveData(product) in teachmode.o
  "TiXmlNode::TiXmlNode(TiXmlNode::NodeType)", referenced from:
      TiXmlText::TiXmlText(char const*) in teachmode.o
  "TiXmlNode::~TiXmlNode()", referenced from:
      TiXmlDocument::~TiXmlDocument() in teachmode.o
      TiXmlText::TiXmlText(char const*) in teachmode.o
  "TiXmlDocument::SaveFile(char const*) const", referenced from:
      saveData(product) in teachmode.o
  "TiXmlNode::FirstChildElement() const", referenced from:
      TiXmlNode::FirstChildElement() in teachmode.o
  "vtable for TiXmlDocument", referenced from:
      TiXmlDocument::~TiXmlDocument() in teachmode.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "vtable for TiXmlText", referenced from:
      TiXmlText::TiXmlText(char const*) in teachmode.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [untitled.app/Contents/MacOS/untitled] Error 1
19:25:51: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project untitled (kit: Desktop Qt 5.4.0 clang 64bit)
When executing step "Make"

不要包含 源文件,这就是导致问题的原因!

仅包含头文件,然后像编译您自己的源文件一样编译和link源文件。


源文件包含所有函数的定义,您的程序中只能有一个定义。