简体   繁体   中英

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

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 (AC 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM