简体   繁体   中英

Visual Studio C++ WChar_t library conflict

I'm working on a C++ project using Visual Studio. At the moment I'm making use of 2 libraries, one of them (let's call it 'no.lib') requires the 'Treat WChar_t as built-in-type' property to be set to 'no', while the other one ('yes.lib') needs it to be 'yes'. So whatever I set the property to, my program will either fail to compile or fail to run.

I tried the solution described in this thread but it didn't work for me. Additionally, I would like to keep the setting off if possible, since my entire project is based on no.lib and I'm only using two functions of yes.lib.

What are my options?

Microsoft Docs gives an explanation:

f you write new code that has to interoperate with older code that still uses the typedef version of wchar_t, you can provide overloads for both the unsigned short and __wchar_t variations of wchar_t, so that your code can be linked with code compiled with /Zc:wchar_t or code compiled without it. Otherwise, you would have to provide two different builds of the library, one with and one without /Zc:wchar_t enabled. Even in this case, we recommend that you build the older code by using the same compiler that you use to compile the new code. Never mix binaries compiled with different compilers.

When /Zc:wchar_t is specified, _WCHAR_T_DEFINED and _NATIVE_WCHAR_T_DEFINED symbols are defined. For more information, see Predefined Macros.

I suggest that you could try to add the following code:

#ifndef _WCHAR_T_DEFINED
typedef unsigned short wchar_t;
#define _WCHAR_T_DEFINED
#endif

When compiling a C++ file, if /Zc:wchar_t is in effect then the compiler pre-defines _WCHAR_T_DEFINED . If /Zc:wchar_t- is in effect it doesn't - so the above snippet should work nicely with C++ as well.


You could refer to this troublesome method. The following is an example:

In fact, one of the functions in the lib uses wchar_t* as a string, and the other lib uses unsigned short * as a string.

If you don't recompile the lib, the solution is to change the header file. Now the function declarations in the header files of the two libs all declare to use wchar_t* as a string.

But in fact one of the functions exported by lib uses unsigned short * as a string.

If the A lib is compiled with "wchar_t as a built-in type" when compiled, the A library header file has the following function declaration

void FunctionA(const wchar_t *wsz);

The B lib was compiled with "wchar_t not as a built-in type" when compiled, and the B library header file has the following function declaration

void FunctionB(const wchar_t *wsz);

Because the B lib is compiled with "wchar_t not as a built-in type", the wchar_t in the B lib header file is actually unsigned short.

So now create a new project, set it to "wchar_t is a built-in type", then introduce two libraries A and B, and then modify the header file of the B library.

Change to:

void FunctionB(const unsigned short *wsz);

There will be no link errors. It's just a little troublesome when calling.

wchar_t wsz[] = L"Hello";
FunctionA(wsz);
FunctionB((const unsigned short *)wsz);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM