using c++ files in objective c sample tutorial - ios

Where can I get sample tutorial where I can get how to use visual C++ files in XCODE using objective c? I am working on app which is having multiple visual C++ files. Basically this is an gaming application which is developed for windows. And now want to develop it on iPad. So can I reuse those files?
Thank you in Advance.

You can just add the c++ files directly to Xcode and it will compile them (assuming you have all the proper headers etc). You can create 'bridges' to that code by using ObjectiveC++ - meaning you can set the suffix of an ObjectiveC file to '.mm' and the methods in the file will be visible to any other ObjectiveC object, but you can instantiate C++ objects too. In fact if you have C++ ivars (object variables), they get the proper destruct messages when the ObjectiveC class is released. Google 'ObjectiveC++' for more info.

Related

Apple Core ML fails to compile with mixed swift and objective-C

I'm a C++/Python developer recently starting to learn Swift and trying out iOS programming. I have a machine learning model that I've converted into Apple's Core ML format, and have successfully been able to use it in an iOS app in Swift, such as initializing a model with:
var model = MODELNAME()
Now, I have some existing C++ codes that I want to integrate into the app. A simple way to do this is to create a bridging .mm file in Obj-C, and wrapping the C++ code in Obj-c. By itself, the Obj-C wrapper and the C++ codes work without a problem with Swift, and I've been able to use the C++ functions in the iOS Swift app.
However, I noticed a rather confusing bug when I tried to load the Core ML model in the mixed Swift/Objective-C project. If I add .m source codes - even empty files - into the project (which automatically creates a bridging file Project-Bridging-Header.h), the compiler would report an error of "Use of unresolved identifier MODELNAME" for the Core ML model in the Swift code. On a Mac, simpling adding one .m file will cause the project to fail to compile. On iOS, adding one .m file is okay, but adding two .m files causes the same error to appear. Note that, only the .m (and by extension .mm, .cpp) source files affect the compiler, and header files do not.
I'm rather confused with this error...(since somehow, the sheer number of Obj-C sources in the XCode "compiled sources" list - not their contents, affects whether the Core ML model can be compiled). My guess is this might have something to do with how XCode automatically generates a header MODELNAME.h for Core ML models (which usually do not need to be explicitly imported in the code) and this somehow interferes with Obj-C bridging headers Project-Briding-Header.h and the Obj-C source codes.
May I ask if anyone has met similar problems or might have an idea of the reason behind this phenomenon? Thanks!
Screenshot of the project files

using an ios framework for native c++ code for ios

I have written a c++ library that needs opencv which is an image processing library. I want to now use this c++ library on ios. To do that I am going to copy my code to a mac and build to produce a cocoa touch static library.
Since, this has a dependency on opencv, I downloaded its ios framework. But now I am confused whether a framework can be used from c++ code or just from objective c/c++ ? Do I have to recompile this library so that i get c++ libraries or I can use the framework in my c++ code?
Yes, it can be used with c++. You will have to make sure you Type is set to "Objective C++ Source" for where you are making the framework calls.
I mix my C++ code with frameworks all the time.
Note this goes both ways. If you have Obj-C interacting with C++, you'll need to either have the file be a .mm or be of the "Objective C++ Source" Type.
The Type selection is in the File Inspector for files.

Using SNMP++ library in application written in Swift

I'm new in Swift and I would like if someone could tell me how can I use SNMP++ lib (written in C++) in a simple iOS application written in Swift? how to import the lib and begin coding? Thank You.
In 2016, Dariusz Stojaczyk wrote C code to construct probes and deconstruct response of SNMP v1 messages hidden in a project focused on BER. That project contains the code necessary to take one or more SNMP OIDs and construct a full SNMP message, then take a response and decode it, but has no networking code to actually do this.
I took this code and added an ObjectiveC wrapper that can be used to use the existing code to construct a SNMPv1 message, send it to an endpoint using a Socket, and receive a response with a user settable timeout using a dispatch block.
This code is far simpler than any of the big libraries that offer additional services like walking, v2/v3, and traps. If all you want to do is read one or more OIDs, then you should appreciate this code.
Objective-C can be mixed with C++.
That's called Objective-C++ (.mm files)
So if you can get the code into those files, in theory you can create a bridging header to deal with linking your swift files: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
Swift 2.2 does not work with C++, and neither will Swift 3. So it's not coming any time soon which means you simply can't develop a Swift app with a C++ library.
However Objective-C works with C++, so although you don't have to write your entire app in Objective-C, any code that deals with a C++ library will have to be written in Objective-C (or in C...).
You can then link your Objective-C code to you Swift app with a bridging header.

How to organize Android NDK / iOS project

I wrote a game for Android using Android NDK. Core of game logic written in pure C++. So, I want to port it to iOS. I know how to port Java (JNI) wrapping to Objective-C.
My question is how to organize this project on FS? I want to keep a single code base, and I use git (link).
First, note that there should be no need at all do wrap your C++ code: the objective-C extension of the C language is also compatible with C++. So, you can compile your code as "Objective-C++" and directly use C++ classes with Objective-C code.
Second, you can put your code anywhere you wish on the disk. Both eclipse for Android and xcode for iOS will let you include source code into your project from any relative directory.
If you ever need platform-specific native code you can use #ifdef __ANDROID__ for android-specific code inside your C++.

Can an iOS static library contain classes/interfaces, that can be used/implemented by iOS code that is using the library?

I apologize if this is a dumb question, or if it does not make sense. I've written some Objective-C code before, but I am not very familiar with writing code for OS X or iOS; I'm pretty much a novice. Currently, I'm trying to port a project from OS X into iOS. The project compiles into a Framework, that other OS X projects can use.
I'm trying to do something similar for iOS. I understand that iOS does not support Frameworks that contain dylibs and that the solution is to create a static library. However, the OS X Framework has several classes (in .m files) that the implementing code uses, extends, or implements. All the examples I've seen for static libraries seem to define a header-file with some functions that can be compiled into a static library.
Is it possible to have classes inside the static library, that iOS code can use? Also, how can I tell if the code is using dynamic libraries?
Yes, static libraries can contain Objective-C classes.
In some cases, you will need to pass options to the linker to force it to include all of the classes and categories defined in a static library. See http://developer.apple.com/library/mac/#qa/qa1490/_index.html

Resources