Recommend way to use a large C++ library in a Cocoa application? - c++

I'm trying to develop an application that can "stack" FITS images. To read FITS images I can either use CCFits (a C++ library) or CFITSIO (A C library) - there is no native Objective-C library.
I would prefer to use CCFits as it allows object-oriented design which I'm hoping will allow me to organize the program better. I've already tried to use CFITSIO but it gets rather unwieldy after a while (of course, this could be because of my inexperience in developing large applications).
So overall, what's the best way to approach this? Should I write wrappers for the CFITSIO functions and write my own classes? Is there a way to use C++ classes in Objective-C - the library contains quite a few classes and I know I can use opaque pointers to wrap around the classes, but how would things like class inheritance be preserved? Will I have to subclass a class in Objective-C manually and wrap the subclass there?

You can easily mix C++ and Objective-C using Objective-C++. In XCode just rename your .m files to .mm and it will recognize the files as Objective-C++. Then you can use the C++ classes in your Objective-C code (some restrictions apply, but not many).
Since Objective-C is an extension to C you can use C libraries in any Objective-C program easily.

Mixing C++ and Objective-C is easy, as #myrkos says.
However do make sure you read up carefully how the two languages interact.
In particular you cannot inherit an Objective-C class from a C++ class, or vice-versa (which you seem to allude to in your question).
That's not usually an issue in practice.
They compose very nicely, however (that is you can have an Objective-C object as a member of a C++ class and vice-versa - as long as it's all in a .mm file).
If you hold a C++ object as an ivar of an Objective-C class by value then constructors and destructors will be called for you automatically.

So overall, what's the best way to approach this?
Use the variant you are comfortable with (CCFits).
Should I write wrappers for the CFITSIO functions and write my own classes?
No.
Is there a way to use C++ classes in Objective-C - the library contains quite a few classes and I know I can use opaque pointers to wrap around the classes, but how would things like class inheritance be preserved?
Objective-C++ allows you to use C, Objective-C, and C++ programs in the same translation with very few issues. Therefore, your implementations can mix Cocoa and C++ as needed. C++ classes can hold ObjC types, and ObjC types can hold C++ types.
Will I have to subclass a class in Objective-C manually and wrap the subclass there?
Just use the CCFits library as-is. There is little to gain by wrapping it in ObjC types, and a lot of time to lose.
If you want to stick with strict ObjC in some areas, then you will need to learn how to hide C++ types behind ObjC interfaces (not a wrapper interface -- just a high level interface for your app's usage of CCFits).

Related

Real time processing data using algorithms written in c++

Here is the scenario:
My swift app collects data from bluetooth and should process them in real-time. These data comes mainly from IMU(gyroscope, magnetometer and accelerometer).
Algorithms that process all of that is going to be written in c++ and utilize some libraries like Eigen. How should I approach such problem? From what I've found:
1) put c++ files into my project, write wrapper in objective-c and bridge it to Swift. Also now sure if I can include Eigen in mobile app easily. This would be tedious process I suppose
2) Get all algorithms as library( .dll, .lib and call it directly from swift, not sure if it's possible)
3) Rewrite all algorithms to Swift, utilize Eigen substitute for Swift, not sure if anything like this exist. Also, this solution is less efficient and would probably fail because of deadlines.
How should I approach such problem? How to solve it in a most efficient way, where I can make use of already exisiting c++ code?
I believe some of you would see this question as opinion based, but I do not know how to state this problem in a way that excludes any ambiguity.
How to run c++ files that uses Eigen in iOS app written is Swift?
Thanks in advance
You cannot call c++ from Swift. However, you can call c++ from objective-c++. And you can call objective-c++ from both Swift and objective-c.
Just make sure that your public-facing #interface code (in the .h file) contains only objective-c. (no classes, no templates, no namespaces, no including c++ headers).
Your #implementation goes in a file with extension .mm which will compile as objective-c++ - giving you an objective-c interface with the full use of c++ in the implementation of the Objective-C object.
c++ objects live quite happily as the member variables of an objective-c++ implementation. However, they will be default-constructed then the objc runtime calls Init. If you are using objects which don't have default constructors, you will need to either wrap them in a boost::optional or a std::unique_ptr (etc).
You can them import the objective-c objects into your Swift program.
Full example for anyone who has not done this before:
https://github.com/madmongo1/swift-to-cpp-demo.git

