Tuesday, October 13, 2020

how to configure : _cplusplus macro variables in Visual Studio 2017

_cplusplus macro in Visual Studio 2017. how to use it?

  1. Predefined macros
  2. The Microsoft C/C++ compiler (MSVC) predefines certain preprocessor macros, depending on the language (C or C++), the compilation target, and the chosen compiler options.

    MSVC supports the predefined preprocessor macros required by the ANSI/ISO C99, C11, and C17 standards, and the ISO C++14 and C++17 standards. The implementation also supports several more Microsoft-specific preprocessor macros. Some macros are defined only for specific build environments or compiler options. Except where noted, the macros are defined throughout a translation unit as if they were specified as /D compiler option arguments. When defined, the macros are expanded to the specified values by the preprocessor before compilation. The predefined macros take no arguments and can't be redefined.

  3. /Zc (Conformance)
  4. You can use the /Zc compiler options to specify standard or Microsoft-specific compiler behavior.

    Syntax /Zc:option{,option}

  5. /permissive- (Standards conformance)
  6. 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.

    To set this compiler option in the Visual Studio development environment

    In Visual Studio 2017 version 15.5 and later versions, use this procedure:
    Open your project's Property Pages dialog box. Select the Configuration Properties > C/C++ > Language property page. Change the Conformance mode property value to Yes (/permissive-). Choose OK or Apply to save your changes.

    In versions before Visual Studio 2017 version 15.5, use this procedure:
    Open your project's Property Pages dialog box. Select the Configuration Properties > C/C++ > Command Line property page. Enter the /permissive- compiler option in the Additional Options box. Choose OK or Apply to save your changes.

  7. /Zc:__cplusplus (Enable updated __cplusplus macro)
  8. The /Zc:__cplusplus compiler option enables the __cplusplus preprocessor macro to report an updated value for recent C++ language standards support. By default, Visual Studio always returns the value "199711L" for the __cplusplus preprocessor macro.

    Syntax /Zc:__cplusplus[-]

  9. /std (Specify Language Standard Version)
  10. Enable supported C and C++ language features from the specified version of the C or C++ language standard.

    Syntax /std:c++14
    /std:c++17
    /std:c++latest
    /std:c11
    /std:c17

    By default, when code is compiled as C, the MSVC compiler doesn't conform to a particular C standard. It implements ANSI C89 with several Microsoft extensions, some of which are part of ISO C99. Some Microsoft extensions can be disabled by using the /Za compiler option, but others remain in effect. It isn't possible to specify strict C89 conformance.

    Starting in Visual Studio 2019 version 16.8, you may specify /std:c11 or /std:c17 for code compiled as C. These options specify conformance modes that correspond with ISO C11 and ISO C17. Because the new preprocessor is needed to support these standards, the /std:c11 and /std:c17 compiler options set the /Zc:preprocessor option automatically. If you want to use the traditional (legacy) preprocessor for C11 or C17, you must set the /Zc:preprocessor- compiler option explicitly. Setting the /Zc:preprocessor- option may lead to unexpected behavior, and isn't recommended.

    Since C17 is largely a bug fix release of ISO C11, MSVC support for C11 already includes all the relevant defect reports. At present, there are no differences between the C11 and C17 versions except for the __STDC_VERSION__ macro. It expands to 201112L for C11, and 201710L for C17.

    To set this compiler option in the Visual Studio development environment

    1. Open the project's Property Pages dialog box. For details, see Set C++ compiler and build properties in Visual Studio.
    2. Select Configuration Properties, C/C++, Language.
    3. In C++ Language Standard (or for C, C Language Standard), choose the language standard to support from the dropdown control, then choose OK or Apply to save your changes.

  11. Microsoft C++ language conformance table
  12. Mouse and KeyBoard Hooking utility with VC++
  13. it has good code snippet:

    6 #ifdef __cplusplus
    7 extern "C" {
    8 #endif // __cplusplus
    9 #define LIBSPEC __declspec(dllexport
    11 LIBSPEC BOOL InstallKeyBoardHook(HWND hWndParent);
    12 LIBSPEC BOOL UnInstallKeyBoardHook(HWND hWndParent);
    13 #undef LIBSPEC
    14 15 #define UWM_KEYBOARD_MSG ("UWM_KEYBOARD_USER_MSG")
    16 #ifdef __cplusplus
    17 }
    18 #endif // __cplusplus

  14. /Zp (Struct Member Alignment)
  15. Controls how the members of a structure are packed into memory and specifies the same packing for all structures in a module.

    Syntax
    /Zp[1|2|4|8|16]

  16. Linker options
  17. Some Handy Visual C++ Pre-Processor Macros
  18. The Lost Art of Structure Packing
  19. Objects and alignment

No comments:

Post a Comment