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

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.

Related

Wrap a C++ library in Swift code, compile as a Cocoa Touch framework

I have a C++ library that is full of warts and weird features, and I want to make it much easier to use (and reuse) in a Swift project. So I intend to wrap this C++ library in some Swift code, then make it available as a Cocoa Touch framework.
I've added both my (fat) .a file and the required .h files to my project, then added the .a to the "Link Binary with Libraries" section of Build Phases, and added the main .h file to the Project section of the Headers build phase. But the build fails with:
Library not found for -lMyLib
I'm obviously missing something here - I'm guessing maybe I need to adjust the Framework Search Path and/or Header Search Path? Or am I missing something more fundamental? My desired end result is a .framework another developer can pull into their Swift project without having to do any other build setting tweaks.
You're probably looking for "Library Search Paths" (LIBRARY_SEARCH_PATHS). You may need to add to "Header Search Paths (HEADER_SEARCH_PATHS) as well, depending on your setup. In this case, you shouldn't have to add anything to the Framework search paths, since you are not dealing with a Framework.
(As an aside, you are likely going to find that you have to wrap your C++ library in Objective-C++ code to expose the functionality to Swift.)

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.

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

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.

Where does Xcode store .m files of a framework?

In Xcode there are many frameworks (like mkmapkit.framework).
This framework contains only .h (header) files. Where can I find the corresponding .m (implementation) files.
The implementation files are not distributed with Xcode. Apple keeps them proprietary.
The framework classes are available as binary library files (.dylib, .a etc. for example). So .m which are implementation source files are not available. Header .h must be available as without header files compiling is not possible.
The only way to see the implementation is to decompile the frameworks static library with tools like those described in this question:
Decompiling Objective-C libraries
Since the frameworks you are probably interested in will generally have symbols stripped, it will be a bit of a task understanding what is going on but you can glean some things from tools like these.
You can find them inside Apple's internal source code repositories. If you work at Apple on an appropriate team, you can check them out. If you don't, then you're out of luck.

Objective-C, .m / .mm performance difference?

I tend to use the .mm extension by default when creating new classes so that I can use ObjC++ later on if I require it.
Is there any disadvantage to doing this? When would you prefer .m? Does .m compile to a faster executable (since C is generally faster than C++)?
Before Xcode 4.0 (released in 2011), which can use the Clang frontend tool chain for both, the major disadvantage to using .mm over .m for "normal" Objective-C was that compile times are significantly higher for Objective-C++. This is because the C++ compiler takes longer than the C compiler.
A better strategy is to use .m by default. If you need to use Objective-C++ later in development, there is no harm in renaming the file to use a .mm extension. If you so from within Xcode, the project will be automatically updated to use the newly named file.
Of course all of the standard caveats apply once you try to compare Objective-C++ vs. Objective-C performance at run time. Since Objective-C++ is a C++ superset while Objective-C is a C superset, you are dealing with two different languages each with performance tradeoffs at runtime. Given that you're using Objective-X at all, you are likely writing a user-level application (not a systems level app) and the difference in performance between C and C++ wil likely be completely determined by your abilities to code efficient algorithms in each language. If you're a C++ developer, you'll likely code better than in C and visa versa. So, as always, use the appropriate tool for the job.
For reference, you may also be interested in this answer: C vs C++ (Objective-C vs Objective-C++) for iPhone
UPDATE Feb 17, 2012 As of Xcode 4.0 (with LLVM 3.0), Clang has supported Objective-C++. Even C++11 support is quite strong now.
If you only use C features, .mm files should generate code that performs very similar to .m
There is also no downside to renaming a file from .m to .mm later when you desire C++ features