How to detect LLVM and its version through #define directives?

后端 未结 8 1286
广开言路
广开言路 2020-12-24 04:33

The question is quite clear I think. I\'m trying to write a compiler detection header to be able to include in the application information on which compiler was used and whi

8条回答
  •  Happy的楠姐
    2020-12-24 05:11

    I agree that the best choice is to use has feature macroses, not version macroses. Example with boost:

    #include 
    
    #if defined(BOOST_NO_CXX11_NOEXCEPT)
     #if defined(BOOST_MSVC)
      #define MY_NOEXCEPT throw()
     #else
      #define MY_NOEXCEPT
     #endif
    #else
     #define MY_NOEXCEPT noexcept
    #endif
    
    void my_noexcept_function() MY_NOEXCEPT; // it's example, use BOOST_NOEXCEPT (:
    

    But anyway, if you need compiler version, you can use boost.predef:

    #include 
    #include 
    
    int main() {
    #if (BOOST_COMP_CLANG)
      std::cout << BOOST_COMP_CLANG_NAME << "-" << BOOST_COMP_CLANG << std::endl;
    #else
      std::cout << "Unknown compiler" << std::endl;
    #endif
      return 0;
    }
    

    Output examples:

    Clang-30400000
    Clang-50000000
    

提交回复
热议问题