0

在本網站上,對於此問題的每個答案(從我所看到的,這是很多)已在我的案例中得到解決,而且我仍然處於僵局。我正在使用遺留代碼,不得不通過設置正確連接的開發環境來破解我的方式。使用VS 2012,我有一個包含22個項目的解決方案,它們之間有一個spiderweb依賴關係。 1個項目,phtranscript,依賴於來自另一個項目hunspell的代碼,而phtranscript的代碼反過來需要第三個項目speechRecognizer。以下是相關文件和編譯器/連接器輸出:錯誤LNK2019:無法解析的外部符號,排除所有典型原因

在項目phtranscript:

phTranscript.h:

#ifndef _phTranscript_h__ 
#define _phTranscript_h__ 

#include <vector> 
#include <string> 
#include <map> 
#include "config.h" 
#include "character.h" 
... 
class hunspellMorph{ 
public: 
    static hunspellMorph *instance(); 
protected: 
    hunspellMorph(); 

private: 
    hunspellMorph(const hunspellMorph &); 
    hunspellMorph& operator=(const hunspellMorph &); 

public: 
    ~hunspellMorph(); 

    void Morph(const std::string &in,std::vector<std::string> &out); 

private: 
    class impl; 
    impl *pImpl_; 
}; 


#endif 

hunspellMorph.cpp:

#include "phTranscript.h" 
#include "hunspell.hxx" 
#include <treeNode.h> 

#include <string.h> 

class hunspellMorph::impl{ 
private: 
    Hunspell hs; 

public: 
    impl(); 

    void Morph(const std::string &in,std::vector<std::string> &out); 

}; 

void hunspellMorph::impl::Morph(const std::string &in,std::vector<std::string> &out){ 
    char **slst; 
    int re = hs.analyze(&slst,in.c_str()); 
    ... 
    freelist(&slst,re); 
} 

hunspellMorph::hunspellMorph(){ 
    pImpl_ = new impl(); 
} 

hunspellMorph::~hunspellMorph(){ 
    delete pImpl_; 
} 

.... 

在項目中的hunspell:

hunspell.hxx:

#include "affixmgr.hxx" 
#include "suggestmgr.hxx" 
#include "csutil.hxx" 
#include "langnum.hxx" 

#define SPELL_COMPOUND (1 << 0) 
#define SPELL_FORBIDDEN (1 << 1) 
#define SPELL_ALLCAP (1 << 2) 
#define SPELL_NOCAP  (1 << 3) 
#define SPELL_INITCAP (1 << 4) 

#define MAXDIC 20 
#define MAXSUGGESTION 15 
#define MAXSHARPS 5 

#ifndef _HUNSPELL_HXX_ 
#define _HUNSPELL_HXX_ 

class Hunspell 
{ 
    ... 
public: 
    Hunspell(const char * affpath, const char * dpath, const char * key = NULL); 
    ~Hunspell(); 

    int analyze(char ***slst,const char *word,int d=0); 
    ... 
}; 
#endif 

csutil.hxx:

#ifndef __CSUTILHXX__ 
#define __CSUTILHXX__ 

// First some base level utility routines 

#define NOCAP 0 
#define INITCAP 1 
#define ALLCAP 2 
#define HUHCAP 3 
#define HUHINITCAP 4 

#define MORPH_STEM  "st:" 
#define MORPH_ALLOMORPH "al:" 
#define MORPH_POS   "po:" 
#define MORPH_DERI_PFX "dp:" 
#define MORPH_INFL_PFX "ip:" 
#define MORPH_TERM_PFX "tp:" 
#define MORPH_DERI_SFX "ds:" 
#define MORPH_INFL_SFX "is:" 
#define MORPH_TERM_SFX "ts:" 
#define MORPH_SURF_PFX "sp:" 
#define MORPH_FREQ  "fr:" 
#define MORPH_PHON  "ph:" 
#define MORPH_HYPH  "hy:" 
#define MORPH_PART  "pa:" 
#define MORPH_HENTRY  "_H:" 
#define MORPH_TAG_LEN  strlen(MORPH_STEM) 

#define MSEP_FLD ' ' 
#define MSEP_REC '\n' 
#define MSEP_ALT '\v' 

// default flags 
#define DEFAULTFLAGS 65510 
#define FORBIDDENWORD 65510 
#define ONLYUPCASEFLAG 65511 

typedef struct { 
    unsigned char l; 
    unsigned char h; 
} w_char; 

#define w_char_eq(a,b) (((a).l == (b).l) && ((a).h == (b).h)) 

