How to compile specific files in objective-c++ and the rest of the project in objective-c
Solution 1
Try renaming the files where you are including the library headers to myClass.h for interface and myClass.mm for implementation files. This forces the files to be compiled as objective-c++.
Solution 2
Select the file you want to compile as Objective C++ from the file navigator, and then select the File Type in the file inspector view. This is in Xcode 4, but there is a similar mechanism in Xcode 3.
Solution 3
I have resolved this problem:
- You should set "According to file type" to "Complile Sources As",
- Set "-ObjC++" to the "Other Linker Flags"
- The last,you should modify the files's suffix to .mm that reference the library method
Related videos on Youtube
Themikebe
Updated on May 30, 2022Comments
-
Themikebe almost 2 years
I'm currently busy on a project where I need to use an external accessory to read Mifare 1k tags.
The accessory was provided with an SDK, written in (Objective ?)C++ and I followed the instructions provided to set XCode to "Compile sources as: Objective-C++" and added "-Obj-C++" in "Other linkers flags.
The SDK compiles fine then, but trouble is I am already using several libraries in the project (such as ASIHTTPRequest, JSONKit, ...) and I get compilation problems because of those new settings in those libraries. If I switch back to the previous settings, I get compilation problems in the reader's SDK
The question is: is there a way to compile only the class from the SDK as C++ and the rest of the project as objective-c ?
Edit: the SDK files consists only of .h (and a linked library)
thanks for your help, Mike
-
Themikebe about 13 yearsThere are compilation errors, mostly conversion --- Reachability.m:200: error: invalid conversion from 'BOOL' to 'NetworkStatus' ---- ASIHTTPRequest/ASIHTTPRequest.m:443:0 ASIHTTPRequest/ASIHTTPRequest.m:443: error: invalid conversion from 'const void*' to 'const uint8_t*' ...
-
-
Themikebe about 13 yearsthanks for your answer, but I only have the headers accessible (and a linked library). Is there something else I can do ?
-
Marko Hlebar about 13 yearsAre the files of the class where you are including headers named like this: myClass.h myClass.mm?
-
Themikebe about 13 yearsI don't have anything imported just yet, it's just by changing the compilers flags and 'compile source as' option
-
Marko Hlebar about 13 yearsTry changing the compiler flags back, it should compile normally if you haven't included the library headers anywhere. Then try to do it as per my instructions.
-
Themikebe about 13 yearsthanks for your answer, I'll try this if I'm having trouble with the solution I found. (shared in the question at the moment)
-
Themikebe about 13 yearsthanks for your help, I found another solution which seems to compile (share in the question at the moment)
-
Anansi over 12 yearsHi I am using the same Mifare SDK and I have the same problem with the AsyncSocket library when I change the compiler settings in XCode. How did you solve the problem? Thanks.
-
Colin about 10 yearsThank you! In my case, could not change file extension because code was shared windows/mac #ifdef'd code. Setting -ObjC++ flag for the source file in Xcode was also not working for me, but this solution did work.
-
pmdj almost 3 yearsI'd say the accepted answer is a lot better than this solution. Using the
.mm
extension also communicates to any developers working with the files that they're using Objective-C++, and if those files are ever used in another target or project, they'll automatically use the correct language. -
Juraj Antas almost 3 yearsdepends. If same cpp file is used also on another platform and inside via defines are blocked some parts of it depending on platform, you have no choice. You have to use this flag because .mm is not going to be well accepted on Linux or Windows.