Can I compile my c++ class into static library (.a) in order to use in my iPhone project? - iphone

Due to my image processing class is written in c++ and I want to use it in my iPhone project so , Is it possible to compile this c++ class into static library (.a) and use it in my iPhone project? I also want to know about the command to compile c++ source file into static library (.a) on Mac OS-X and how we call the c++ function that compiled into static library (.a) in Xcode.
Thank you very much.

To make your life easier, you can directly include your C++ source file into your Xcode project. In fact, if you name your Objective-C files with an .mm extension, they will be able to directly use C++ source code (this is actually called Objective C++).
On the other hand, you can make a static library with your processing class and link it to your target, but still, you will need to use Objective C++ (i.e., .mm extensions), since you will anyway need to include the C++ header files and use the compiler ABI (binary interface) to link to C++ binary.
Creating a static library from a C++ source code is no different than creating a static library from C or Objective-C code (since making a static library is simply compile+archive).
Look also at this post from S.O. for more details on the process of creating a universal static library.

Yes, you can.
The only point to notice is that in the link above, the author created an Objective-C class in .m files and headers but in your case you need to import your .cpps and headers.

Related

How to use in ObjectiveC project C++ files

I have a reqular objective C project. I import 10 C++ files into my project. All this files take "are communicating" with each other. If I have a look at File1.h, this file can manage all the operations in other class.
So, the question is How to include reference to File1.h and to call functions from it.
For example: [File1Class getAudioData];
If I include it by #include "File1.h", I will get a error "Class is unknown element"
P.S. I want to import aurioTouch2 sample code to my project and to call functions from theire C++ code.
You Can simply open both the projects in Xcode and drag and drop the files you need from the aurioTouch2 application. Make sure the files are actually copied by selecting the check mark that you get after you drop the files in your xcode project. After this, you will be able to use those files/classes. Consider using ".mm" as the extension of the implementation file in which you want to use the c++ files/classes. You may also use ".mm" as extension for your appdelegate implementation file in case you get c++ related errors.

Using compiled resources in iPhone projects instead of source files

when I have to use third parties code (ex.: three20) I normally import the source code into my iPhone XCode project. Is there a way to import the compiled library (one file only) instead of importing all the source files?
You can use static libraries. Youu can refer for example to this article (XCode 3.X):
http://blog.stormyprods.com/2008/11/using-static-libraries-with-iphone-sdk.html
It explains not only how to use the library, but also how to make one by yourself.
Here is one (maybe better) which shows the same but in XCode 4:
http://blog.carbonfive.com/2011/04/04/using-open-source-static-libraries-in-xcode-4/#using_a_static_library

Use C++ static library in Objective-C Xcode project

I made a cocoa touch static library in iOS in which I have C++ classes (.h and .cpp files).
I built the project successfully, but when I include this library (having .a extension) and any .h file, I get a compilation Error.
How can I add this library in my objective-C project and use the C++ classes?
Your problem is likely that the .h header contains C++ code. You should read up on "compilation units". In short, there's no way to tell what language a header file is written in for the compiler. Therefore, it always uses the language of the source file that includes the header. So if you include a C++ header from a .m file, it will not work.
But there is a solution: Apple invented a "new language" it calls Objective-C++ that lets you write both C++ and Objective-C statements in the same file. For every ObjC file that uses a C++ header, you have to change the file name suffix of the source file that uses it from .m (ObjC) to .mm (ObjC++), which means the source files will be able to compile both ObjC and C++ headers.
Of course, you may not want to change all your files to be ObjC++. For one, C++ (and by extension Objective-C++) is a language with much more complex syntax than C and Objective-C, so your compile times will be longer, and also, C++ behaves differently in some aspects than C (and by extension, ObjC++ behaves a bit differently than ObjC).
What people usually do is constrain the C++ parts to their implementation files, and keep C++ out of the header. They write an Objective-C class that "wraps around" the C++ class, and provides methods that call the corresponding C++ methods on the C++ object. Then any ObjC file in your project can include that class, without having to turn on the ObjC++ compiler itself, which internally ("secretly") uses ObjC++ to call the C++ code.
For some useful tricks on how to hide C++ code inside an ObjC class, see Can I separate C++ main function and classes from Objective-C and/or C routines at compile and link?
Most probably you just should rename your .m files which are objective-c specific to .mm files that can accept C++ code (objective-c++).
Second is to check if everything it depends on is properly included before your library header.
Also check the architecture you have built your library for. If you run on an emulator - it should be x86, if for deploying to device - arm.
If you built the .a on the same system, there shouldn't be a problem #includeing its headers and linking against it.

