如何使用visual studio 2015(C ++项目)解决特定的LNK2001错误 [英] How to resolve a specific LNK2001 error using visual studio 2015 (C++ project)

查看:116
本文介绍了如何使用visual studio 2015(C ++项目)解决特定的LNK2001错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从历史上看,为了让我们的项目成功编译和链接,编译器选项'Treat WChar_t as built in type'一直设置为No.



现在我们通过使用第三部分库文件添加新功能。通过测试看来,为了成功使用这个库,上面的编译器选项必须设置为Yes或者我会得到一个链接错误:



错误LNK2001未解析的外部符号public:int __thiscall BluetoothClient :: Connect(unsigned short *)(?Connect @ BluetoothClient @@ QAEHPAG @ Z)cmj C:\dev\Products\cmj\cmj\VC7 Project\main.obj



BluetoothClient :: Connect方法接受一个LPWSTR类型的变量,它是根据WCHAR定义的,WCHAR是根据wchar_t(winnt.h)定义的。 />


如果我将编译器选项设置为yes,它将在我的测试项目中成功链接(并运行)。



但是,如果我为真实项目设置为yes,则会在项目的其他区域中导致数百个C2664编译器错误,其中wchar_t和tchar类型的变量等等,作为参数传递。



如何在不打开'Treat WChar_t as built in type'编译器选项的情况下解决此链接错误?

(如果有帮助,我可以提供更多细节)。



我尝试过:



我尝试将参数传递给BluetoothClient :: Connect以多种方式。我认为问题在于BluetoothClient :: Connect接受类型为LPWSTR的参数,该参数在wchar_t中是不定义的,这不是一个定义的类型(我不能使编译器opion on ...导致太多其他编译器错误)。



BluetoothClient :: Connect包含在库中,我没有源代码。

Historically, in order for our project to compile and link successfully the compiler option 'Treat WChar_t as built in type' has always been set to No.

Now we are adding a new feature through the use of a 3rd part library file. Through testing it appears that in order to use this library successfully the above compiler option must be set to Yes or I will get a linking error:

Error LNK2001 unresolved external symbol "public: int __thiscall BluetoothClient::Connect(unsigned short *)" (?Connect@BluetoothClient@@QAEHPAG@Z) cmj C:\dev\Products\cmj\cmj\VC7 Project\main.obj

The BluetoothClient::Connect method accepts one variable of type LPWSTR which is defined in terms of WCHAR which is defined in terms of wchar_t (winnt.h).

If I set the compiler option to yes it will link successfully (and run) in my test project.

​But, ​If I set it to yes for the real project it causes hundreds of C2664 compiler errors in other areas of the project where variables of type wchar_t and tchar, etc, are passed as arguments.

How can I resolve this linking error without turning on the 'Treat WChar_t as built in type' compiler option?
(I can provide more details if it helps).

What I have tried:

I have tried passing the argument to BluetoothClient::Connect many different ways. I think the problem is that BluetoothClient::Connect accepts an argument of type LPWSTR which is defiend in termes of wchar_t which is not a defined type (I can't tur that compiler opion on...causes too many other compiler errors).

BluetoothClient::Connect is contained ina library for whcih I do not have the source code.

推荐答案

请参阅 / Zc:wchar_t(wchar_t是原生类型) [ ^ ]:

See /Zc:wchar_t (wchar_t Is Native Type)[^]:
引用:

如果从早期版本的Visual C ++升级并遇到编译器错误C2664,因为代码试图将wchar_t隐式转换为unsigned short,我们建议您更改修复错误的代码,而不是设置/ Zc:wchar_t - 。

If you upgrade from earlier versions of Visual C++ and encounter compiler error C2664 because the code is trying to implicitly convert a wchar_t to unsigned short, we recommend that you change the code to fix the error, instead of setting /Zc:wchar_t-.



可能有一个解决方案,但我不是确定它是否有效(只需试一试):



更改函数声明中的参数类型(例如通过编辑库头文件)到 __ wchar_t (resp。 * __ wchar_t const * __ wchar_t )并在调用函数时强制转换参数:


There may be one solution but I'm not sure if it works (just give it a try):

Change the parameter types in the function declarations (e.g. by editing the library header file) to __wchar_t (resp. *__wchar_t and const *__wchar_t) and cast the parameters when calling the functions:

// Library header file
// Old
//int Connect(LPWSTR str);
// New
int Connect(__wchar_t *str);




// Cast when calling
wchar_t lpParam[] = L"test";
BluetoothClient::Connect((__wchar_t *)lpParam);





[更新]

解决方案经过验证工作后,我会解释在这里做了什么。



取决于编译器设置, wchar_t 定义为 unsigned short 或内置类型 __ wchar_t



因为库是使用内置类型构建的,所以函数声明必须使用该类型才能使链接器满意。使用相同设置构建应用程序时, LPWSTR 将根据需要 __ wchar_t * 。否则 LPWSTR 将是 unsigned short * ,这会导致链接器错误。因此,更改函数声明以使用正确的类型 __ wchar_t * 可以避免链接器错误。



但是这样做编译器会抱怨传递 LPWSTR 参数( unsigned short * ),其中预计__wchar_t * 。这可以通过将参数转换为所需类型来解决。

[/ UPDATE]



[UPDATE]
After the solution has been verified as working, I will explain what is done here.

Depending on the compiler setting, wchar_t is defined as unsigned short or the built-in type __wchar_t.

Because the library has been build using the built-in type, the function declaration must use that type to make the linker happy. When the application is build with the same setting, LPWSTR will be __wchar_t * as required. Otherwise LPWSTR will be unsigned short * which leads to the linker error. So changing the function declaration to use the correct type __wchar_t * avoids the linker error.

But when doing so the compiler would complain about passing a LPWSTR argument (which is unsigned short *) where a __wchar_t * is expected. This is solved by casting the parameter to the required type.
[/UPDATE]


这篇关于如何使用visual studio 2015(C ++项目)解决特定的LNK2001错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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