Include Apple's SimplePing (Objective-C) in Qt C++ Project - c++

ICMP ping for MacOS is not possible with Qt, because it would require raw sockets. There is however a SimplePing Objective-C code available from Apple, and a SimplePingHelper from Chris Hulbert:
https://developer.apple.com/library/archive/samplecode/SimplePing/Listings/Common_SimplePing_m.html#//apple_ref/doc/uid/DTS10000716-Common_SimplePing_m-DontLinkElementID_4
http://www.splinter.com.au/how-to-ping-a-server-in-objective-c-iphone/
https://github.com/chrishulbert/SimplePingHelper
My problem is that this is Objective-C, and not Objective-C++, and to use it with Qt I would need to wrap it into an Objective-C++ class where the header is pure C++ and the Objective-C/C++ SimplePing code in the .mm implementation of that class.
Although I managed to include other Objective-C++ code in Qt projects, I'm stuck with this.
(Alternatively, to use QProcess to run Ping is not desired, the overhead is too big for the volume of pings needed here.)

Related

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

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.

CoreBluetooth with C++

I need to send data via bluetooth from iOS app into a C++ application. So
I have to implement Bluetooth module in c++, in iOS app using Corebluetooth framework.I created a wrapper class of bluetooth module for C++ app. I can call the iOS function from C++ application but, C++ doesn't have Corebbluetooth. Is it possible to create wrapper class for corebluetooth to use with C++ ?
If possible how to create the same?
Yes, that is possible.
You just need to create an Objective-C++ wrapper that forwards the messages to your C++ code. Objective-C++ allows you to use C++ code in your Objective-C classes. To use it, you either need to set the extensions of source files with your Objective-C wrapper from .m to .mm or set the sources to Objective-C++ in the File Inspector in Xcode.

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.