Character set encoding in Visual Studio 2013

By , last updated August 10, 2019

Visual Studio character set encoding is not only important for representation of local character sets. It’s also an important factor that will let you compile different libraries together. If you are using many different libraries you must decide what character set your project should use.

In Visual Studio you can select different character sets. In general, if you’re not sure if you need those, then you don’t need it. You’ll know when you need to use those character sets.

Read also: How to show compile build time in Visual Studio

This rather annoying warning comes if you mix character sets in your application and libraries. Stay consistent and use only one of Not SetUnicode, or Multibyte Character Set in all your libraries and applications.

LINK : warning LNK4098: defaultlib 'mfc120.lib' conflicts with use of other libs; use /NODEFAULTLIB:library
LINK : warning LNK4098: defaultlib 'mfcs120.lib' conflicts with use of other libs; use /NODEFAULTLIB:library

Open up your project property settings (right click your project in the solution explorer) and make sure all projects and all configurations use the same character set.

property_pages_character_set

I usually use Not Set, unless I plan to use other than English characters in my applications. Anything else is a hornet nest of incompatibilities.

Behind the covers, Visual Studio and the Microsoft SDK, will do some things if you select Unicode. It will define the macro _UNICODE.

A very common WinAPI method is MessageBox. It comes in two flavors, an ASCII version (MessageBoxA) and a Wide version (MessageBoxW).

#ifdef UNICODE
#define MessageBox  MessageBoxW
#else
#define MessageBox  MessageBoxA
#endif // !UNICODE

The definition of MessageBoxA and MessageBoxW are similar, but with one crucial difference.

WINUSERAPI
int
WINAPI
MessageBoxA(
    _In_opt_ HWND hWnd,
    _In_opt_ LPCSTR lpText,
    _In_opt_ LPCSTR lpCaption,
    _In_ UINT uType);
WINUSERAPI
int
WINAPI
MessageBoxW(
    _In_opt_ HWND hWnd,
    _In_opt_ LPCWSTR lpText,
    _In_opt_ LPCWSTR lpCaption,
    _In_ UINT uType);

The former uses LPCSTR, and the latter use LPCWSTR. And much of the WinAPI goes like this.

If you have two modules built with different character sets, you can get crazy results when running the program (if you’re able to build and run it).