Click here to Skip to main content
15,887,854 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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.
Posted
Updated 16-Mar-16 5:28am
Comments
RedDk 16-Mar-16 12:50pm    
Try adding /V on commandline (LINKER) ... attempt recompile so that you can see what the linker is trying to reference and CANNOT. Then find the mangled name. Copy it to clipboard then go to windows search and narrowing the filetype to *.lib find that stubb in the list that comes back. You are missing that reference. Plain and simple.

1 solution

See /Zc:wchar_t (wchar_t Is Native Type)[^]:
Quote:
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-.

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:
C++
// Library header file
// Old
//int Connect(LPWSTR str);
// New
int Connect(__wchar_t *str);

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


[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]
 
Share this answer
 
v2
Comments
nv3 17-Mar-16 6:01am    
Good idea, Jochen.
Member 12396519 17-Mar-16 9:31am    
Thank you Jochen! That idea worked like a charm! I am kind of new to this stuff so thank you for showing exactly what had to be done in the header file and when calling.

Steve
Jochen Arndt 17-Mar-16 9:41am    
Thank you for your feedback and fine to hear that it is working.

Because you are new, do you understand what is done here or should I explain it?

I already thought about adding some explanation to the solution but decided to wait until it has been verified as working.
Member 12396519 17-Mar-16 10:16am    
It definitely worked. I would be interested to see further explanation, it can only enhance my understanding. Data types, casting, etc, is pretty confusing at this point!
Jochen Arndt 17-Mar-16 10:35am    
Done. I hope it is understandable.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900