Call c++ dll class functions from other programming language

I need to make gui application that must call c++ dll class functions. What programming languages are able to call class functions without problems? By problems I mean wrappers and similar things.
Why you don't make the GUI directly in c++?
QT is a well known and mature framework, for instance
C functions in a DLL can be called from most languages.
C++ functions, particularly with classes are difficult at best. Probably they will cause you more trouble than it is worth.
My advice is to just use C++ and prevent the problems.
If you insist on building your GUI with another language X, then your best approach would be to write a glue DLL, written in C++, that shows a C-only interface to the X-GUI and accesses the target C++ classes directly.

What is the difference between c++, objective-c and objective-c++?

I want to know the difference between c++ and objective-c and objective-c++.
Can any one give me the difference and Can we use the c++ for iPhone development
Thank you,
Madan Mohan
C++ is Bjarne Stroustroup's language based on adding classes and metaprogramming to C in such a way that puts most additional work into the compiler, and relies on least possible effort at runtime.
Objective-C is Brad Cox's language based on adding a SmallTalk-style dynamic message-passing runtime library to C, with a small amount of syntax addition to make it easier to use.
Objective-C++ is, to put it bluntly, what you get when you add the Objective-C runtime and syntax to C++. It has its limitations (e.g. you can't create an Objective-C subclass of a C++ class or vice versa, and Objective-C doesn't like C++ namespaces) but allows you to use C++ classes from Objective-C objects and vice versa.
You can use Objective-C++ in iPhone development. What this means practically is that you could write an application whose object model was entirely C++, where the controller layer would need to interface to Objective-C in order to use the Cocoa Touch APIs.
C++ and Objective C were/are two different approaches to adding object orientation to C. Current objective C compilers also accept C++ as input, so you can build a program with some files written in Objective-C and other files written in C++. When C++ is used this way, it's often called Objective-C++.
1) C++ is a language derived from C that adds Object Orientation (OO) amongst other features. *
2) Objective-C is a language derived from C that adds Object Orientation (OO) amongst other features. *
3) Objective-C++ is Objective-C that you can use C++ classes with.
You CAN use C++ for iPhone development but you will need "some" Objective-C code to interface with the iPhone libraries.
(*) Though they both try to solve the same problem they do it quite differently. There is some information about the differences on wikipedia and I'm sure you can use google to find more.
You CAN use C++ for iPhone development but you will need "some" Objective-C code to interface with the iPhone libraries.
This will very likely give you code and possibly memory bloat. As you know, iOS programming should be a lean as possible: minimize both the library size and runtime memory needs. iOS programming and runtime environments are also highly optimized for Objective C.
Pure ObjC is much better than C++ for iOS. Unless you're trying to use existing large C++ code base it will probably be better to re-write from scratch. Nearly all of the C++ STL have analogs in the iOS frameworks, often easier to use, and highly optimized by Apple.
Learn Objective C memory management, get familiar with the frameworks and go for it.

Is Objective-C++ a totally different language from Objective-C?

