Xcode .m vs. .mm - xcode

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

Related

Including C++ header files in Objective-C++ when they conflict with Objective-C macros

In Xcode, I've created a "Cocoa application" project. One of its dependencies is a framework containing C++ code. I renamed AppDelegate.m to AppDelegate.mm and included the framework.
The project fails to compile. The problem is that the C++ header files in the framework are using some symbols that conflict with Objective-C or Cocoa.
The C++ header files are defining functions called verify() and check(), which conflict with /usr/include/AssertMacros.h in the MacOSX10.8 SDK.
The C++ header files contain a variable called NO, which conflicts with the Objective-C macro NO.
A workaround would be to modify the C++ code in the framework to avoid these conflicts. But since it's a large C++ project maintained by another organization, this would take time and would possibly break in future updates of the C++ project.
Is there some way just to tell Clang/Xcode to treat those C++ header files as C++ instead of Objective-C++?
Reading through the /usr/include/AssertMacros.h that comes with Mac OS 10.8, it looks like you could do:
#define __ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES 0
before including AssertMacros.h, which will prevent it from defining macros called verify() and check().
Regarding NO: you could use the preprocessor to rename that variable for you. For example:
#define NO NO_libraryname_renamed
#include <libraryname.hh>
#undef NO
Depending on how the NO variable is used by the library, this might cause problems — if the header is declaring it as extern, then your Cocoa app will refer to it by the wrong name, and you'll get an undefined symbol error. But as long as you're not using that variable, and the library isn't depending on your app to define that variable, then you should be fine.
(And please file a bug report with the offending library, requesting that they rename their variable.)
Mixing several languages is calling for grief. Even more so mixing Objective C++ (itself a strange hybrid) with C++. Don't do it.

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.

How to add a .mm file into the project?

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.

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.

Resources