is there any third party tool to create static library for armv6/armv7?

I have some source code in C/C++. I have to make static library to use in my iPhone application. is there any third party tool to create static library for armv6/armv7 ?
this link will help you create a static library in xcode ...
http://www.icodeblog.com/2011/04/07/creating-static-libraries-for-ios/
just rename the .m file to .mm file and write your c/c++ implementation code in it....
try it for a small function first and if it works for you can implement all your classes in it...
Please make sure when you try to use this library in any other project in XCODE , only use .mm extension for all your files even the appdelegate... hope this helps.
I would recommend iOS Universal Framework. I have used it to create a number of static frameworks that I link against my application projects. It works like a charm and is really easy to setup - no need to come up with your own complicated shell scripts.
It adds a template for a new target project type, which you can then import and use just like an ordinary Apple framework. I would recommend setting up a workspace containing both the framework project and application project, that way the dependencies are handled automatically by Xcode.
In general, you DO NOT WANT to link to a static lib that is made with another compiler than the one used to compile the lib. Static libs are not really portable between compilers, since static lib formats are not covered by C/C++ standards. Due to name mangling and other formatting differences, you may not be able to link at all, or worse, it seems to work but injects bugs. Worse, if you use the standard library from your compiler in your lib, it will create horrible name collisions when someone links to your lib and they don't use an IDENTICAL version of the standard library!
Publish the lib using the compiler you expect them to be using. And if you must use the standard library, then you have to make sure they have the same version you have. Really, you should just have them compile the lib themselves or prebuild it for each compiler you support. It sucks, but that's the reality of it as it stands today.

Compile C library as iPhone framework?

Each C/C++ library has some amount of headers that should be used with that library. And if you're use more than 1-2 libraries, custom header paths is kind of headache.
So I thought: is there a way to compile C libraries as frameworks. Static library + headers + versioning.
I found in XCode template for Cocoa framework but nothing about iPhone framework building. This simple step could allow developers to build and share between each other frameworks with some interesting code.
Also it would be great to use libpng, libjpeg and other libraries packaged as frameworks.
I won't use dynamic libraries in those frameworks. Only static version will be present.
I combined techniques I found here and here into my open source library here (NOTE - I have since removed the relevant scripts from this project. I have updated the link to point to the last revision that included these techniques.). Think that's basically the solution you're looking for.
Basically, it has a target that will build a fat static library (lipo builds for arm6, arm7 and i386). It has another target that takes that library and builds it into a framework.
There's no reason you can't use the same techniques for a C project. In fact I've started work on porting C the VTD XML parser to an Objective C framework using the same techniques.
Frameworks are basically just bundles of dynamic/shared libraries. Since this is not allowed in the App Store, you have to build static libraries and link them with your App's executable at compile time.
However, to ease the pain a little, you can have a Xcode project for each library and compile each library into a static lib. Another way would be to put all required source files into the main Xcode project and configure it appropriately so it all builds at once (I did this with small libraries like Minizip, for instance).
Hope that helps.
the problem you are trying to make already exists - it's called DLL hell
Best way is to stick with plain old static libraries when making small apps and organizing source/headers structure