0

I don't know if this is some silly mistake from my end, or Xcode 4.2.1 is doing something, but I'm unable to compile even a simple project that has both C++ and ObjC files. I've done this in past, but somehow the compiler is treating the C++ header files as ObjC header and throwing silly errors like

Test.h

I'm able to compile if I force to compile all files as ObjectiveC++ from "Project Settings > Apple LLVM compiler 3.0 - Language > Compile Sources As". But, that is just a workaround and I don't want to fight any thing silly when the code has grown.

8
  • 2
    For files containing (or #including) both Objective-C and C++ code, have you given them the .mm file name extension? Commented Dec 22, 2011 at 18:36
  • What is the extension of the file that is being compiled? Commented Dec 22, 2011 at 18:36
  • There a 4 files in the project Test.h Test.cpp AppDel.h and AppDel.m. Test.* files contain pure c++ code, while AppDel.* have pure ObjectiveC code, the AppDel.m contains a '#import Test.h' Commented Dec 22, 2011 at 18:49
  • 2
    If AppDel.m imports Test.h, then it is not pure Objective-C code. So it has to be called AppDel.mm (or change the type manually) Commented Dec 22, 2011 at 18:53
  • I just tried renaming Test.cpp to Test.mm, no help Commented Dec 22, 2011 at 18:54

2 Answers 2

3

I just did this with a project I am working on. I believe @pmjordan is right, you need to rename any files referencing the C++ code to .mm files, rather than .m.

Sign up to request clarification or add additional context in comments.

4 Comments

but in future I'm planning to use box2d code, which contains all .cpp files, I don't think renaming is the real option
You can compile wholly-C++ files separately (just make sure they have the .cpp extension) from Objective-C files (.m), then use Obj-C++ files (.mm) to design the interface between box2d and your Cocoa code. They all compile down to .o files that can be linked together. The different files are just to tell the compiler what rules to follow for each compilation unit.
Yes, thanks, renaming the .m files that import c++ code to .mm has resolved my issue :)
Yes Just like C/C++ are different languages Obj-C/Obj-C++ are different languages. The compiler need to know which you are using.
0

In XCode 4.2 you can also change the type of individual files using the "File Type" setting in the "Identity and Type" inspector on the right pane. I would suggest looking into this if it only with particular header files that you're having trouble with. Maybe change the type from Objective C header to C++ header?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.