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
//Objective C++ specific code here
//Plain C++ specific code here
The problem was that by default, XCode determines the file type by the extension and not by the project, so it thought that the .cpp file was a plain C++ file even though it's part of an Objective C++ project, so it didn't define the
__OBJC__ macro in that file. The solution was to change the compilation mode by right clicking on the .cpp file in the file list in the left part of XCode, click on "Show File Inspector" and in the "Type" dropdown in the right side of XCode select "Objective-C++ Source" instead of "Default - C++ Source":
This will make XCode think that the .cpp file is an Objective C++ file so it will define the
Since this is a per-project setting, it only has effect on the Objective C++ project and does not change anything in the plain C++ project, so
#ifdef __OBJC__ will be a way to check which project the file is being compiled in.