Can you still use C++ in Xcode? - c++

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.

Related

Can we use iOS framework like coremotion in c++ code

I need to build a library in C++, which have to get data from motion sensors. And then library in both android and iOS. It looks like it is possible to do it for android but I am struggling to find an answer for iOS.
You would be needing bridges for ios and android separately. Like for android you need different wrapper / JNI Bridge and different wrapper for ios Objective-c (.mm) files to used the sensor values. Ofcourse you can't share the sensor its different for each platform.
You can check this open source repository to get an idea how the wrappers around C++ and obj-c / Swift can be used.
https://github.com/foundry/OpenCVSwiftStitch
You can use C++ with iOS. The most typical way to do this is through hybrid "Objective-C++" files (.mm), which can compile C++ functionality in an Objective-C wrapper, but you can also use straight C/C++ source and C/C++ libraries. If you want your iOS app to be written in Swift (recommended), it can still use such code - I recommend wrapping the C++ in Objective-C(++) and calling the Objective-C wrappers from Swift with a bridging header.

Is it possible to include framework in C++ project in Xcode?

I have a C++ project generated from Cmake to Xcode. My project is just some static and dynamic libraries. I want to include some 3rd party frameworks to it.
Is it possible? Is there any chance to include? Or I should completely redesign the project to Cocoa app?
I haven't access to the framework source code.
Work with Xcode 9.2.
No, you can't use Cocoa frameworks in pure C++ project.
But you can just create an obj-c project and include all of your existing sources inside - everything will work. Just remember to use .mm extension for the source files, calling C++ code.
You can use swift as well, but that will require to create a wrapper for C++ code.

Porting a C++ project into an iOS app?

project compiles fine, how do I export it as an iOS app though
I have a large C++ project that I'd like to turn into an iOS app... any tips on how I might go about doing this? What I've done so far is to use CMake to generate an XCode project. I've been able to subsequently build (and archive -- but I can't find the archives in the organizer) my project in XCode, but to my understanding this is merely using XCode as an IDE...
Is there an easy way to remedy this situation? Or do I need to reconstruct the project all over again iOS style. If so, any guides you might recommend?
.mm files compile to objective-c++.
This is how you will get c++ code to talk to all the IOS libraries, which unfortunately are only generally easily available in objective-c and swift.
So create c++ interfaces in .h or .hpp files, and back them up with an objective-c++ impl that then talks to the objective-c runtime.
To get cmake to work nicely with iOS, you'll need a toolchain file.
There is a nice collection that you can use as a starting point here:
https://github.com/ruslo/polly

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

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.

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.