using c++ (.cpp) with objective-c (.m) - c++

sorry for my english is not good.
I'm trying to use pure c++ code in my iOS project but I have the next issues:
I create one new file product.cpp + product.h, I create his own methods and atributes, and I want to use this class in my viewcontroller.m but dont run, I change the extensión of the viewcontroller.m to viewcontroller.mm but dont run but if I change the extension of the appDelegate.m to appDelegate.mm this is ok and run.
My question is I always have to change mi files to .mm if I want to use .cpp?
:/

Generally yes, a file extension of .mm tells Xcode to invoke the Objective-C++ compiler, whereas .m tells the compiler to invoke the Objective-C compiler. You can set the compiler type on a per-file basis using the file-settings pane on the right (so you could force Objective-C++ compilation on a .m file), but this is non-intuitive and is likely to confuse future maintainers.
If you're willing to consider some advice from a long-time C++ and Objective-C user, I've done a lot of mixing of the two and over time it's more trouble than it's worth. You're much better off if sticking purely to Objective-C, and if you have some C++ library that you just have to integrate, then make a C wrapper for it.
It used to be the case that Xcode analyzer only worked on Objective-C files, not Objective-C++, so you'd lose a lot of the value that tool provides by writing Objective-C++ files. I'm not sure if Apple has changed it in the year or so since I last wrote any Objective-C++ but I imagine there isn't a lot of reason for them to. Stick with Objective-C if you can because the Analyzer is extremely helpful.

Related

Can you still use C++ in Xcode?

I want to learn C++ on my Mac computer. A lot of forums recommend using Xcode, but when I downloaded it I realized that it only has options for Swift or Objective-C. Is there still a way to use C++ in Xcode?
The parts of a problem that interact with system APIs for making an app have to be Swift or Objective-C. (As such, the project templates that give you the bare skeleton of an app to get started with are only Swift and ObjC.)
However, an Objective-C app can use C++ internally. Just create .mm files instead of .m (or rename the ones you have from the project template) so that the compiler knows you're writing Objective-C++. Then you can write ObjC classes that create C++ objects or call into C++ libraries, write C++ classes or templates that store pointers to ObjC objects, etc.

calling .cpp files from Objective-C class .m

I have a few classes written in c++ which needs to be integrate with my iOS project written in Objective-C. I got a Mac (C++) project from my client to integrate with my project. I need to call the methods in .cpp class from Objective-C class with NSNotificationCenter or some other way which is better do it?
If you make a file with extension .mm you can use both Objective-C and C++ inside that one. It can quickly get messy so i usually try to keep the C++ touchpoints contains in a few .mm files so the whole thing won't be a mix of Objective-C and C++ and c code.

including c++ libraries in ios programming

I'm developing an ios app that's very basic and uses objective almost all of the time. However my app needs to deal with big integer numbers (eg: 2^200) and add and multiply them. To achieve that I need to include a c++ library called bigint that allows these operations on huge integers. The problem I have is that when I include the bigint project I get many errors and I thought that this could be because it's c++ and can't mix with objective c. I'm new to this idea and was wondering if there are any steps i need to take to correctly add a c++ library to an objective c project. By the way I'm not using opengl or anything complicated just simple ui and some quartz stuff.
Update:
I did everything you guys said, I changed all the extensions to .mm and added the bigint library. My project ran perfectly without errors before doing these things. I get an error when i do this and I get an error even if I don't even add the library. just changing the file extensions to .mm gives me the following error.
This just doesnt make sense since everything ran fine before and I don't have any duplicates in my program. I have no idea why just changing the extensions to .mm could cause this error. Any ideas guys?
You can mix in C++ files, but use a .cpp suffix for them (and .hpp for their corresponding header files). If you want to mix C++ and Obj-C in the same file, you can do that, but give it a .mm suffix.

XCode 4.1 no longer seems to create header file when you create c++ file, any idea how to make a template?

Why does Apple seem to hate C++ so much; this always worked in Xcode in prior versions, and still works with .m files. Why do they remove features people actually use every day? Not everyone just uses Objective-C by itself. Is there some way to make the file template do this again?
The solution is to go to "/Developer/Library/Xcode/Templates/File Templates/C and C++", and copy __FILEBASENAME__.h from "Header File.xctemplate" into both "C File.xctemplate" and "C++ File.xctemplate"
No idea why this would've been changed in Xcode 4.1...

Can I mix Objective-C and C++ in a single application in Xcode?

I have some legacy code to reuse written in c++ with opengl but most of the examples online are written in objective c... so porting my code would be probably easier in a mixed env.
I would prefer to avoid a "library" solution with separated .so
Yes you can. Just add your c++ code in a file called *.cpp
You can even mix C++ and objective-c code in the same file, but make sure the file is named *.mm (instead of *.m). That's a gotacha that a lot of people fall far.
Considering you want to use some legacy C++ code, this is probably exactly what you are looking for: Sample code from Apple for Cocoa With Carbon or CPP
Yes, you can use both in a single project. If memory serves, the extension for "Objective-C++" files is .mm.