exe中有未解决的外部符号错误,但dll中没有 [英] Unresolved external symbol error in exe but not in dll

查看:121
本文介绍了exe中有未解决的外部符号错误,但dll中没有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个库(dll),该库公开了一个类及其应在其他模块(exe和dll)中使用的构造函数.我可以从其他库模块实例化该类,但不能从exe模块实例化该类.链接期间出现链接器错误-错误LNK2019:无法解析的外部符号".我很困惑为什么在其他库项目而不是exe项目中使用链接时会成功.有人可以帮我吗?

I have a library (dll) which exposes a class along with its constructors which are supposed to be used in other modules (exe and dll). I am able to instantiate that class from other library modules but not exe modules. I get the linker error - 'error LNK2019: unresolved external symbol' during linking. I am confused why linking succeeds when used in other library projects and not exe project. Can somebody help me with this?

以下是类声明:

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)公共:__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)

This is the whole error: error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl MyException::MyException(unsigned int,char const *,char const *,enum MyException::MyExceptionType,unsigned short const *,...)" (_imp??0MyException@@QAA@IPBD0W4MyExceptionType@0@PBGZZ) referenced in function "public: __thiscall MyClassUsesMyException::MyClassUsesMyException(class SomeClass *,int)" (??0MyClassUsesMyException@@QAE@PAVSomeClass@@H@Z)

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

MyClassUsesMyException is being instantiated in 'MyApp.cpp'.

谢谢Rakesh.

推荐答案

更新:wchar_t并非总是本地的

Update: wchar_t Not Always Native

经过相当长的信息交换并从OP中获得了更多信息之后,这个问题至关重要:

After a fairly long exchange of information and getting a little more info from the OP, the problem is essential this:

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的指定宏替换.链接器抱怨上面的函数无法解决,但是真正引起我注意的是未定义符号的结尾:

Visual C++ can be configured to either treat wchar_t as a native type or not. When not treated as a native type, unsigned short is the specified macro substitution for wchar_t. The linker was complaining about the above function being unresolvable, but what really caught my eye was this at the tail of the undefined symbol:

,unsigned short const *,...)

请注意unsigned short.这向我暗示,编译器在编译EXE时正在使用非本机wchar_t.我认为可能是DLL是使用wchar_t配置为本机编译的,从而引入了不同的签名,因此在链接时无法匹配.

Note the unsigned short. This hinted to me that the compiler was using non-native wchar_t when compiling the EXE. I considered it possible that maybe the DLL was compiled with wchar_t configured as native, thereby introducing a different signature and thus not matching up at link time.

如果您很惊讶这是问题所在,请想象我和Rakesh = P感到多么惊讶

If you're surprised this was the problem, imagine how surprised Rakesh and I were =P

原始答案

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

That class should be in a single common header with preprocessor logic to determine proper import/export state of the declaration. Like so:

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项目中(并且在该项目中),将MYDLL_EXPORTS添加到项目配置中的预处理器定义列表中.

Then, in your DLL project that implements the exception (and only in that project), add MYDLL_EXPORTS to the preprocessor defines list in your project configuration.

这篇关于exe中有未解决的外部符号错误,但dll中没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