c++xcodepch

What is the preprocessor define for C++ language in Xcode precompile header?


In my Prefix.pch file I am using __OBJC__ preprocessor define for compilation of Objective C headers. What is the equivalent for compilation of C++ headers?

#ifdef __OBJC__
    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
#endif

Solution

  • There is a standard preprocessor constant, __cplusplus. Its value is expanded to version number of C++ standard being used:

    __cplusplus

    denotes the version of C++ standard that is being used, expands to value 199711L (until C++11), 201103L (C++11), 201402L (C++14), or 201703L (C++17)

    Source: cppreference

    So, you can write, for example:

    #ifdef __cplusplus
      #if __cplusplus >= 201103L
        // include new stuff
      #else
        // use legacy features
      #endif
    #endif