Using a c++ static library (CCFits) in Xcode iOS Project - c++

I am trying to using the CCfits library in an iOS project. CCfits is a C++ wrapper to the c library cfitsio.
I have the source code to both of these, and also have them built as static libraries (.a files). I would like to add the CCfits library (libCCfits.a) to an iOS project but when I am confused on how to access the library classes from my iPhone source files. I have the library added under "Link Binary with Libraries".
I understand that Xcode needs me to import a header file, but what is the proper way to attach the headers to the project? Do I need to make a framework file somehow? Or is the .a file enough?

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.
See https://stackoverflow.com/posts/10156243/edit

Related

Using Cocoa static library built in c++, some ObjC code tries to rebuilt it in OC

I am programming in Xcode in ObjC using a Cocoa static library built in c++ (built by me). For the library project, I made one public header(.hpp) that includes all the other header files(.hpp). Then in the ObjC project, I wrote a wrapper code(.hpp and .mm) to play as the interface, so it includes the public hpp header. To access the library, I then include the wrapper code in a normal m file. I believe all of these followed the tutorials, as it worked well for a few weeks.
However, after doing something stupid yesterday, the compiler for the ObjC project started to insist on building all the headers in ObjC. It first had problems with openCV packages in the c++ library, producing errors saying thoses are supposed to be built in c++ (as shown below). Even if I removed the openCV, the compiler cannot find any standard c++ libararies. I tried renaming the .hpp's to .h but it did not work.
Any suggestions on what I did wrong and how I can fix this?Much appreciated!
()
Your errors are coming from compiling ImageProcessor.m. .m is the extension Obj-C files, so the compiler will try to compile it, and everything it includes as Obj-C.
I'd suggest renaming it to ImageProcessor.mm.
After learning more about header files, I realized what I did wrong: I included every header in the interface, so they are re-built every time outside the library, even if they are referenced by none-c++ code. The solution is to separate the interface header with other headers in the library completely.

Adding 3rd party c++ library into objective C project

I have 3rd party static library.. It contains only (.h and .hpp) header files.. They are in C++ language.. But my project was in objective C. Can i use these libraries in my project and is it possible to call the c++ functions in my objective c project.. ???
Yes, you can do that. C++ is fully supported in Objective-C. Just rename the relevant Objective-C implementation files from .m to .mm, and they start to be compiled as C++ now, instead of “plain C mode.” These .mm files can directly call your C++ code.

Import C++ code to IOS application [duplicate]

This question already has answers here:
How can I use C++ with Objective-C in XCode
(2 answers)
Closed 8 years ago.
I write a code in C++, and I want to use some of it's methods in my IOS application, so is it possible to (import) C++ library "t.cpp" to IOS application in XCode?
if yes what's the simple way to do it?
Yes, that will work without error, however you might want to change the Xcode C++ compiler settings (language level and runtime library), depending on the version of iOS you are using.
When it comes to actually using the C++ classes within your Objective-C code, you simply need to rename any files from .m to .mm.
A complication occurs when you want to include the C++ headers in Objective-C headers where both .m and .mm see that Objective-C header file. In this case you might find that you need to change many more files from .m to .mm in order for this to work, but there are ways around this if that becomes too onerous.
Just add the file to the project.
You need to rename a file from .m too .mm only if the translation unit contains a C++ header. Then, the module gets "infected" by C++ and needs to be compiled with the Objective-C++ compiler.
This is only required if any C++ header will be directly or indirectly included/imported from that module. There is no need to rename all files.
Additionally, if this C++ code depends on the C++ standard lib, you need also ensure, that the executable binary links against the C++ standard lib, via setting the appropriate build setting in the executable binary.
For example, in the target build settings of your app, add the following option to Other Linker Flags:
-lc++
e.g.:
OTHER_LDFLAGS = -ObjC -lc++
Caution:
If possible, don't include/import a C++ header in a public Objective-C header, since then all modules will be infected by C++ when they import this Objective-C header and become Objective-C++.

Objective C Project using C++ POSIX Classes

I have to create a iOS Programm using Code of some C++ POSIX Classes.
I already read the "Using C++ With Objective-C" manual of the Apple Developer Center.
They describe how to mix C++ & Objective C code in a .mm file.
My question is, is there any possibility to use the C++ Classes in my .h/.m files of a normal Objective C Project?
Or is it necessary to write the whole Project in that .mm file style with its own main?
You can combine in a project any types of files, say .c, .m, .cpp and .mm, and the compiler is chosen automatically depending on the extension. For example, you can keep the standard main.m file which comes with the XCode template, and add your new .h and .mm files to use Objective-C++.
In other words, there's no distinction between a normal Objective-C project and a Objective-C++ project. You just have to use .mm extension for the specific files which needs Objective-C++. This can be used in any project.
You have to force compilation of the Obj-C files in which you want to use C++ to Obj-C++ in the build menu. You can then create and use C++ objects in your Obj-C classes.
It depends on how you want to use the C++. In my projects I usually only make a few calls out to do some heavy lifting, etc.
What I do is have C++ in .cpp files, then create a few .mm files that have headers that have no C++ in them. These .mm files are obj-c wrappers for the C++. Then the C++ is 'contained' to the original posix files, plus a few files that give the C++ classes and calls all the interface you need. As few .mm files as possible is a good thing.
Keeping the C++ out of most of your code makes debugging, etc easier.
--Tom
Select Project directory in Xcode, In Build Settings tap, choose "Compile Sources As" is Objective-C++. No need change name type from .m to .mm.

Use boost library in cocoa project

It is theoretically possible to use a boost library (e.g. boost threads) inside a cocoa project?
Yes, there is nothing stopping you from doing that:
you can mix Objective-C and C++ - the result is called Objective-C++
you can of course also link to C and C++ libraries
Yes. You can compile boost as a static library and link against it, or you can add the source code to your project and compile it with it.
Yeah, just make sure you use Objective-C++ and include to the boost directories you need. (Note: to change to objective-C++ change the file extension to .mm instead of .m, and keep the .h files the same)