zoukankan      html  css  js  c++  java
  • 预定义宏__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 ",__GNUC__);    

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

        printf("press Enter to exit ");    

        (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("---------------------------------------- ")

        #pragma message("---------------------------------------- ")

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

        #pragma message(" ---------------------------------------- ")

        #pragma message("---------------------------------------- ")

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

       #endif
        

      int main()

      {    

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

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

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

        return 0;

      }





  • 相关阅读:
    Linux下g++编译与使用静态库和动态库(仅命令)
    Shell算数运算
    (转载)解决/usr/bin/ld: cannot find -lxxx 问题
    (转)C语言中的EOF和feof()
    204 Count Primes
    228 Summary Range
    235 Lowest Common Ancestor of a Binary Search Tree
    242 Valid Anagram
    简易计算器实现优化
    原生JS操作cookie
  • 原文地址:https://www.cnblogs.com/jeffen/p/5953825.html
Copyright © 2011-2022 走看看