How to add a .mm file into the project? - xcode

I need to add a .mm file which contains both ObjectC and C++, I am wondering how to do it using XCode 4?

There are absolutely no issues in doing that, just add the .mm fle to the project and XCode will compile it as Objective-C++.
The only caveat is that the .h associated with that ObjC++ code must not contain any C++ specific code or every file in which that header is included must be .mm too.
This because XCode will use different compiler according to the single file, so if a .m is found it will try to compile it as plain ObjC and not ObjC++. You can force to compile it with che ObjC++ compiler but I suggest you to follow the principle described or rename other files to .mm just to avoid getting things complicated.

Related

static library in xcode 4.4.1

I need to make a static library in xcode of my project , so that i can use it other project by linking it, I have through the tutorial of making static library on icodeblog.com
I have some of the following questions ??
1)- Wat does a library actually contain ? does it contain compiled version of both the .h and .m files of the project or just of the .m files ?
2)-If It contains compiled .h and .m files , then why do we need to add .h files in the project in which we are using the static library(By using the copy headers option)
3)-Even after adding .h files to that project , then why does the following error comes ?
"$OBJC_CLASS_NAME appeared in CLASS.o" not found ...
To make a static library I heartily recommend this approach
To answer your questions:
It contains object code for the contents of your .h and .m files.
The header files lets you use the code in the library. If there are no header files, your project would not know what to call.
If you are using the correct header files, this indicates to me that you are not linking against the correct library or that the library is incorrectly built. Is the guide you have been using correct? The guide I am pointing to works for me and many others.
I got the answer for problem...it is because libraries cannot be tested on Simulator
need to test on devices only ..
thats why . i was getting the error mentioned in "3)"

Xcode not recognizing c++ syntax

I have a project in xcode where I use a c++ library, it use to work and compile correctly, but whithout any change now it won't compile telling some errors in the .h files libraries and in my view controller where I use some c++ variables, I have my file with the .mm extension and all the errors I'm having are about the c++ syntax like in the word namespace it tells it's Unknown type name or in every other line where the syntax is c++ type Xcode can't recognize it.
Found out by my self on this page http://answers.oreilly.com/topic/631-how-to-get-c-and-objective-c-to-play-nicely-in-xcode/
The problem was that the compiler was making a mess because only the files where I used c++ code I put them the .mm extension and there was some files with just .m
By changing the extension of every file in my project to .mm even if it hasn't any c++ code solved the problem.

Xcode Project #include <cmath>

I'm trying to use code that already works in another standalone project without problems.
When I bring this code into my other final project, it indicate that 'cmath' file not found.
The file with the #include is in an .hpp file (it just defines some structs for opengl stuff), so there is not a corresponding .mm file. (This is C++ code and I'm mainly an objective-c user so not sure of all the c++ stuff)
But this is for opengl stuff and works fine we not in this project.
I've tried everything to make this work.
The final project with the problem has other code that uses #include without problems.
It is almost like something is causing xcode not to recognize the path the header anymore.
I've checked its file type it is "Default C++ header"
In the final project I'm using Zxing and also using CorePlots. Not sure if they are causing any problems. Also some file are using #include not sure if that could conflict with the #incude or not. (But again the other files with #include are working fine.
Any help will be greatly appreciated...
Alternately to Jon Reid's good advice you can pick "Compile as Objective-C++" in the Build Settings. (Search for "Compile sources" in the Build Settings window.) This is a little bit wild-west but is a quick way to see if the problem is in fact your C++ source compiling as C or Objective-C
Your header file doesn't exist by itself; something must import it. Rename the importing files, changing their file types from .m to .mm.
For example, let's say your header that includes <cmath> is named foo.h. And that this in turn is used by a bar module, composed of bar.h and bar.m. So foo.h is imported in either bar.h or bar.m.
Rename bar.m to bar.mm so that it uses C++. Do the same for all .m files that depend on foo.h.

Xcode objective C++ -- avoid making .hh files?

To get header files with both Objective C and C++ objects to work, I have to rename them from .h to .hh. But my colleague uses .h with no problems. Neither of us understands why.
Can anyone explain?
I guess your colleague's project settings have the Compile Sources As set to Objective-C++ and you don't.

Xcode .m vs. .mm

In the zxing iphone project the readme states:
It can happen that when trying to build your own project with
ZXingWidgetController you get linker
errors like "undefined reference to".
If this error looks like a c++
undefined reference, then renaming
main.m into main.mm (Objective-C++
source suffix) may fix the problem
It did indeed. But I'm wondering why?
.mm extension stands for Objective-C++, when compiler can process C++ classes.
But when using .m extension it will be able to compile only C code, without C++ classes.
Both .m and .mm are class file extensions of source code for Mac-based applications. .m files can contain both Objective-C and Objective-C++ classes. To avoid conflicts between the two in mixed-use scenarios there's the convention to rename all Objective-C++ class files to .mm. This helps compilers to distinguish.
So in a project which uses both Objective-C and Objective-C++ you will see:
.m files containing Objective-C
.mm files containing Objective-C++

Resources