17

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
  • 361
  • 1
  • 3
  • 11
  • Are you actually having compile errors or linker errors? Providing the actual errors can help generate better answers – Nektarios Apr 22 '11 at 14:07
  • 1
    There 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 Apr 22 '11 at 14:16

4 Answers4

25

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.

Selecting File Type from the file inspector

Casey Fleser
  • 5,707
  • 1
  • 32
  • 43
  • thanks 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 Apr 22 '11 at 14:41
  • 1
    Thank 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. – Colin May 06 '14 at 02:51
7

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++.

bentford
  • 33,038
  • 7
  • 61
  • 57
Marko Hlebar
  • 1,973
  • 17
  • 18
  • 1
    thanks for your answer, but I only have the headers accessible (and a linked library). Is there something else I can do ? – Themikebe Apr 22 '11 at 14:06
  • 1
    Are the files of the class where you are including headers named like this: myClass.h myClass.mm? – Marko Hlebar Apr 22 '11 at 14:09
  • I don't have anything imported just yet, it's just by changing the compilers flags and 'compile source as' option – Themikebe Apr 22 '11 at 14:23
  • Try 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. – Marko Hlebar Apr 22 '11 at 14:28
  • thanks for your help, I found another solution which seems to compile (share in the question at the moment) – Themikebe Apr 22 '11 at 14:42
  • Hi 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. – Anansi Aug 14 '11 at 03:14
6

I have resolved this problem:

  1. You should set "According to file type" to "Complile Sources As",
  2. Set "-ObjC++" to the "Other Linker Flags"
  3. The last,you should modify the files's suffix to .mm that reference the library method
Juan Mellado
  • 14,973
  • 5
  • 47
  • 54
dale
  • 311
  • 4
  • 5
0

well, in Build phases tab, there is a section Compile sources. For file you want to use Objective-C++ compiler add flag: -xobjective-c++

tested in Xcode 12.5

Juraj Antas
  • 3,059
  • 27
  • 37
  • I'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. – pmdj May 26 '21 at 08:57
  • depends. 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. – Juraj Antas May 26 '21 at 19:41