1

I am doing my first steps in IPhone developing. I want to use some c\c++ code but I can't find any reference of how it's done (will very appriciate if you can also refer me to your source when you give an answear)

I have a file called calc.h containing a "calculator" class with simple add and mult functions, I imported it exactly as I did with an Objective-C header file. What am I suppose to do now?

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
AYBABTU
  • 986
  • 3
  • 17
  • 39

2 Answers2

0

Whatever you were doing when you were coding just in C/C++.

For example:

#include "calc.h"
....
-(void) testCode { //obj-c
 float x = 3;
 float y = 8.0;
 float sumOfTwo = sum(x, y);
}

Assuming that you have a function named sum in your header file similar with the one used above.

Alin
  • 568
  • 2
  • 9
  • I did exactly that with objective c header files. The difference is that I have a class in the c++ file. How do I address a function in the class? Will appriciate if you can also refer me to a source where I can read about this. – AYBABTU Sep 23 '10 at 15:35
  • 1
    1. Rename your .m file to .mm 2. Create an object of that class type as you do it normally in c++. 3. Call a method on that object. Again, nothing special except for the file name. http://www.macresearch.org/objective_c_for_objective_c_programmers and http://gavmacprogramming.wordpress.com/2007/04/26/c-for-objc-programmers-part-i/ – Alin Sep 23 '10 at 15:47
0

If you use standard extensions for the source files (e.g. .cpp / .c) you can simply build them without doing anything special.
If you use uncommon extensions you need to set the file type manually: In FileGet Info set File Type to sourcecode.cpp.cpp / sourcecode.c.c.

Note however that you can't use C++ in plain Objective-C (.m) files - if you want to do that you need to use Objective-C++ (.mm) instead.
For using C there is no such restriction as Objective-C is a superset of C - as with C++ you may need to watch out for uses of identifiers that are keywords in Objective-C though.

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
  • Can you refer me to a source discussing this issue? – AYBABTU Sep 23 '10 at 15:38
  • @Gal: There is a link to the Apple Objective-C++ discussion in my answer. For related problems see [here](http://robnapier.net/blog/wrapping-c-take-2-1-486), [here](http://stackoverflow.com/questions/1885586/language-mixing-model-and-view/1885649), [here](http://stackoverflow.com/questions/2226912/can-i-separate-c-main-function-and-classes-from-objective-c-and-or-c-routines-a/2227013), [here](http://stackoverflow.com/questions/2746215/c-reference-type-as-instance-variable-in-objective-c/2746250), ... – Georg Fritzsche Sep 23 '10 at 15:44