Ошибка LNK2019: неразрешенный внешний символ, исключены все типичные причины

Каждый ответ (из того, что я видел, что много) на этот вопрос на этом сайте был рассмотрен в моем случае, и я все еще в тупике. Я работаю с унаследованным кодом, мне нужно взломать свой путь через настройку правильно подключенной среды разработки. Используя VS 2012, у меня есть решение с 22 проектами с паутиной зависимостей между ними. 1 проект phtranscript зависит от кода другого проекта hunspell, а код phtranscript, в свою очередь, необходим третьему проекту speechRecognizer. Вот связанные файлы и выходные данные компилятора / компоновщика:

В проекте 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, установленную в общих свойствах, поле включаемых каталогов включает в себя каталог включения hunspell, а поле каталогов библиотеки включает в себя выходную папку библиотеки hunspell (это все в каталогах VC++), C/C++ дополнительные каталоги включения также перечисляют каталог включения hunspell. Я оставил поле Linker->Input дополнительные библиотеки в одиночку, так как получаю ошибки компоновщика "уже объявленный символ", когда у меня есть и он, и указанная директория VC++. В разделе Project Dependencies я проверяю hunspell и проверяю, что он предшествует phtranscript в порядке сборки. Наконец, я вручную добавил существующие библиотеки hunspell (после их компиляции) в проект phtranscript. В проекте speechrecognizer я предпринял идентичные шаги, поскольку они соответствуют зависимостям проекта phtranscript.

Код в значительной степени кошмар Imo. Какое минимальное количество изменений необходимо исправить? Желательно просто путем изменения / добавления чего-либо на стороне 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

Эти ошибки ссылок ожидают, пока не возникнет компиляция приложения в самом конце, а не при компиляции речи-распознавателя как приложения. Существуют и другие неразрешенные связи, но они кажутся независимыми (хотя и связаны) с этой проблемой.

1 ответ

Ваш вывод говорит мне, что оба проекта скомпилированы в файлы *.exe. Я не знаю, как вы ожидаете, что phtranscript будет использовать модули, которые являются частью hunspell, в этом случае.

Если вы создаете такие зависимости, hunspell должен быть статической (или динамической) библиотекой, на которую ссылается phtranscript. Я верю, что вы правильно устанавливаете все зависимости, но VS не имеет ничего для связи друг с другом, и поэтому линкер так зол на вас - он не может использовать *.exe в качестве зависимости.

Простое решение: измените тип "hunspell" на "статическая библиотека (.lib)" или "динамическая библиотека (.dll)" и настройте "phtranscript", чтобы использовать его в качестве входной библиотеки.

Другие вопросы по тегам