How can I check if a cpp file is being compiled in an Objective-C++ project using the preprocessor?

Viewed 1438

I have a C++ source file (.cpp) which is shared between two of my projects, one using Objective-C++ and the other just using plain C++. I have a part of that file where I need to use different code for the Objective-C++ project and for the plain C++ project, so I need to check if the project is being compiled using Objective-C++ or not. After some searching, I found that the __OBJC__ macro can be used to check if the file is compiled by an Objective-C compiler, so I tried this code:

#ifdef __OBJC__
    //Objective-C++ specific code here
#else
    //Plain C++ specific code here
#endif

The problem is that the __OBJC__ macro is never defined, not even in the Objective-C++ project. I also noticed that the __OBJC__ macro is defined in the .mm files in the Objective-C++ project, but not in the .cpp file.

In case it matters, I'm compiling the Objective-C++ project for iOS with Xcode, and the plain C++ project is a cross-platform computer program which can be compiled using Visual Studio, Xcode or GCC.

How can I check using the preprocessor if my .cpp file is being compiled in an Objective-C++ project or a plain C++ project?

1 Answers
Related