... 
// free character array list 
void freelist(char *** list, int n); 
#endif 

hunspell.cxx:

#include "license.hunspell" 
#include "license.myspell" 

#ifndef MOZILLA_CLIENT 
#include <cstdlib> 
#include <cstring> 
#include <cstdio> 
#else 
#include <stdlib.h> 
#include <string.h> 
#include <stdio.h> 
#endif 

#include "hunspell.hxx" 
#include "./config.h" 
#include "./treeNode.h" 
#include "cache.h" 

#include <string> 
#include <vector> 

#ifndef MOZILLA_CLIENT 
#ifndef W32 
using namespace std; 
#endif 
#endif 

Hunspell::Hunspell(const char * affpath, const char * dpath, const char * key) 
{ 
    ... 
} 

Hunspell::~Hunspell() 
{ 
    ... 
} 

int Hunspell::analyze(char ***slst,const char *word,int d){ 
    ... 
} 

csutil.cxx:

#include "license.hunspell" 
#include "license.myspell" 

#ifndef MOZILLA_CLIENT 
#include <cstdlib> 
#include <cstring> 
#include <cstdio> 
#include <cctype> 
#else 
#include <stdlib.h> 
#include <string.h> 
#include <stdio.h> 
#include <ctype.h> 
#endif 

#include "csutil.hxx" 
#include "atypes.hxx" 
#include "langnum.hxx" 

#ifdef OPENOFFICEORG 
# include <unicode/uchar.h> 
#else 
# ifndef MOZILLA_CLIENT 
# include "utf_info.cxx" 
# define UTF_LST_LEN (sizeof(utf_lst)/(sizeof(unicode_info))) 
# endif 
#endif 

#ifdef MOZILLA_CLIENT 
#include "nsCOMPtr.h" 
#include "nsServiceManagerUtils.h" 
#include "nsIUnicodeEncoder.h" 
#include "nsIUnicodeDecoder.h" 
#include "nsICaseConversion.h" 
#include "nsICharsetConverterManager.h" 
#include "nsUnicharUtilCIID.h" 
#include "nsUnicharUtils.h" 

static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID); 
static NS_DEFINE_CID(kUnicharUtilCID, NS_UNICHARUTIL_CID); 
#endif 

#ifdef MOZILLA_CLIENT 
#ifdef __SUNPRO_CC // for SunONE Studio compiler 
using namespace std; 
#endif 
#else 
#ifndef W32 
using namespace std; 
#endif 
#endif 

... 

void freelist(char *** list, int n) { 
    if (list && (n > 0)) { 
     for (int i = 0; i < n; i++) if ((*list)[i]) free((*list)[i]); 
     free(*list); 
     *list = NULL; 
    } 
} 

而這裏的不同之間的清理生成輸出項目:

6>------ Build started: Project: hunspell, Configuration: Debug Win32 ------ 
... 
6> hunspell.vcxproj -> C:\temp\speech\divided_rm_speech_proj\Debug\hunspell.exe 
... 
10>------ Build started: Project: phtranscript, Configuration: Debug Win32 ------ 
... 
10> phtranscript.vcxproj -> C:\temp\speech\divided_rm_speech_proj\Debug\phtranscript.exe 
... 
19>------ Build started: Project: speechRecognizer, Configuration: Debug Win32 ------ 
... 
19> Generating Code... 
19>hunspellMorph.obj : error LNK2019: unresolved external symbol "void __cdecl freelist(char * * *,int)" ([email protected]@[email protected]) referenced in function "public: void __thiscall hunspellMorph::impl::Morph(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > &)" ([email protected]@[email protected]@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@[email protected]@[email protected]@Z) 
19>hunspellMorph.obj : error LNK2019: unresolved external symbol "public: __thiscall Hunspell::Hunspell(char const *,char const *,char const *)" ([email protected]@[email protected]@Z) referenced in function "public: __thiscall hunspellMorph::impl::impl(void)" ([email protected]@@[email protected]) 
19>hunspellMorph.obj : error LNK2019: unresolved external symbol "public: __thiscall Hunspell::~Hunspell(void)" ([email protected]@[email protected]) referenced in function "public: __thiscall hunspellMorph::impl::~impl(void)" ([email protected]@@[email protected]) 
19>hunspellMorph.obj : error LNK2019: unresolved external symbol "public: int __thiscall Hunspell::analyze(char * * *,char const *,int)" ([email protected]@@[email protected]) referenced in function "public: void __thiscall hunspellMorph::impl::Morph(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > &)" ([email protected]@[email protected]@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@[email protected]@[email protected]@Z) 
19>C:\temp\speech\divided_rm_speech_proj\Debug\speechRecognizer.exe : fatal error LNK1120: 4 unresolved externals 