As the title says... are they considered different languages? For example if you've written an application using a combination of C++ and Objective-C++ would you consider it to have been written in C++ and Objective-C, C++ and Objective-C++ or all three?
Obviously C and C++ are different languages even though C++ and C are directly compatible, how is the situation with Objective-C++ and Objective-C?
From: https://en.wikipedia.org/wiki/Objective-C#Objective-C.2B.2B
Objective-C++ is a front-end to the GNU Compiler Collection, which can compile source files which use a combination of C++ and Objective-C syntax. Objective-C++ adds to C++ the extensions Objective-C adds to C. As nothing is done to unify the semantics behind the various language features, certain restrictions apply:
A C++ class cannot derive from an Objective-C class and vice versa.
C++ namespaces cannot be declared inside an Objective-C declaration.
Objective-C classes cannot have instance variables of C++ classes which do not have a default constructor or which have one or more virtual methods, but pointers to C++ objects can be used as instance variables without restriction (allocate them with new in the -init method).
C++ "by value" semantics cannot be applied to Objective-C objects, which are only accessible through pointers.
An Objective-C declaration cannot be within a C++ template declaration and vice versa. However, Objective-C types, (e.g., Classname *) can be used as C++ template parameters.
Objective-C and C++ exception handling is distinct; the handlers of each cannot handle exceptions of the other type.
Care must be taken since the destructor calling conventions of Objective-C and C++’s exception run-time models do not match (i.e., a C++ destructor will not be called when an Objective-C exception exits the C++ object’s scope). The new 64-bit runtime resolves this by introducing interoperability with C++ exceptions in this sense
Objective-C++ simply allows Objective-C and C++ code to be mixed (with caveats). It's not really a language on its own so much as a mechanism for allowing the two languages to intermix.
C and C++ are not directly compatible. Neither is a superset of the other (though most C is valid C++). Objective-C is a strict superset of C, and Objective-C++ is a strict superset of C++. Those are the only statements you can make (except trivially reversing it).
It's hard to answer this question confidently without understanding what definition of "different language" you want to apply.
Objective-C is a superset of C: it adds some additional syntax on top of the C language. Objective-C++ is a superset of C++ in the same way.
C and C++ are actually different languages. Although C++ is designed to be compatible, there is some C that is not valid C++, and vice versa.
So, I'd say, yes, Objective-C++ is a different language from Objective-C, because C++ is a different language from C. However, I wouldn't call them totally different.
At first glance, using the Objective-C++ dialect looks like a straightforward approach. It is the result of mashing C++ and Objective-C together in the same compiler, and robust implementations exist in GCC and now clang. Considering just how different the details of Objective-C and C++ are, the GCC hackers have done a great job of it. But as you start renaming your .m files to .mm to introduce chunks of C++, you quickly realise it's not quite so simple.
Header files and the C preprocessor in general have caused headaches for C, C++ and Objective-C programmers for decades. It gets worse when you try to mix the languages. Say you wanted to use the STL's map in an Objective-C class in your project. Apple's Foundation libraries to my knowledge don't contain a sorted, tree-based map; one of our StyleKit Components needs exactly that, for example. So we simply create an instance variable for the map in our class and away we go:
#include <map>
#interface MyClass : NSObject {
#private
std::map<int, id> lookupTable;
}
// ...
#end
However, std::map [2] only makes sense to a C++-aware compiler, and only after an #include [3], so this header now can only be #imported from Objective-C++ files. Any code using this class now needs to be converted to Objective-C++ itself, and importing from other headers leads to a cascade effect that quickly encompasses the whole project.
In some cases, this may be acceptable. However, switching a whole project or large parts of it across just to introduce a library which is used in one location is not only excessive; if you're the only one who knows C++ on a project with multiple Objective-C programmers, you might find this to be an unpopular idea. It might also cause issues in the same way that compiling pure C code with a C++ compiler rarely is completely hassle-free. Moreover, it means that code isn't automatically reusable in other Objective-C projects.
So it's always better not to mix up unless it's so important
To know more about strategies for using objective c++ in objective c and vice versa click here
Objective-C is probably the official term that you would put in a resume.
Objective-C++ is not really a new language, it just specifies a few things that allow Objective-C code to co-exist with C++ code. Saying your app was written in Objective-C and C++ or just Objective-C++ is probably what you want. Putting all of Objective-C, Objective-C++, C++ is redundant.

What Are Some Quirks/Surprises with Using .mm Files in Objective-C?

I want to use some C++ STL collections in my Objective-C iPhone app. Apparently this is possible by giving files the extension ".mm" . What are some of the quirks/surprises associated with this?
I want to use the basic containers that I'm familiar with (vector, queue, set, ...)
Cheers!
See Using C++ With Objective-C for a detailed list of what you can and can't do. You can do most things that you would expect. You just can't do things like have a C++ class inherit from an Objective-C class or vice-versa, you can't mix C++ exceptions with Objective-C exceptions, and C++ introduces several new keywords not present in Objective-C.
The major quirk of Objective-C++ is that if you don't pass -fobjc-call-cxx-cdtors to g++, it won't call the constructor and destructor of C++ instance variables in of ObjC objects. So remember to turn that option on and you should be good.
The files will be compiled with the Objective-C++ compiler, so things will behave more like g++ than gcc, but since that's what you want it probably doesn't count as a surprise. From the Objective-C side, I'm not aware of any gotchas, and I've worked on several large projects that made significant used of Objective-C++. Just keep in mind that Objective-C reserved words are reserved in Objective-C++ as well, so you can occasionally run into issues with third-party C++ libraries (e.g., if they use "id" as a parameter in a public interface).
You'd be surprised how well it works. I haven't really gotten into any problems intermixing Obj-C and C++ in a .mm file.