西西軟件下載最安全的下載網(wǎng)站、值得信賴的軟件下載站!

首頁(yè)編程開(kāi)發(fā)其它知識(shí) → 引用編譯libxml遇到multiple definition 報(bào)錯(cuò)問(wèn)題的解決方案

引用編譯libxml遇到multiple definition 報(bào)錯(cuò)問(wèn)題的解決方案

相關(guān)軟件相關(guān)文章發(fā)表評(píng)論 來(lái)源:西西整理時(shí)間:2013/4/3 9:56:18字體大。A-A+

作者:西西點(diǎn)擊:0次評(píng)論:1次標(biāo)簽: 報(bào)錯(cuò)

  • 類型:辦公軟件大。257KB語(yǔ)言:中文 評(píng)分:7.5
  • 標(biāo)簽:
立即下載

問(wèn)題原因:
當(dāng)多個(gè)文件包含同一個(gè)頭文件時(shí),并且你的.H里面沒(méi)有加上條件編譯
#ifndef test_H
#define TEST_H
#endif
就會(huì)獨(dú)立的解釋,然后生成每個(gè)文件生成獨(dú)立的標(biāo)示符。在編譯器連接時(shí),就會(huì)將工程中所有的符號(hào)整合在一起,由于,文件中有重名變量,于是就出現(xiàn)了重復(fù)定義的錯(cuò)誤。

編譯報(bào)錯(cuò):

multiple definition of `xmlMemStrdup' GStreamPlayerline 75, external location: c:\mingw\lib\gcc\mingw32\4.6.2\include\c++\iostreamC/C++ Problem
multiple definition of `xmlMalloc' GStreamPlayerline 75, external location: c:\mingw\lib\gcc\mingw32\4.6.2\include\c++\iostreamC/C++ Problem
multiple definition of `forbiddenExp' CGStreamBuilder.cpp/GStreamPlayer/Src line 246C/C++ Problem
multiple definition of `xmlMallocAtomic' GStreamPlayerline 75, external location: c:\mingw\lib\gcc\mingw32\4.6.2\include\c++\iostreamC/C++ Problem
multiple definition of `emptyExp' CGStreamBuilder.cpp/GStreamPlayer/Src line 246C/C++ Problem
multiple definition of `xmlRealloc' GStreamPlayerline 75, external location: c:\mingw\lib\gcc\mingw32\4.6.2\include\c++\iostreamC/C++ Problem
multiple definition of `xmlFree' GStreamPlayerline 75, external location: c:\mingw\lib\gcc\mingw32\4.6.2\include\c++\iostreamC/C++ Problem

解決方法:

定義宏:LIBXML_STATIC

在頭文件xmlexports.h 中有如下問(wèn)題:

#if defined(_WIN32) && defined(__MINGW32__)
  #undef XMLPUBFUN
  #undef XMLPUBVAR
  #undef XMLCALL
  #undef XMLCDECL
  /*
   * if defined(IN_LIBXML) this raises problems on mingw with msys
   * _imp__xmlFree listed as missing. Try to workaround the problem
   * by also making that declaration when compiling client code.
   */
  #if !defined(LIBXML_STATIC)
    #define XMLPUBFUN __declspec(dllexport)
    #define XMLPUBVAR __declspec(dllexport)
  #else
    #define XMLPUBFUN
    #if !defined(LIBXML_STATIC)
      #define XMLPUBVAR __declspec(dllimport) extern
    #else
      #define XMLPUBVAR extern
    #endif
  #endif
  #define XMLCALL __cdecl
  #define XMLCDECL __cdecl
  #if !defined _REENTRANT
    #define _REENTRANT
  #endif
#endif

其他方法:

方法1:
給每一個(gè)頭文件加上條件編譯,避免該文件被多次引用時(shí)被多次解釋,這是個(gè)應(yīng)該是習(xí)慣。這個(gè)方法會(huì)解決大部分低級(jí)問(wèn)題。

方法2:
當(dāng)方法1無(wú)效時(shí),可以把所有的全局變量放入一個(gè)頭文件 global.h (名字隨意起,但要加條件編譯)中,每一個(gè)變量前面加extern,聲明一下這些變量將在其它文件中定義。 然后建立一個(gè)和頭文件名字對(duì)應(yīng)的.c or .cpp文件 如global.c。在里面聲明所有的全局變量。例如:void(*Handl_Display)();
然后,讓涉及到全局變量的文件include ”global.h“。這樣編譯時(shí),會(huì)先對(duì)global.c編譯生成一個(gè)global.o ,然后再和其它文件的.o鏈接生成可執(zhí)行文件。

方法3:
懶人方法,在所有的全局變量前加上static ,聲明成靜止變量。也能解決問(wèn)題。

    相關(guān)評(píng)論

    閱讀本文后您有什么感想? 已有人給出評(píng)價(jià)!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過(guò)難過(guò)
    • 5 囧
    • 3 圍觀圍觀
    • 2 無(wú)聊無(wú)聊

    熱門(mén)評(píng)論

    最新評(píng)論

    發(fā)表評(píng)論 查看所有評(píng)論(1)

    昵稱:
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字?jǐn)?shù): 0/500 (您的評(píng)論需要經(jīng)過(guò)審核才能顯示)
    推薦文章

    沒(méi)有數(shù)據(jù)