【问题标题】:unresolved external symbol on unitest file (visual studio)unitest文件上未解析的外部符号(Visual Studio)
【发布时间】:2015-05-11 02:53:19
【问题描述】:

我正在使用 c++ 和 Visual Studio 进行我的第一次测试,但我不知道为什么,当它没有在程序中发生时,我遇到了“未解析的外部符号”的问题。

错误 97 错误 LNK2001: 无法解析的外部符号“public: static 类 Session * __cdecl Session::Instance(void)" (?Instance@Session@@SAPAV1@XZ) (...)\MyTest\Messages.obj MyTest

错误 98 错误 LNK2001: 无法解析的外部符号 "public: char * __thiscall Config::getLanguage(void)" (?getLanguage@Config@@QAEPADXZ) (...)\MyTest\Messages.obj MyTest

错误 99 错误 LNK2001:无法解析的外部符号“类 wxString 字符串翻译” (?StringTranslate@@3VwxString@@A) (...)\MyTest\Messages.obj MyTest

错误 100 错误 LNK2001: 无法解析的外部符号 "public: void __thiscall Session::addMSG(class std::shared_ptr)" (?addMSG@Session@@QAEXV?$shared_ptr@VMessage@@@std@@@Z)(...)\speech-translation-tools\MyTest\Messages .obj 我的测试

错误 101 错误 LNK2001: 无法解析的外部符号 "public: char * __thiscall Config::getNick(void)" (?getNick@Config@@QAEPADXZ) (...)speech-translation-tools\MyTest\Messages.obj MyTest

错误 102 错误 LNK2001: 无法解析的外部符号 "public: char * __thiscall Config::getServerAddress(void)" (?getServerAddress@Config@@QAEPADXZ) (...) MyTest\Messages.obj MyTest 错误 103 错误 LNK1120:6 个未解决的外部问题

测试文件:

#include "stdafx.h"
#include "CppUnitTest.h" 
#include <wx/wx.h>
#include <wx/stattext.h> 
#include "data/Session.h"

#include "lib/ClientTS.cpp"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace MessageTXT    
{
    TEST_CLASS(UnitTest1)
    {

    public:
        TEST_METHOD(TEST_MESSAGE_SEND)
        {
            ClientTS *clientts = new ClientTS;
            wxString sr = "enviar_mensaje";
            clientts->sendMessage(&sr);
            clientts->disconnect();

        }
    };
}

Clientts.h

#pragma once

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include <wx/wx.h>
#include <wx/frame.h>
#else
#include <wx/wxprec.h>
#endif

#include "../data/Session.h"
#include "../data/Message.h"
#include "../data/Config.h"

#include "EventType.h" 

#include <wx/sizer.h>
#include <wx/wx.h>
#include <wx/timer.h>
#include <wx/stattext.h>
#include <wx/richtext/richtextctrl.h>
#include <wx/textctrl.h>
#include <wx/button.h>
#include <wx/grid.h>
#include "../gui/NationList.h"
#include "../gui/NationInfo.h" 
#include "../ArchiveLog.h"

#include "../GlobalVariables.h"
#include "../translateController/translateController.h"
#include "../translateController/translateVariable.h"

#include <list>
#include <functional> 

#define MENU_ESCI 1800
#define MENU_OPZIONI 1801
#define MENU_SPEECH 1802


typedef std::function<void()> cbClientTsFrm;


class ClientTS  {
public:
    static Session* session;
    static ConfigPTR config;
    static bool flagSave;
    static char LANG_MSG_SRC[500];
    static char MSG_SRC[500];
    static cbClientTsFrm notifyMSGcb;
    //static ISoundEngine* engine;              //Audio Engine to record sound
    static IAudioRecorder* recorder;            //Flow of audio daa
    //static uint64 scHandlerID;

    static char identity[IDENTITY_BUFSIZE];

    /* Create struct for callback function pointers */
    static struct ClientUIFunctions funcs;
public:
    ClientTS(){
        session = Session::Instance();
        config = session->getConfig();
    }

    virtual ~ClientTS(){}

    static void sendMessage(wxString *msgToSend);

    static void disconnect();
    static char* getLANG_MSG_SRC(){ return LANG_MSG_SRC; }

    static char* getMSG_SRC(){ return MSG_SRC; }
    static IAudioRecorder* getIAudioRecorder(){ return recorder; }

    static bool getFlagSave(){ return flagSave; }

    static void setFlagSave(bool flg){ flagSave = flg; }
    template <typename Observer>
    static void setCBClientTSMSG(Observer && fn){ notifyMSGcb = std::forward<Observer>(fn); }

