首页 文章

exe中未解决的外部符号错误,但在dll中没有

提问于
浏览
1

我有一个库(dll),它暴露了一个类及其构造函数,这些构造函数应该在其他模块(exe和dll)中使用 . 我能够从其他库模块实例化该类,但不能从exe模块实例化 . 我在链接期间收到链接器错误 - “错误LNK2019:未解析的外部符号” . 我很困惑为什么链接成功用于其他库项目而不是exe项目 . 有人可以帮我这个吗?

以下是类声明:

class __declspec(dllimport) MyException
{
public:
MyException(unsigned int _line, const char *_file, const char *_function, MyExceptionType _type, const wchar_t* _message = 0, ...);
};

这是整个错误:错误LNK2019:未解析的外部符号"__declspec(dllimport) public: __cdecl MyException::MyException(unsigned int,char const *,char const *,enum MyException::MyExceptionType,unsigned short const *,...)"(_imp ?? 0MyException @@ QAA @ IPBD0W4MyExceptionType @ 0 @ PBGZZ)在函数"public: __thiscall MyClassUsesMyException::MyClassUsesMyException(class SomeClass *,int)"中引用(?? 0MyClassUsesMyException @@ QAE @ PAVSomeClass @@ H @ Z)

MyClassUsesMyException正在'MyApp.cpp'中实例化 .

谢谢,拉克什 .

1 回答

  • 0

    Update: wchar_t Not Always Native

    经过相当长时间的信息交流并从OP获得更多信息后,问题至关重要:

    class __declspec(dllimport) MyException
    {
    public:
        MyException(unsigned int _line, 
            const char *_file, 
            const char *_function, 
            MyExceptionType _type, 
            const wchar_t* _message = 0, // <<== note wchar_t type
            ...);
    };
    

    可以将Visual C配置为将 wchar_t 视为本机类型 . 如果不将其视为本机类型, unsigned shortwchar_t 的指定宏替换 . 链接器抱怨上面的函数无法解析,但真正引起我注意的是未定义符号的尾部:

    ,unsigned short const *,...)
    

    注意 unsigned short . 这告诉我编译器在编译EXE时使用非本机 wchar_t . 我认为有可能将DLL编译为 wchar_t 配置为本机,从而引入不同的签名,因此在链接时不匹配 .

    如果你对这个问题感到惊讶,想象一下拉克什和我是多么惊讶= P


    Original Answer

    该类应该在具有预处理器逻辑的单个公共头中,以确定声明的正确导入/导出状态 . 像这样:

    MyDLL.h

    #ifndef MYDLL_H
    #define MYDLL_H
    
    // setup an import/export macro based on whether the 
    //  DLL implementing this class is being compiled or
    //  a client of the DLL is using it. Only the MyDLL.DLL
    //  project should define MYDLL_EXPORTS. What it is 
    //  defined as is not relevant. *That* it is defined *is*.
    
    #ifdef MYDLL_EXPORTS
    #define MYDLL_API __declspec(dllexport)
    #else
    #define MYDLL_API __declspec(dllimport)
    #endif
    
    class MYDLL_API MyException
    {
        // your class definition here...
    };
    
    #endif
    

    然后,在实现异常的DLL项目中(以及该项目中的 only ),将MYDLL_EXPORTS添加到项目配置中的预处理器定义列表中 .

相关问题