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

15,888

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.

Selecting File Type from the file inspector

Solution 3

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
Share:
15,888

Related videos on Youtube

Themikebe
Author by

Themikebe

Updated on May 30, 2022

Comments

  • Themikebe
    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
      Themikebe about 13 years
      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
    Themikebe about 13 years
    thanks for your answer, but I only have the headers accessible (and a linked library). Is there something else I can do ?
  • Marko Hlebar
    Marko Hlebar about 13 years
    Are the files of the class where you are including headers named like this: myClass.h myClass.mm?
  • Themikebe
    Themikebe about 13 years
    I don't have anything imported just yet, it's just by changing the compilers flags and 'compile source as' option
  • Marko Hlebar
    Marko Hlebar about 13 years
    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.
  • Themikebe
    Themikebe about 13 years
    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
    Themikebe about 13 years
    thanks for your help, I found another solution which seems to compile (share in the question at the moment)
  • Anansi
    Anansi over 12 years
    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.
  • Colin
    Colin about 10 years
    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.
  • pmdj
    pmdj almost 3 years
    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.
  • Juraj Antas
    Juraj Antas almost 3 years
    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.