预定义宏__GNUC__和_MSC_VER

一、预定义__GNUC__宏

    1 __GNUC__ 是gcc编译器编译代码时预定义的一个宏。需要针对gcc编写代码时, 可以使用该宏进行条件编译。

    2 __GNUC__ 的值表示gcc的版本。需要针对gcc特定版本编写代码时,也可以使用该宏进行条件编译。

    3 __GNUC__ 的类型是“int”,该宏被扩展后, 得到的是整数字面值。可以通过仅预处理,查看宏扩展后的文本。

示例:

  #include <assert.h>

  #include <stdio.h>

  #include <typeinfo>
  #ifndef __GNUC__

    #error sample for gcc compiler

  #else

    /* use gcc special extension: #warning , __attribute__, etc.  */

  #endif

  int main() 

  {    

    printf("hello gcc %d\n",__GNUC__);    

    assert( typeid(__GNUC__)==typeid(int) );    

    printf("press Enter to exit\n");    

    (void)getchar();

  }


二、预定义_MSC_VER宏

    1 _MSC_VER是微软C/C++编译器——cl.exe编译代码时预定义的一个宏。需要针对cl编写代码时, 可以使用该宏进行条件编译。

    2 _MSC_VER的值表示cl的版本。需要针对cl特定版本编写代码时, 也可以使用该宏进行条件编译。

    3 _MSC_VER的类型是"int"。该宏被扩展后,得到的是整数字面值。可以通过仅预处理, 查看宏扩展后的文本。

示例:

  /* _MSC_VER\_MSC_VER.cpp */

  #include <stdio.h>

   #include <stdlib.h>

   #include <typeinfo>

   #define TO_LITERAL(text) TO_LITERAL_(text)

   #define TO_LITERAL_(text) #text

   #ifndef _MSC_VER

     #error sample for msvc compiler

  #else

    /* use msvc special extension: #pragma message,__declspec,__stdcall,etc. */

    #pragma message("----------------------------------------\n")

    #pragma message("----------------------------------------\n")

    #pragma message("---------- hello msvc " TO_LITERAL(_MSC_VER) " -------------")

    #pragma message("\n----------------------------------------\n")

    #pragma message("----------------------------------------\n")

    extern __declspec(dllimport) void __stdcall declare_but_dont_reference(void);

   #endif
    

  int main()

  {    

    printf("hello msvc, version=%d\n",_MSC_VER);    

    printf("typeof _MSC_VER=\"%s\"\n",typeid(_MSC_VER).name());    

    system("pause"); /* msvc only on windows? */    

    return 0;

  }


原文链接: https://www.cnblogs.com/whiteyun/archive/2010/09/05/1818491.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    预定义宏__GNUC__和_MSC_VER

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/14734

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月7日 下午2:27
下一篇 2023年2月7日 下午2:29

相关推荐