错误LNK2019:未解析的外部符号,排除了所有典型原因

时间:2013-12-17 00:45:39

标签: c++ visual-studio-2012 linker linker-errors project-properties

在我的案例中,针对本网站上的这个问题的每个答案(来自我已经看到的,很多),我仍然处于僵局。我正在使用遗留代码,不得不通过设置正确连接的开发环境来破解我的方式。使用VS 2012,我有一个包含22个项目的解决方案,它们之间有一个依赖关系的spiderweb。 1个项目,phtranscript,取决于来自另一个项目hunspell的代码,并且第三个项目speechRecognizer需要phtranscript的代码。以下是关联文件和编译器/链接器输出:

在项目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)" (?freelist@@YAXPAPAPADH@Z) 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> > > > &)" (?Morph@impl@hunspellMorph@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AAV?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@4@@Z)
19>hunspellMorph.obj : error LNK2019: unresolved external symbol "public: __thiscall Hunspell::Hunspell(char const *,char const *,char const *)" (??0Hunspell@@QAE@PBD00@Z) referenced in function "public: __thiscall hunspellMorph::impl::impl(void)" (??0impl@hunspellMorph@@QAE@XZ)
19>hunspellMorph.obj : error LNK2019: unresolved external symbol "public: __thiscall Hunspell::~Hunspell(void)" (??1Hunspell@@QAE@XZ) referenced in function "public: __thiscall hunspellMorph::impl::~impl(void)" (??1impl@hunspellMorph@@QAE@XZ)
19>hunspellMorph.obj : error LNK2019: unresolved external symbol "public: int __thiscall Hunspell::analyze(char * * *,char const *,int)" (?analyze@Hunspell@@QAEHPAPAPADPBDH@Z) 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> > > > &)" (?Morph@impl@hunspellMorph@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AAV?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@4@@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包含目录。我离开Linker-&gt;单独输入附加库字段,因为我已经声明符号&#34;链接器错误,当我同时指定它和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)" (?freelist@@YAXPAPAPADH@Z) 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> > > > &)" (?Morph@impl@hunspellMorph@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AAV?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@4@@Z)
21>speechRecognizer.lib(hunspellMorph.obj) : error LNK2019: unresolved external symbol "public: __thiscall Hunspell::Hunspell(char const *,char const *,char const *)" (??0Hunspell@@QAE@PBD00@Z) referenced in function "public: __thiscall hunspellMorph::impl::impl(void)" (??0impl@hunspellMorph@@QAE@XZ)
21>speechRecognizer.lib(hunspellMorph.obj) : error LNK2019: unresolved external symbol "public: __thiscall Hunspell::~Hunspell(void)" (??1Hunspell@@QAE@XZ) referenced in function "public: __thiscall hunspellMorph::impl::~impl(void)" (??1impl@hunspellMorph@@QAE@XZ)
21>speechRecognizer.lib(hunspellMorph.obj) : error LNK2019: unresolved external symbol "public: int __thiscall Hunspell::analyze(char * * *,char const *,int)" (?analyze@Hunspell@@QAEHPAPAPADPBDH@Z) 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> > > > &)" (?Morph@impl@hunspellMorph@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AAV?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@4@@Z)
21>C:\temp\speech\divided_rm_speech_proj\Debug\interface11.exe : fatal error LNK1120: 8 unresolved externals

这些链接错误要等到最后编译应用程序才会弹出,而不是将speechrecognizer编译为应用程序。还有其他尚未解决的联系,但它们似乎与此问题无关(尽管是相关的)。

1 个答案:

答案 0 :(得分:0)

您的输出告诉我,两个项目都编译为* .exe文件。在这种情况下,我不知道你怎么期望'phtranscript'使用模块,这是'hunspell'的一部分。

如果你创建了这样的依赖项,hunspell应该是静态(或动态)库,phtranscript链接到它。我相信你可以正确设置所有依赖项,但VS没有任何东西可以链接在一起,这就是链接器对你如此生气的原因 - 它不能使用* .exe作为依赖项。

简单解决方案:将'hunspell'类型更改为'静态库(.lib)'或'动态库(.dll)'并设置'phtranscript'以将其用作输入库。