    static void speak(char *LANG, char*MSG);
    static void Print(char*word);
    static size_t read_callback(static void *ptr, size_t size, size_t nmemb, static void *userp);
    static void writeWaveFile(const char* filename, SAudioStreamFormat format, static void* data);
    static void SetupColor(COLORE *c);
    static void onConnectStatusChangeEvent(uint64 serverConnectionHandlerID, int newStatus, unsigned int errorNumber);
    static void onNewChannelEvent(uint64 serverConnectionHandlerID, uint64 channelID, uint64 channelParentID);
    static void onNewChannelCreatedEvent(uint64 serverConnectionHandlerID, uint64 channelID, uint64 channelParentID, anyID invokerID, const char* invokerName, const char* invokerUniqueIdentifier);
    static void onDelChannelEvent(uint64 serverConnectionHandlerID, uint64 channelID, anyID invokerID, const char* invokerName, const char* invokerUniqueIdentifier);
    static void onClientMoveEvent(uint64 serverConnectionHandlerID, anyID clientID, uint64 oldChannelID, uint64 newChannelID, int visibility, const char* moveMessage);
    static void onClientMoveSubscriptionEvent(uint64 serverConnectionHandlerID, anyID clientID, uint64 oldChannelID, uint64 newChannelID, int visibility);
    static void onClientMoveTimeoutEvent(uint64 serverConnectionHandlerID, anyID clientID, uint64 oldChannelID, uint64 newChannelID, int visibility, const char* timeoutMessage);
    static void onTalkStatusChangeEvent(uint64 serverConnectionHandlerID, int status, int isReceivedWhisper, anyID clientID);
    static void onIgnoredWhisperEvent(uint64 serverConnectionHandlerID, anyID clientID);
    static void onServerErrorEvent(uint64 serverConnectionHandlerID, const char* errorMessage, unsigned int error, const char* returnCode, const char* extraMessage);
    static void onUserLoggingMessageEvent(const char* logMessage, int logLevel, const char* logChannel, uint64 logID, const char* logTime, const char* completeLogString);
    static void onCustomPacketEncryptEvent(char** dataToSend, unsigned int* sizeOfData);
    static void onCustomPacketDecryptEvent(char** dataReceived, unsigned int* dataReceivedSize);
    static void onEditMixedPlaybackVoiceDataEvent(uint64 serverConnectionHandlerID, short* samples, int sampleCount, int channels, const unsigned int* channelSpeakerArray, unsigned int* channelFillMask);
    static void showChannels(uint64 serverConnectionHandlerID);
    static void showChannelClients(uint64 serverConnectionHandlerID, uint64 channelID);
    static void onTextMessageEvent(uint64 serverConnectionHandlerID, anyID targetMode, anyID toID, anyID fromID, const char* fromName, const char* fromUniqueIdentifier, const char* message);
    static void showClients(uint64 serverConnectionHandlerID);
    static void createChannel(uint64 serverConnectionHandlerID, const char *name);
    static void deleteChannel(uint64 serverConnectionHandlerID);
    static void renameChannel(uint64 serverConnectionHandlerID);
    static void switchChannel(uint64 serverConnectionHandlerID);
    static void toggleVAD(uint64 serverConnectionHandlerID);
    static void setVadLevel(uint64 serverConnectionHandlerID);
    static void requestWhisperList(uint64 serverConnectionHandlerID);
    static void requestClearWhisperList(uint64 serverConnectionHandlerID);
    static void toggleRecordSound(uint64 serverConnectionHandlerID);
    static int readIdentity(char* identity);
    static int writeIdentity(const char* identity);
    static uint64  enterChannelID();
    static void createDefaultChannelName(char *name);
    static void enterName(char *name); 
    static void emptyInputBuffer();

    static DWORD WINAPI TTS_THREAD(LPVOID lpParameter);
    static DWORD WINAPI CTRL_STT(LPVOID lpParameter);
    static DWORD WINAPI ClientStart(LPVOID lpParameter);
    static DWORD WINAPI STT_THREAD(LPVOID lpParameter);
};

【问题讨论】:

    标签: c++ unit-testing visual-c++ visual-studio-2013


    【解决方案1】:

    您正在包含一个 .cpp 文件

    #include "lib/ClientTS.cpp"
    

    应该吗?

    #include "lib/ClientTS.h"
    

    【讨论】:

    • 特别指出!但是,如果包含所有项目依赖项,则会触发编译错误(缺少 .h 的定义)或 LNK1169 / LNK2005 错误消息(即多重定义的符号)
    【解决方案2】:

    消息指出未解析的外部符号

    这意味着您的代码在语法上是正确的,但在将所有 .obj 和 .lib 文件链接在一起时,并没有找到您引用的所有外部符号(Session 类和Config 类成员函数)。

    这意味着您缺少依赖项:项目中缺少源文件,或者“更可能”是库文件。

    【讨论】:

    • 谢谢@christophe!你对如何解决这个问题有任何想法吗?也许..我的控制类可能以错误的方式编程,有什么建议吗?非常感谢!
    • 当然:您能提供您编译的项目中所有 cpp 的列表吗?你还能说出你在哪一个中定义了上面提到的两个类吗?
    • 如果我把她放在存储库中查看列表可能会更容易。感谢你的宝贵时间。 github.com/danieltoledo/speech-translation-tools/tree/teamspeak/…
    • 它更简单,但并不能完全满足需求。我可以在data 文件夹中找到ConfigSession。我想您已经将这些模块编译为您的库或应用程序的一部分。但是你没有为你的单元测试创​​建一个单独的项目吗?您是否在测试项目中包含了相关的源文件?或者,您是否将 .lib/.dll/.obj 文件作为项目的外部依赖项包含在内?如果不确定,能否在 github 存储库中添加 .sln 和 .vcxproj 文件?
    • 在编译时但在链接时没有出现错误的事实表明您已插入所有必需的声明(因此编译器知道会话和配置)但没有定义(即.相应的cpp)。您的错误消息是指带有 Message.cpp 的文件夹 MyTest。我在你的 github 上找不到这个。此外,您的存储库中唯一的项目文件根本不包含任何消息。最后但同样重要的是,过了一会儿,我找到了一个 MyTest.dll。我用dumpbin分析了一下,和你的错误信息无关。只有部分信息很难提供帮助:-/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-06
    • 2014-07-28
    • 2011-10-02
    • 2015-10-30
    • 1970-01-01
    相关资源
    最近更新 更多