项目属性-配置属性-c/c++-将wchar视为内置类型:否(/Zc:wchar_t-)

如果这样不行。

就把第3方lib提供的头文件里不能解析的函数的参数内的wchar改成 unsigned short。

 

C++, my almost favorite language. This language had good intentions, really. But this is such a pain -- a  pit of despair -- to have to deal with library compatibility and compilation parameters matching issues, where it should be handled by the system.

Anyway, if you're trying to use the Broadcom Bluetooth SDK for Windows CE with VS2005 or VS2008, you might encounter this pretty message :

error LNK2019: unresolved external symbol "__declspec(dllimport) public: int __cdecl CRfCommIf::SetSecurityLevel(wchar_t *,unsigned char,int)" (__imp_?SetSecurityLevel@CRfCommIf@@QAAHPA_WEH@Z)

The symbol actually exists in the broadcom provided library (BtSdkCE30.lib or BtSdkCE30.lib)  but is defined like this :

SetSecurityLevel@CRfCommIf@@QAAHPAGEH@Z

which, undecorated, means :

int CRfCommIf::SetSecurityLevel(unsigned short *,unsigned char,int)

See the difference ? The wchar_t type the compiler is using should be an unsigned short. Basically, both types are binary equivalents but the compiler is treating them as different, hence the different signature.

To fix this, just set the option "Treat wchar_t as Built-in Type" to No.

This way, the wchar_t will go back to being a short, and then match the method built in the static library.

I'm going back to my C# code, now.

 

 

 

 

在 Visual C++ 2005 中执行的一致性工作也可能导致 LNK2019;现在,默认情况下 /Zc:wchar_t 处于打开状态。可能不是所有模块都使用相同的 /Zc:wchar_t 设置编译的,这样,类型引用便没有解析为可兼容的类型。要解决此问题,请确保所有模块中的类型都是兼容的,方法是使用相应的 /Zc:wchar_t 设置进行编译(例如,要使用 Visual C++ 2005 工具集生成与以前版本中的模块链接的模块,请使用 /Zc:wchar_t-),或者尽可能更新类型以使其兼容。

现在,由于 /Zc:wchar_t 在默认情况下处于打开状态,从注释杂注或通过命令行显式引用 comsupp.lib 应改为使用 comsuppw.lib 或 comsuppwd.lib。使用 /Zc:wchar_t- 进行编译时仍应使用 comsupp.lib。

有关更多信息,请参见 Breaking Changes in the Visual C++ 2005 Compiler 和 /Zc:wchar_t(wchar_t 是本机类型)

下面的示例创建一个导出,该导出使用解析为 wchar_t 的 WCHAR。

 
// LNK2019g.cpp
// compile with: /LD
#include "windows.h"
// WCHAR resolves to wchar_t
__declspec(dllexport) void func(WCHAR*) {}

下面的示例生成 LNK2019:

 
// LNK2019h.cpp
// compile with: LNK2019g.lib
// LNK2019 expected
__declspec(dllimport) void func(unsigned short*);

int main() {
   func(0);
}

若要解决此错误,请将 unsigned short 更改为 wchar_t 或 WCHAR,或使用 /Zc:wchar_t- 编译 LNK2019g.cpp。