How to compile specific files in objective-c++ and the rest of the project in objective-c
Asked Answered
P

4

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

Photocopier answered 22/4, 2011 at 14:0 Comment(2)
Are you actually having compile errors or linker errors? Providing the actual errors can help generate better answersEncrinite
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*' ...Photocopier
C
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++.

Caddy answered 22/4, 2011 at 14:3 Comment(6)
thanks for your answer, but I only have the headers accessible (and a linked library). Is there something else I can do ?Photocopier
Are the files of the class where you are including headers named like this: myClass.h myClass.mm?Caddy
I don't have anything imported just yet, it's just by changing the compilers flags and 'compile source as' optionPhotocopier
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.Caddy
thanks for your help, I found another solution which seems to compile (share in the question at the moment)Photocopier
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.Cerell
R
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

Religieux answered 22/4, 2011 at 14:19 Comment(2)
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)Photocopier
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.Elodia
C
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++.

Caddy answered 22/4, 2011 at 14:3 Comment(6)
thanks for your answer, but I only have the headers accessible (and a linked library). Is there something else I can do ?Photocopier
Are the files of the class where you are including headers named like this: myClass.h myClass.mm?Caddy
I don't have anything imported just yet, it's just by changing the compilers flags and 'compile source as' optionPhotocopier
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.Caddy
thanks for your help, I found another solution which seems to compile (share in the question at the moment)Photocopier
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.Cerell
S
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
Stamin answered 20/4, 2012 at 14:49 Comment(0)
A
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

Astrogate answered 25/5, 2021 at 19:42 Comment(2)
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.Intrust
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.Astrogate

© 2022 - 2024 — McMap. All rights reserved.