請記住,有很多代碼取出,所以如果缺少重要的東西讓我知道,我會更新這個描述。

在Visual Studio 2012的環境設置中,phtranscript的項目屬性具有在公共屬性下建立的hunspell引用,include目錄字段包含hunspell include目錄,庫目錄字段包含hunspell庫輸出文件夾(全部使用VC++目錄),C/C++附加包含目錄還列出了hunspell包含目錄。當我同時指定了它和VC++目錄時,我得到了鏈接器 - >輸入附加庫字段,因爲我得到了「已聲明符號」鏈接器錯誤。在Project Dependencies下,我檢查hunspell並確保它在構建順序中的phtranscript之前。最後,我已經手動將現有的hunspell庫(在它們編譯之後)添加到phtranscript項目中。在speechrecognizer項目中,我已經採取了相同的步驟,因爲它們對應於phtranscript項目依賴關係。

代碼幾乎是一場噩夢。解決這個問題需要做的最少量的修改是什麼?最好只需在IDE端更改/添加某些內容,而不是更改代碼(儘管這些內容不可避免)。

更新:

所有項目被指定爲項目屬性下的應用程序。其更改爲靜態庫(所有,但1個項目旨在執行)後,鏈接錯誤變成這樣:

21>speechRecognizer.lib(hunspellMorph.obj) : error LNK2019: unresolved external symbol "void __cdecl freelist(char * * *,int)" ([email protected]@[email protected]) referenced in function "public: void __thiscall hunspellMorph::impl::Morph(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > &)" ([email protected]@[email protected]@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@[email protected]@[email protected]@Z) 
21>speechRecognizer.lib(hunspellMorph.obj) : error LNK2019: unresolved external symbol "public: __thiscall Hunspell::Hunspell(char const *,char const *,char const *)" ([email protected]@[email protected]@Z) referenced in function "public: __thiscall hunspellMorph::impl::impl(void)" ([email protected]@@[email protected]) 
21>speechRecognizer.lib(hunspellMorph.obj) : error LNK2019: unresolved external symbol "public: __thiscall Hunspell::~Hunspell(void)" ([email protected]@[email protected]) referenced in function "public: __thiscall hunspellMorph::impl::~impl(void)" ([email protected]@@[email protected]) 
21>speechRecognizer.lib(hunspellMorph.obj) : error LNK2019: unresolved external symbol "public: int __thiscall Hunspell::analyze(char * * *,char const *,int)" ([email protected]@@[email protected]) referenced in function "public: void __thiscall hunspellMorph::impl::Morph(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > &)" ([email protected]@[email protected]@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@[email protected]@[email protected]@Z) 
21>C:\temp\speech\divided_rm_speech_proj\Debug\interface11.exe : fatal error LNK1120: 8 unresolved externals 

這些鏈接錯誤等到在最後編譯應用程序來涌現,而不是在編譯時speechrecognizer作爲應用程序。還有其他未解決的聯繫,但它們似乎是獨立的(儘管有關)這個問題。

回答

0

您的輸出告訴我,這兩個項目都被編譯爲* .exe文件。我不知道在這種情況下,'phtranscript'如何使用模塊,它是'hunspell'的一部分。

如果您創建了這樣的依賴關係,hunspell應該是靜態(或動態)庫,即phtranscript鏈接到的庫。我相信你會正確地設置所有的依賴關係,但VS沒有任何東西可以鏈接在一起,這就是爲什麼鏈接器對你很生氣 - 它不能使用* .exe作爲依賴關係。

簡單的解決方案:將'hunspell'類型更改爲'靜態庫(.lib)'或'動態庫(.dll)'並設置'phtranscript'以將其用作輸入庫。

+0

它沒有完全工作,只是推它。上面的第一個更新解決了您的建議。 – Erik

+0

檢查hunspell是否被正確添加爲speechRecognizer的輸入庫。據我所知,所有失蹤的外部都是在hunspell來源中定義的。 –

+0

在speechrecognizer的屬性頁面中,我將hunspell引用爲引用,並將包含其對象的目錄作爲VC++庫目錄的一部分。剛纔我把lib文件顯式地放到了庫管理器下的附加依賴項中,它可以工作。非常感謝你!但爲什麼其他人不工作?更奇怪的是,爲什麼我的其他項目中沒有任何一個項目存在這個問題,即使它們的配置方式完全相同(在圖書館管理員的幫助下沒有明確的庫規範)? – Erik

相關問題