Saturday, October 23, 2021

Visual C++ project configuration: treat wchar_t as built in type

Visual C++ project configuration: Configuration Properties ==>C/C++ ==> Language==> treat wchar_t as built in type: YES or NO

    One Document

  1. 1.24. Enforcing Strict Conformance to the C++ Standard
  2. Problem You want your compiler to accept only programs that conform to the C++ language standard.

    Solution Command-line options for specifying strict conformance to the C++ standard are listed in Table 1-37. Instructions for enforcing strict conformance from your IDE are given in Table 1-38..

    Visual C++ IDE: From your project’s property pages, go to Configuration Properties→ C/C++→ Language

    1. Set Disable Language Extensions,
    2. Treat wchar_t as Built-in Type, and
    3. Force Conformance in For Loop Scopes to Yes.
  3. Set compiler and build properties
  4. In the IDE, all information that is needed to build a project is exposed as properties. This information includes the application name, extension (such as DLL, LIB, EXE), compiler options, linker options, debugger settings, custom build steps, and many other things. Typically, you use property pages to view and modify these properties. To access the property pages, choose Project > projectname Properties from the main menu, or right-click on the project node in Solution Explorer and choose Properties..

  5. Compiler options listed by category
  6. This article contains a categorical list of compiler options. For an alphabetical list, see Compiler options listed alphabetically..

  7. Linker options
  8. LINK.exe links Common Object File Format (COFF) object files and libraries to create an executable (.exe) file or a dynamic-link library (DLL)..

  9. Linking
  10. In a C++ project, the linking step is performed after the compiler has compiled the source code into object files (*.obj). The linker (link.exe) combines the object files into a single executable file.

    Linker options can be set inside or outside of Visual Studio. Within Visual Studio, you access linker options by right-clicking on a project node in Solution Explorer and choosing Properties to display the property pages. Choose Linker in the left pane to expand the node and see all the options..

    Questions & Answers

  11. wchar_t is not treated as built-in type even when the option is enabled
  12. wchar_t is not treated as built-in type even when the option is enabled.

  13. /Zc:wchar_t (wchar_t Is Native Type)
  14. Parse wchar_t as a built-in type according to the C++ standard.

    Remarks If /Zc:wchar_t is on, wchar_t is a keyword for a built-in integral type in code compiled as C++. If /Zc:wchar_t- (with a minus sign) is specified, or in code compiled as C, wchar_t is not a built-in type. Instead, wchar_t is defined as a typedef for unsigned short in the canonical header stddef.h. (The Microsoft implementation defines it in another header that is included by stddef.h and other standard headers.)

    We do not recommend /Zc:wchar_t- because the C++ standard requires that wchar_t be a built-in type. Using the typedef version can cause portability problems. If you upgrade from earlier versions of Visual Studio 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-.

    The /Zc:wchar_t option is on by default in C++ compilations, and is ignored in C compilations. The /permissive- option does not affect /Zc:wchar_t.

    Microsoft implements wchar_t as a two-byte unsigned value. It maps to the Microsoft-specific native type __wchar_t. For more information about wchar_t, see Data Type Ranges and Fundamental Types.

  15. /permissive- (Standards conformance)
  16. Specify standards conformance mode to the compiler. Use this option to help you identify and fix conformance issues in your code, to make it both more correct and more portable..

    Remarks The /permissive- option is supported in Visual Studio 2017 and later. /permissive is supported in Visual Studio 2019 version 16.8 and later.

    You can use the /permissive- compiler option to specify standards-conforming compiler behavior. This option disables permissive behaviors, and sets the /Zc compiler options for strict conformance. In the IDE, this option also makes the IntelliSense engine underline non-conforming code.

    The /permissive- option uses the conformance support in the current compiler version to determine which language constructs are non-conforming. The option doesn't determine if your code conforms to a specific version of the C++ standard. To enable all implemented compiler support for the latest draft standard, use the /std:c++latest option. To restrict the compiler support to the currently implemented C++20 standard, use the /std:c++20 option. To restrict the compiler support to the currently implemented C++17 standard, use the /std:c++17 option. To restrict the compiler support to more closely match the C++14 standard, use the /std:c++14 option, which is the default.

  17. How to resolve a specific LNK2001 error using visual studio 2015 (C++ project)
  18. 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?.

  19. This is a paragraph.

  20. This is a paragraph.

  21. This is a paragraph.

  22. This is a paragraph.

  23. This is a paragraph.

  24. This is a paragraph.

  25. This is a paragraph.

  26. This is a paragraph.

  27. This is a paragraph.

No comments:

Post a Comment